我无法将位图保存到Android中的图库吗? [英] I can't save my bitmap to the Gallery in Android?
问题描述
我正在尝试将编辑后的图像保存到用户可以从中访问的图像库中。
我从引用中使用了将照片添加到图库: https: //developer.android.com/training/camera/photobasics.html
I'm trying to save my edited image into Gallery from where user can access it. I have used "Add the photo to the Gallery" from reference: https://developer.android.com/training/camera/photobasics.html
这是我的代码:
String mCurrentPhotoPath;
public void startSave() {
FileOutputStream fileOutputStream = null;
File new_file = null;
try {
new_file = createImageFile();
fileOutputStream = new FileOutputStream(new_file);
imageView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(imageView.getDrawingCache());
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
Toast.makeText(this, "save Image Success", Toast.LENGTH_SHORT).show();
fileOutputStream.flush();
fileOutputStream.close();
}
catch (FileNotFoundException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
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 = image.getAbsolutePath();
return image;
}
我还更改了 AndroidManifest.xml
这样,并被授予写入外部存储的权限,并授予Uri权限。
I have also changed AndroidManifest.xml
like this and given permission to write in external storage and granted Uri permission.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackage">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mypackage.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
</application>
我已添加xml文件路径根据以下内容:
I have added xml file paths according to this:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images"
path="Android/data/com.sparkexel.blurd1/files/Pictures" />
</paths>
我无法理解这里存在的问题。首先,出现有关未授予外部权限或URI权限的错误消息。之后,我设置了URI权限。最后,控制台中没有错误消息。但是我无法将图片保存到图库中。我已经记录了保存文件的路径。它是:
I can't get my head around what is the problem here. First, there was an error message about external permission or URI permission not granted. I have set URI permission after that. Finally, There is no error message in console. But I can't save my image into the gallery. I have logged path of my saved file. and it is:
/storage/emulated/0/Android/data/com.mypackage/files/Pictures/JPEG_20171216_171109_2682421804312420480.jpg
/storage/emulated/0/Android/data/com.mypackage/files/Pictures/JPEG_20171216_171109_2682421804312420480.jpg
从那开始,我假设文件已成功保存。但是它不会显示在Gallery中。我尝试了许多解决方案,但没有任何效果。请帮助我。
From that, I assumed that file is saving successfully. But it does not show up in Gallery. I have tried many solutions but nothing works. Please help me.
推荐答案
仅在成功完成媒体扫描程序后,才会显示文件。
The File Will be Displayed only if after a Successful Completion of media Scanner.
-
将文件保存到外部存储中。现在,路径为Android数据文件夹。大多数情况下,媒体文件夹不会扫描数据文件夹中的文件。
为此,请使用Environment.getExternalStorageDirectory()。getAbsolutePath()+ / Pictures / Foldername /
作为文件夹路径
Environment.getExternalStorageDirectory()。getAbsolutePath()
将返回外部存储作为路径
Environment.getExternalStorageDirectory().getAbsolutePath()
will Return the ExternalStorge as the Path
也请参考以编程方式触发媒体扫描器
这篇关于我无法将位图保存到Android中的图库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!