【问题标题】:how to show image after store image into sd card on android如何在android上将图像存储到sd卡后显示图像
【发布时间】:2018-05-26 19:32:03
【问题描述】:

在我的应用程序中,我想从服务器下载图像并将其存储到 SD 卡
我写了下面的代码,我可以下载并存储在 SD 卡上。
但是将图像存储到 SD 卡后不会显示到图库中!

我的代码:

private void createDirectoryAndSaveFile(Bitmap imageToSave, String fileName) {
        File direct = new File(Environment.getExternalStorageDirectory() + "/MyApp/Wallpaper");

        if (!direct.exists()) {
            File wallpaperDirectory = new File("/sdcard/MyApp/Wallpaper/");
            wallpaperDirectory.mkdirs();
        }

        File file = new File(new File("/sdcard/MyApp/Wallpaper/"), fileName);
        if (file.exists()) {
            file.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(file);
            imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
            showReturnMessage("Message","Successfully saved :) ",R.color.alertSuccess);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

怎么可能?

【问题讨论】:

  • 你是 #12345 这个问题。您必须调用媒体扫描程序来告诉媒体存储您的新图像文件。 Google... 或者重新启动您的设备,然后查看 Gallery 是否知道您的文件。
  • how to show image after store image into sd card on android。当您的意思是它没有显示在画廊中时,这是一个错误的主题。
  • @greenapps,我知道重启手机时会显示图像,但我不重启手机。我想不用重启手机,你能帮我发个代码吗?请

标签: java android android-gallery


【解决方案1】:

每次保存图片时都需要刷新图库。保存后尝试调用此方法:

static public void notifyMediaScannerService(Context context, String path) {
    MediaScannerConnection.scanFile(context,
            new String[] { path }, null,
            new MediaScannerConnection.OnScanCompletedListener() {
        public void onScanCompleted(String path, Uri uri) {
            Log.i("ExternalStorage", "Scanned " + path + ":");
            Log.i("ExternalStorage", "-> uri=" + uri);
        }
    });
}

【讨论】:

  • 谢谢亲爱的,但是ContentValues(3); 有什么新功能?为什么你设置 3 ?不再为我工作:(
  • @BoboGoooool 值集的初始大小。您的 Android 版本是多少?
  • 安卓 7 。你能帮助我吗?请
  • 你能帮帮我吗?请
  • 如何使用新的更新代码?你能帮我解决我上面的代码吗?因为我是业余爱好者,真的需要你的帮助。请
猜你喜欢
  • 2011-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-07
  • 1970-01-01
  • 1970-01-01
  • 2011-06-20
  • 1970-01-01
相关资源
最近更新 更多