【问题标题】:Bitmap saved in internal storage doesn't show on Gallery保存在内部存储中的位图不显示在图库中
【发布时间】:2015-11-09 23:19:31
【问题描述】:

我创建了一个简单的应用程序来将位图保存在内部存储中,并且能够像任何其他图像一样在 android 库中显示该位图。

问题是:保存后它没有显示在画廊中......而且我不知道我做错了什么。 (我在模拟器上测试)

这是我处理位图保存的类。当我单击按钮保存位图时,此 Saver 类在我的主要活动中使用。

public class Saver {

private Context mContext;
private String mNameOfFolder = "Storager";
private String mNameOfFile = "Quote";

public void saveImage(Context context, Bitmap imageToSave) {
    mContext = context;

    // Create the directory
    File dir = mContext.getDir(mNameOfFolder, Context.MODE_PRIVATE);
    String filePath = dir.getAbsolutePath();

    // Get the current date and time to attach to the name of the image
    String currentDateAndTime = getCurrentDateAndTime();

    if(!dir.exists()) {
        dir.mkdirs();
    }

    File file = new File(dir, mNameOfFile + currentDateAndTime + ".jpg");

    try {
        FileOutputStream out = new FileOutputStream(file);
        imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);

        out.flush();
        out.close();

        galleryAddPic(file);

        successSave();
    } catch (FileNotFoundException e) {
        Log.d("d", "file not found");
        unableToSave();
    } catch (IOException e) {
        Log.d("d", "IO Exception");
        unableToSave();
    }
}

private void galleryAddPic(File file) {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    Uri contentUri = Uri.fromFile(file);
    mediaScanIntent.setData(contentUri);
    mContext.sendBroadcast(mediaScanIntent);
}

private String getCurrentDateAndTime() {
    Calendar calendar = Calendar.getInstance();
    // Setting format of the time
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
    // Getting the formated date as a string
    String formattedDate = dateFormat.format(calendar.getTime());

    return formattedDate;
}

}

我什至添加了 android 开发者网站here 提供的方法 galleryAddPic(),以便能够将图像添加到 Media Provider 的数据库中,使其在 Android Gallery 应用程序和其他应用程序中可用。

【问题讨论】:

    标签: java android bitmap android-gallery


    【解决方案1】:

    第三方应用(包括MediaStore)无法访问您应用的internal storage。如果您希望第三方应用可以访问该文件,请使用external storage

    另外,我不知道你为什么要在这里创建ContextWrapper。如果您有需要 Context 的方法(例如,当前代码中的 getDir()),请在传递给您的方法的 Context 上调用它们。

    【讨论】:

    • 编辑了 ContextWrapper 问题...(我一定在编码时一直在睡觉...)所以我读到的外部存储不仅仅是 sd 卡吗?
    • @Cap.Alvez:External storage 很少是 removable storage。在绝大多数 Android 设备上,外部存储是板载闪存的一部分,内部存储也是如此。区别在于外部存储是公共的(具有适当的权限),而内部存储是私有的。
    • 我现在有点困惑。我应该使用什么作为保存外部存储的路径?我想用应用程序的名称创建一个文件夹,里面有位图,但仍然可以在默认的 android 库中看到它们。我应该使用例如 Environment.getExternalStorageDirectory() 吗?因为如果我使用它指向“/storage/sdcard”,它会给我fileNotFoundException。
    • @Cap.Alvez:“我应该使用什么作为保存外部存储的路径?” -- 这在the blog post that I linked to 中有介绍。 “因为如果我使用它指向“/storage/sdcard”并且它给了我fileNotFoundException”——我不知道“它”是什么。如果您在模拟器上进行测试,请确保您的 AVD 配置具有外部存储。
    • 发现问题:我的 AVD 没有正确配置外部存储。谢谢,不错的博客顺便说一句。
    猜你喜欢
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 2015-06-14
    相关资源
    最近更新 更多