我在保存照片时出现问题,在应用程序目录中(如示例whatsapp)直接在我的应用程序上拍照时出现问题。应用程序拍摄的照片不保存在图库中
在我清单我已经设置这些许可:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.myapp.fileprovider"
android:exported="false"
android:grantUriPermissions="true"
android:readPermission="com.myapp.READ">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
</application>
在我的活动,我有:
public void showCamera(View v){
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
if (intent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.myapp.fileprovider",
photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(intent, REQUEST_TAKE_PHOTO);
galleryAddPic();
}
}
}
String mCurrentPhotoPath;
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);
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
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 = "file:" + image.getAbsolutePath();
return image;
}
在我的文件路径资源:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="Android/data/com.myapp/files/Pictures" />
它打开相机,拍摄照片并返回我的活动,但我无法在任何地方找到它,所以看起来不保存照片。
好吧,我把'galleryAddPic()'放在'onActivityResult'的Override上,但没有任何改变。 我有'CreateImageFile()'和'galleryAddPic()'我有'Uri contentUri = Uri.fromFile(photoFile);' 返回的文件可能是sendBroadcast的问题? – LorenzoBerti
@LorenzoBerti:“可能是sendBroadcast有问题吗?” - 使用'adb shell'来查看图像是否存在,你要求它被保存。如果完整的照片存在,那么媒体扫描逻辑会出现问题。如果文件存在,但是它是空的,那么图像捕获逻辑就会出错('createTempFile()'会创建一个空文件)。如果该文件不在那里,那么你的代码没有被运行。 – CommonsWare
Thankyou,我在Android/data/com.myapp/files /图片中看到照片,但重新启动后,但不在画廊中,如何刷新媒体并将其放入媒体库中? – LorenzoBerti