1.通知的更多效果
Notification notification = new NotificationCompat.Builder(context)
- 设置声音 .setSound(Uri)
- 设置震动 .setVibrate()
- 设置提示灯 .setLight()
- 设置重要程度 .setPriority()
2.调用摄像头拍照
想要调用摄像头拍照,需要完成如下几个步骤:
- 创建File,用于存储拍照后的图片
- 使用FileProvider获得图片的Uri
- 将Uri参数传入,启动相机程序
- 将拍摄的照片显示出来
FileProvider是一种特殊的内容提供器,它使用了和内容提供器类似的机制来对数据进行保护,可以选择性地将封装过的Uri共享给外部,从而提高了应用的安全性。
public class MainActivity extends AppCompatActivity {
private static final String AUTHORITY = "com.futuring.camerademo.fileprovider";
private static final int TAKE_PHOTO = 1;
private Button takePhoto;
private ImageView picture;
private Uri imageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
takePhoto = findViewById(R.id.take_photo);
picture = findViewById(R.id.picture);
takePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//第一步 创建File,用于存储拍照后的图片
File outputImage = new File(getExternalCacheDir(), "output_image.jpg");
if (outputImage.exists()) {
outputImage.delete();
}
try {
outputImage.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
//第二步 获得图片的Uri
imageUri = FileProvider.getUriForFile(MainActivity.this, AUTHORITY, outputImage);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
//第三步 将Uri参数传入,启动相机程序
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
//返回可执行的activity的相关信息
List<ResolveInfo> cameraActivities = getActivity().getPackageManager()
.queryIntentActivities(intent,PackageManager.MATCH_DEFAULT_ONLY)
//给可执行intent的活动临时授权
for (ResolveInfo activity : cameraActivities) {
getActivity().grantUriPermission(activity.activityInfo.packageName,
imageUri ,Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
startActivityForResult(intent, TAKE_PHOTO);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
switch (requestCode) {
case TAKE_PHOTO:
if (resultCode == RESULT_OK) {
try {
//第四步 将拍摄的照片显示出来
Bitmap bitmap = BitmapFactory.decodeStream
(getContentResolver().openInputStream(imageUri));
picture.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
break;
default:
break;
}
}
}
//AndroidMainfest.xml
<provider
android:authorities="com.futuring.camerademo.fileprovider"
android:exported="false"
android:name="android.support.v4.content.FileProvider"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_path"
/>
</provider>
// res/xml/file_path
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="output_image"
path="" /> //表示将整个external-path路径下的文件共享
</paths>
网友评论