【问题标题】:Saved photos are invisible for Gallery已保存的照片对图库不可见
【发布时间】:2013-12-12 11:11:22
【问题描述】:

我的应用程序将照片保存到Gallery,但Gallery 看不到这些照片。我使用文件管理器来检查现有的,它们就在那里!我可以从文件管理器打开它们,但不是Gallery

我用来保存图片的代码:

public static void saveImg(long pid, ImageView picture, Context context) {
        File file = new File(Environment.getExternalStorageDirectory() + "/Lackrosy/"  + pid + ".jpeg");
        File dir = new File(Environment.getExternalStorageDirectory() + "/Lackrosy/");
        try {
            if (!dir.exists())
                dir.mkdir();
            picture.buildDrawingCache();
            Bitmap bmap = picture.getDrawingCache();
            FileOutputStream out = new FileOutputStream(file);
            bmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            Log.d("palval", "Photo saved: " + file.getAbsolutePath());
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("palval", e.toString());
            file.delete();
            Toast.makeText(context, "Image not saved", Toast.LENGTH_SHORT).show();
        }
    }

【问题讨论】:

    标签: android image save visibility android-gallery


    【解决方案1】:

    您需要发送广播,以便画廊自行更新。

            Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
            File f = new File(image_file_path);
            Uri contentUri = Uri.fromFile(f);
            mediaScanIntent.setData(contentUri);
            sendBroadcast(mediaScanIntent);
    

    编辑 1:您拥有一切:http://developer.android.com/training/camera/photobasics.html

    您可以下载该示例,它是我发现的唯一代码,适用于所有带照片的手机,存储在我想要的位置并显示在图库中...

    但有些手机会在画廊中出现重复,例如 HTC 渴望 C...

    【讨论】:

    • 我需要以前的代码来保存图像还是直接替换它?
    • 你说你的代码通过将图像保存到正确的位置来工作,所以它应该可以正常工作。保存文件后添加广播即可。
    • 这篇文章可能会更有帮助。 androidyue.github.io/blog/2014/01/19/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 2020-11-28
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多