频道栏目
首页 > 程序开发 > 移动开发 > Android > 正文
Android 7.0系统获取相机、相册实现代码
2017-10-09 14:17:40      个评论    来源:ff_hh的博客  
收藏   我要投稿

Android 7.0系统获取相机、相册实现代码。

Android 获取相机 :

需要的的权限Manfest.permission.WRITE_EXTERNAL_STORAGE

需要注意运行时权限intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)//增加读取 Uri 权限

以及FileProvider的使用

获取相册及相机方法

/**

* 获取相机 已适配7.0

*

* @param activity

*/

public synchronized void userCamera(Activity activity) {

if (!EaseCommonUtils.isSdcardExist()) {

Toast.makeText(activity, R.string.sd_card_does_not_exist, Toast.LENGTH_SHORT).show();

return;

}

MyRequestPermission.getInstance(activity).setOnRequestPermissionListener(result -> {

if (result) {

camera(activity);

} else {

Toast.makeText(activity, "读取SD卡权限申请被拒,相机无法启动", Toast.LENGTH_SHORT).show();

}

});

boolean checkPermission = MyRequestPermission.getInstance(activity).isCheckPermission(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE});

Observable observable = Observable.create(e -> e.onNext(checkPermission));

observable.filter(aBoolean -> {

if (!aBoolean)

Toast.makeText(activity, "读取SD卡权限申请被拒,相机无法启动", Toast.LENGTH_SHORT).show();

return aBoolean;

}).subscribe(aBoolean -> camera(activity));

}

private void camera(Activity activity) {

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/test/" + System.currentTimeMillis() + ".jpg");

file.getParentFile().mkdirs();

Uri uriForFile = FileProvider.getUriForFile(activity, "com.tiancai.tcggsc.fileprovider", file);

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)//增加读取 Uri 权限

.putExtra(MediaStore.EXTRA_OUTPUT, uriForFile);

activity.startActivityForResult(intent, Constant.REQUEST_CAMERA);

}

/**

* 打开相册

* @param activity 当前activity

*/

public void openPic(Activity activity) {

if (!EaseCommonUtils.isSdcardExist()) {//是否存在SD卡

Toast.makeText(activity, R.string.sd_card_does_not_exist, Toast.LENGTH_SHORT).show();

return;

}

MyRequestPermission.getInstance(activity).setOnRequestPermissionListener(result -> {

if (result) {

openPicture(activity);

} else {

Toast.makeText(activity, "读取SD卡权限申请被拒,无法打开相册", Toast.LENGTH_SHORT).show();

}

});

boolean checkPermission = MyRequestPermission.getInstance(activity).isCheckPermission(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE});

Observable observable = Observable.create(e -> e.onNext(checkPermission));

observable.filter(aBoolean -> {

if (!aBoolean)

Toast.makeText(activity, "读取SD卡权限申请被拒,无法打开相册", Toast.LENGTH_SHORT).show();

return aBoolean;

}).subscribe(aBoolean -> openPicture(activity));

}

private void openPicture(Activity activity){

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

photoPickerIntent.setAction(Intent.ACTION_GET_CONTENT);

photoPickerIntent.setType("image/*");

activity.startActivityForResult(photoPickerIntent, Constant.REQUEST_PHOTO_ALBUM);

}

activity 方法

@Override

public void onRequestPermissionsResult(int requestCode, @android.support.annotation.NonNull String[] permissions, @android.support.annotation.NonNull int[] grantResults) {

super.onRequestPermissionsResult(requestCode, permissions, grantResults);

MyRequestPermission.getInstance(this).onRequestPermissionsResult(requestCode, permissions, grantResults);

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK) {

switch (requestCode) {

case Constant.REQUEST_CAMERA:

File file = FileUtil.getInstance().getFile();

imgSetUserIcon.setImageURI(Uri.fromFile(file));

//在手机相册中显示刚拍摄的图片

Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

Uri contentUri = Uri.fromFile(file);

mediaScanIntent.setData(contentUri);

sendBroadcast(mediaScanIntent);

break;

case Constant.REQUEST_PHOTO_ALBUM:

//

if (Build.VERSION.SDK_INT >= 19) {//注意4.4读取图片路径 不相同

handleImageOnKitKat(data);

} else {

handleImageBeforeKitKat(data);

}

break;

}

}

}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)

private void handleImageOnKitKat(Intent data) {

String imagePath = null;

Uri uri = data.getData();

if (DocumentsContract.isDocumentUri(this, uri)) {

String docId = DocumentsContract.getDocumentId(uri);

if ("com.android.providers.media.documents".equals(uri.getAuthority())) {

String id = docId.split(":")[1];

String selection = MediaStore.Images.Media._ID + "=" + id;

imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);

} else if ("com.android.provider.downloads.documents".equals(uri.getAuthority())) {

Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));

imagePath = getImagePath(contentUri, null);

}

} else if ("content".equalsIgnoreCase(uri.getScheme())) {

imagePath = getImagePath(uri, null);

} else if ("file".equalsIgnoreCase(uri.getScheme())) {

imagePath = uri.getPath();

}

displayImage(imagePath);

}

private void handleImageBeforeKitKat(Intent data) {

String imagePath = null;

Uri uri = data.getData();

imagePath = getImagePath(uri, null);

displayImage(imagePath);

}

// getImagePath的实现:

private String getImagePath(Uri uri, String selection) {

String Path = null;

Cursor cursor = getContentResolver().query(uri, null, selection, null, null);

if (cursor != null) {

if (cursor.moveToFirst()) {

Path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));

}

cursor.close();

}

return Path;

}

// displayImage的实现:

private void displayImage(String Path) {

Bitmap bm = BitmapFactory.decodeFile(Path);

imgSetUserIcon.setImageBitmap(bm);

}


点击复制链接 与好友分享!回本站首页
上一篇:Android开发中如何实现按钮渐变色?
下一篇:android浏览器弹出该网站的安全证书有问题如何解决
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站