【发布时间】:2017-03-24 16:29:13
【问题描述】:
我正在关注 Googles Official 文档,了解如何将使用相机拍摄的照片保存到图库。
他们希望您使用getExternalFilesDir 创建一个文件。
String mCurrentPhotoPath;
private File createImageFile() throws IOException {
// Create an image file name
String imageFileName = "JPEG_" + UUID.randomUUID();
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
mCurrentPhotoPath 等于/storage/emulated/0/Android/data/com.mycompany.myapp/files/Pictures/JPEG_22fda6f2-dad9-4dd9-b327-c1130c8df0eb187766077.jpg
但在下一部分,最重要的部分,将照片添加到图库,
他们说:
如果您将照片保存到由 getExternalFilesDir(),媒体扫描器无法访问文件 因为它们对您的应用来说是私有的。
他们使用的确切方法getExternalFilesDir()。 :-(
所以我也查看了documentation。而且我还不太了解,无法弄清楚我需要使用哪种目录方法。我试过getFilesDir(),但它不喜欢Environment.DIRECTORY_PICTURES。
但他们没有提供使用他们的方法保存到画廊的方法。他们的代码 sn-p 不起作用
private void cameraIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
ex.printStackTrace();
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(getActivity().getApplicationContext(), "com.mycompany.myapp.fileprovider", photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_CODE_CAPUTURE_IMAGE);
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_CAPUTURE_IMAGE && resultCode == Activity.RESULT_OK) {
galleryAddPic();
}
}
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
所以我的应用确实不将图片保存到图库。我根本看不到它在哪里保存它。
有人知道我做错了什么吗?
【问题讨论】:
-
“他们使用的确切方法是什么” - 文档中的段落不正确。
getExternalFilesDir()对于您的应用是唯一的,但对于您的应用来说不是私有的。 “他们的代码 sn-p 不起作用”——尝试MediaScannerConnection及其scanFile()方法。 -
我试过了,但照片从未保存到我的相机胶卷中。
标签: java android android-fragments