手机多媒体

通知

下面的实例是点击按钮发出一条通知,点击通知之后跳转到详情页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.example.study.media;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import com.example.study.MainActivity;
import com.example.study.R;

public class NoticeActivity extends AppCompatActivity {

private Button sendNotice;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notice);
sendNotice = (Button) findViewById(R.id.send_notice);
sendNotice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//2,设置点击事件
Intent intent = new Intent(NoticeActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
String channeId = createNotificationChannel("my_channel_ID", "my_channel_NAME", NotificationManager.IMPORTANCE_HIGH);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//3,构造Notification对象并通知显示
NotificationCompat.Builder notification = new NotificationCompat.Builder(getApplicationContext(), channeId)
.setContentTitle("通知") //设置通知的标题内容
.setContentText("收到一条消息") //设置通知的正文内容
.setContentIntent(pendingIntent) //点击通知后跳转到详情
.setWhen(System.currentTimeMillis()) //指定通知被创建的时间
.setSmallIcon(R.mipmap.ic_launcher) //设置通知的小图标
.setAutoCancel(true) //用户在点击之后会自动移除通知
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
//设置通知的大图标
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))); //设置大图片
.setStyle(new NotificationCompat.BigTextStyle().bigText("蒹葭苍苍,白露为霜,所谓伊人,在水一方,溯洄从之,道阻且长,溯游从之,宛在水中央,蒹葭萋萋,白露未晞,所谓伊人,在水之湄,溯洄从之,道阻且跻,溯游从之,宛"));
//添加大文本
manager.notify(1, notification.build());
}
});
}


//1,创建渠道
private String createNotificationChannel(String channelID, String channelNAME, int level){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel(channelID, channelNAME, level);
manager.createNotificationChannel(channel);
return channelID;
} else {
return null;
}
}
}

调用摄像头和相册并展示图片示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/btn_choose_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选择图片"/>

<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"/>

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:background="@drawable/dialog_bg_shape"
android:orientation="vertical">

<TextView
android:id="@+id/dialog_title"
android:textSize="14dp"
android:text="请选择上传方式"
android:textColor="#777777"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="37dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#eeeeee"/>

<TextView
android:id="@+id/photograph"
android:layout_width="match_parent"
android:layout_height="41dp"
android:gravity="center"
android:text="拍照"
android:textColor="#444444"
android:textSize="18sp" />

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#eeeeee"/>

<TextView
android:id="@+id/photo"
android:layout_width="match_parent"
android:layout_height="41dp"
android:text="从相册中选择"
android:textSize="18sp"
android:textColor="#444444"
android:gravity="center"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_margin="10dp"
android:background="@drawable/dialog_bg_shape"
android:layout_height="wrap_content">
<TextView
android:id="@+id/cancel"
android:layout_width="match_parent"
android:layout_height="47dp"
android:gravity="center"
android:text="取消"
android:textColor="#444444"
android:textSize="18sp"/>
</LinearLayout>

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="10dp" />
<solid android:color="#ffffff" />
</shape>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package com.example.study.media;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;

import com.example.study.R;
import com.zxy.tiny.Tiny;
import com.zxy.tiny.callback.FileWithBitmapCallback;

import java.io.File;
import java.io.IOException;

public class MyCameraActivity extends AppCompatActivity implements View.OnClickListener {

//调取系统摄像头的请求码
private static final int MY_ADD_CASE_CALL_PHONE= 6;
//打开相册的请求码
private static final int MY_ADD_CASE_CALL_PHONE2 = 7;
private static final String TAG = "MyCameraActivity";
private Button chooseImage;
private ImageView image;
private AlertDialog mDia;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
chooseImage = (Button) findViewById(R.id.btn_choose_img);
image = (ImageView) findViewById(R.id.image);
chooseImage.setOnClickListener(this);

}

@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_choose_img:
//点击按钮,弹出一个Dialog选择以哪种方式上传图片
showDilg();
break;
case R.id.photograph:
//弹出摄像头
callCamera();
break;
case R.id.photo:
//弹出相册
callingAlbum();
break;
case R.id.cancel:
//取消
mDia.dismiss();
break;
default:
break;
}
}

private void callingAlbum() {
if(ContextCompat.checkSelfPermission(MyCameraActivity.this, Manifest.permission.CAMERA) !=
PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(MyCameraActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MyCameraActivity.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_ADD_CASE_CALL_PHONE2);

} else {
//在相册中选择图片
choosePhoto();
mDia.dismiss();
}
}

private void choosePhoto() {
Intent picture = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(picture, 2);
}

private void callCamera() {
// Log.e(TAG, "callCamera: click");
//这个方式写请求权限着实有点复杂,可以去看看网上看看封装好的框架或者是自己写一个,今天写这么多就是为了学习。
if(ContextCompat.checkSelfPermission(MyCameraActivity.this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(MyCameraActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MyCameraActivity.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},MY_ADD_CASE_CALL_PHONE);
} else {
//有权限,去打开摄像头
try {
takePhoto();
} catch (Exception e){
e.printStackTrace();
}
mDia.dismiss();
}

}

private void takePhoto() throws IOException {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

//获取文件
File file = createFileNeed("UserIcon.png");
//拍照后将原图灰存此路径下
Uri uri;
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.M){
uri = Uri.fromFile(file);
} else {
uri = FileProvider.getUriForFile(this, "com.example.study.media.fileprovider", file);

}
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, 1);

}

private File createFileNeed(String filename) throws IOException {
String fileA = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/tzpic";
File fileJA = new File(fileA);
if(!fileJA.exists()){
fileJA.mkdirs();
}
File file = new File(fileA,filename);
if(!file.exists()){
file.createNewFile();
}
return file;
}

//展示选择的弹出框
private void showDilg() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View layout = getLayoutInflater().inflate(R.layout.dialog_select_photo, null);
builder.setView(layout);
mDia = builder.create();
Window window = mDia.getWindow();
window.setGravity(Gravity.BOTTOM);
mDia.setCanceledOnTouchOutside(true);
mDia.show();

WindowManager manager = getWindowManager();
Display d = manager.getDefaultDisplay();
WindowManager.LayoutParams p = mDia.getWindow().getAttributes();
p.width = d.getWidth();
window.setBackgroundDrawable(new ColorDrawable(0));

TextView button1 = layout.findViewById(R.id.photograph);
TextView button2 = layout.findViewById(R.id.photo);
TextView button3 = layout.findViewById(R.id.cancel);

button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if(requestCode == MY_ADD_CASE_CALL_PHONE){
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
try {
takePhoto();
} catch (Exception e){
e.printStackTrace();
}
} else {
Toast.makeText(this, "请求摄像头权限被拒绝", Toast.LENGTH_LONG).show();
}
}
if(requestCode == MY_ADD_CASE_CALL_PHONE2){
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
choosePhoto();
} else {
Toast.makeText(this, "请求SDK权限被拒绝", Toast.LENGTH_LONG).show();
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

@Override
protected void onActivityResult(int requestCode, int resultCode,Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode != Activity.RESULT_OK){
String state = Environment.getExternalStorageState();
if(!state.equals(Environment.MEDIA_MOUNTED)) return;
//把原图显示到界面上
Tiny.FileCompressOptions options = new Tiny.FileCompressOptions();
Tiny.getInstance().source(readPic()).asFile().withOptions(options).compress(new FileWithBitmapCallback() {
@Override
public void callback(boolean isSuccess, Bitmap bitmap, String outfile, Throwable t) {
saveImageToSetver(bitmap, outfile);
}
});
} else if(requestCode == 2 && resultCode == Activity.RESULT_OK && data != null){
try {
Uri selectdImage = data.getData();
Tiny.FileCompressOptions options = new Tiny.FileCompressOptions();
Tiny.getInstance().source(selectdImage).asFile().withOptions(options).compress(new FileWithBitmapCallback() {
@Override
public void callback(boolean isSuccess, Bitmap bitmap, String outfile, Throwable t) {
saveImageToSetver(bitmap, outfile);
}
});
} catch (Exception e){

}
}
}

private void saveImageToSetver(final Bitmap bitmap, String outfile) {
//File file = new File(outfile);
//这里可以将图片上传到服务器上,上传成功后将bitmap对应的图片展示
image.setImageBitmap(bitmap);
}

private String readPic() {
String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/tzpic/" + "UserIcon.png";
return filePath;
}

}