使用File Provider调用相机获取高清图片并显示于ImageView问题求助
解决相机拍照后ImageView无法显示高清图片的问题
1. 检查File Provider配置
- 在
AndroidManifest.xml中确认File Provider配置正确,authorities需与代码中的一致:
<provider android:name="androidx.core.content.FileProvider" android:authorities="com.yourpackage.name.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider>
- 对应的
res/xml/file_paths.xml需正确指向照片存储目录,示例:
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-files-path name="camera_images" path="Pictures" /> </paths>
2. 正确生成相机拍照的Uri与权限授予
- 创建存储照片的本地文件:
private File createImageFile() throws IOException { String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()); String fileName = "CAMERA_" + timeStamp + ".jpg"; File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); return File.createTempFile(fileName, null, storageDir); }
- 启动相机时通过File Provider生成Uri,并授予临时访问权限:
File photoFile = createImageFile(); Uri photoUri = FileProvider.getUriForFile(this, "com.yourpackage.name.fileprovider", photoFile); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); // 给相机APP授予Uri读写权限 List<ResolveInfo> resInfos = getPackageManager().queryIntentActivities(cameraIntent, PackageManager.MATCH_DEFAULT_ONLY); for (ResolveInfo info : resInfos) { grantUriPermission(info.activityInfo.packageName, photoUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); } // 用Activity Result API启动相机(替代已废弃的startActivityForResult) ActivityResultLauncher<Intent> cameraLauncher = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), result -> { if (result.getResultCode() == RESULT_OK) { // 传递图片路径到DisplayActivity Intent displayIntent = new Intent(this, DisplayActivity.class); displayIntent.putExtra("IMAGE_PATH", photoFile.getAbsolutePath()); startActivity(displayIntent); } }); cameraLauncher.launch(cameraIntent);
3. 加载高清图片到ImageView
直接加载原文件避免压缩模糊,推荐用Glide简化处理(需先添加Glide依赖):
// DisplayActivity中获取图片路径并加载 String imagePath = getIntent().getStringExtra("IMAGE_PATH"); File imageFile = new File(imagePath); Glide.with(this) .load(imageFile) .into(yourImageView);
如果手动处理Bitmap(需注意OOM):
public Bitmap getSampledBitmap(File file, int targetWidth, int targetHeight) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(file.getAbsolutePath(), options); // 计算采样率,避免加载过大Bitmap options.inSampleSize = calculateSampleSize(options, targetWidth, targetHeight); options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(file.getAbsolutePath(), options); } private int calculateSampleSize(BitmapFactory.Options options, int targetWidth, int targetHeight) { int width = options.outWidth; int height = options.outHeight; int sampleSize = 1; while (width / sampleSize > targetWidth || height / sampleSize > targetHeight) { sampleSize *= 2; } return sampleSize; } // 调用加载 Bitmap bitmap = getSampledBitmap(imageFile, yourImageView.getWidth(), yourImageView.getHeight()); yourImageView.setImageBitmap(bitmap);
4. 权限检查
- 确保已动态申请
CAMERA权限; - Android 10及以下需申请
WRITE_EXTERNAL_STORAGE,Android 11+使用getExternalFilesDir存储无需该权限。
内容的提问来源于stack exchange,提问作者Muhammad Mubeen
相关产品推荐
相关产品推荐

