android - 我无法将我的位图保存到 Android 中的图库中?

标签 android file bitmap android-gallery android-mediascanner

我正在尝试将编辑后的图像保存到图库中,用户可以从中访问它。 我使用了引用文献中的“将照片添加到画廊”: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权限。

<?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文件路径:

<?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 权限。最后,控制台中没有错误消息。但是我无法将图像保存到图库中。我已经记录了我保存的文件的路径。它是:

/storage/emulated/0/Android/data/com.mypackage/files/Pictures/JPEG_20171216_171109_2682421804312420480.jpg

据此,我假设文件已成功保存。但它不会出现在图库中。我尝试了很多解决方案,但没有任何效果。请帮助我。

最佳答案

只有在媒体扫描器成功完成后才会显示该文件。

注意事项

  1. 将文件保存到外部存储器。现在路径是 Android 数据文件夹。大多数情况下,数据文件夹中的文件不会被媒体扫描仪扫描。 为此使用 Environment.getExternalStorageDirectory().getAbsolutePath()+"/Pictures/Foldername/" 作为文件夹路径

    Environment.getExternalStorageDirectory().getAbsolutePath() 将 ExternalStorge 作为路径返回

  2. Run Media Scanner

另请参阅 Trigger Media Scanner Programattically

关于android - 我无法将我的位图保存到 Android 中的图库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47845325/

相关文章:

android - 如何绕过 "Complete Action Using ..."?

java - 多个警报广播接收器android

java - 无法连接到 FTPS 服务器

java - 无法将中文字符写入文件名

java - 更改文件描述符偏移量

java - Android 上的 16 位 RAW RGB_565 图像

android - createBitmap 导致内存不足错误

java - 如何使用 REST Web 服务器维护登录状态?

android - album_info does not contain album_id 列错误

delphi - 我可以强制 Delphi 6 TImageList 位图将其透明像素绘制为某种颜色吗?