我想要的是,我的应用程序照片可以在手机画廊和我的应用程序内可见。
我想显示我的应用程序中以前的所有图像,并这样做(我删除了一些关于填充recyclerView以提高其可读性的代码):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivPhoto = findViewById(R.id.photo);
photoPaths = new ArrayList<>();
File directory = getExternalFilesDir(Environment.DIRECTORY_DCIM);
File[] files = directory.listFiles();
if (files != null && files.length > 0) {
for (File file : files) {
photoPaths.add(file.getAbsolutePath());
}
}
if (photoPaths.size() > 0) {
File file = new File(photoPaths.get(0));
try {
ExifInterface exif = new ExifInterface(file.getCanonicalPath());
String text = exif.getAttribute(ExifInterface.TAG_EXIF_VERSION);
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
} catch (IOException exception) {
exception.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeFile(photoPaths.get(0));
ivPhoto.setImageBitmap(bitmap);
}
}我用来拍摄相机照片的方法:
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_PREFIX_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
ex.printStackTrace();
}
if (photoFile == null) {
return;
}
Uri photoURI;
Uri photoURIQ;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentValues values = new ContentValues(2);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_DCIM);
photoURIQ = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURIQ);
} else {
photoURI = getUriForFile(this, "com.example.photometadata.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
}
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}上面的代码使拍摄的照片在画廊可见,但当然没有图片在我的应用文件夹。
我可以更改代码,使下一张照片在我的应用程序中可见,但这样,他们将无法在画廊可见。
我试图从图片库中检索照片文件并将其保存到getExternalStorageDir()文件夹中的onActivityResult中,但得到了EACCESS SecurityExeption。我试过别的东西,但没什么能像我想要的那样工作。
发布于 2021-03-05 10:13:26
getExternalFilesDir(Environment.DIRECTORY_DCIM);文件目录=
目录getExternalFilesDir()是一个应用程序特定的目录。
它不能被MediaStore扫描。
而且,由于画廊的应用程序主要是查询MediaStore的图像,他们将不会看到它们。
把它们存放在不同的地方,或者以不同的方式存放。
https://stackoverflow.com/questions/66489983
复制相似问题