【问题标题】:Can't see the bitmap I saved to new directory看不到我保存到新目录的位图
【发布时间】:2014-07-27 13:58:50
【问题描述】:

我正在寻找一种将位图保存到文件夹(使用我的应用程序名称)的方法,这样默认画廊就会识别它。

所以我设法保存了图像,但我无法从图库或我的电脑(使用资源管理器)看到它

这是我的代码:

 // Save bitmap to internal memory
private void savePhoto(Bitmap bmp){
    String appName = "myApp";

    String file_path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ File.separator + appName;
    File dir = new File(file_path);

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

    // Image file
    File file = new File(dir, "IMG" + "_" +     System.currentTimeMillis() + ".jpg");
    FileOutputStream out = null;

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

     out = null;
    } catch (Exception e)
    {
        e.printStackTrace();
    }

}

我可以使用安装在我的银河系上的一些文件管理器来查看它,但它并没有太大帮助。 我知道我应该使用媒体扫描仪通知画廊(这是我需要跨越的另一座桥梁),但有人可以帮助我理解我什至找不到文件的方式......我应该以某种方式改变它的可见性吗?

还有一件事:我在另一个关于这个问题的问题上读到,我应该将元数据添加到图像/文件夹中,以便画廊显示它。有必要吗?

非常感谢!!!

【问题讨论】:

    标签: android file bitmap gallery


    【解决方案1】:

    显然它确实与媒体扫描仪有关,我添加了这一行

    MediaScannerConnection.scanFile(this, new String[] { file.getAbsolutePath() }, null, null);
    

    现在它可以工作了。

    来自文档:

    MediaScannerConnection 为应用程序提供了一种将新创建或下载的媒体文件传递给媒体扫描器服务的方法。媒体扫描服务将从文件中读取元数据并将文件添加到媒体内容提供者。

    【讨论】:

      【解决方案2】:

      尝试替换这个 getExternalStoragePublicDirectory(....) 有了这个 getExternalStorageDirectory();

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-10-11
        • 2012-08-13
        • 1970-01-01
        • 1970-01-01
        • 2015-07-04
        • 1970-01-01
        • 2011-05-14
        相关资源
        最近更新 更多