【问题标题】:downloaded image unable to view in gallery下载的图片无法在图库中查看
【发布时间】:2016-06-23 14:37:27
【问题描述】:

我正在尝试下载图像并将其保存到 app 文件夹,如下所示,一切正常。

  @Override
    protected String doInBackground(String... aurl) {
        int count;

        try {

            URL url = new URL(aurl[0]);
            URLConnection conexion = url.openConnection();
            conexion.connect();

            int lenghtOfFile = conexion.getContentLength();
            Log.d("ANDRO_ASYNC", "Length of file: " + lenghtOfFile);

            InputStream input = new BufferedInputStream(url.openStream());
            storagepath = new File(Environment.getExternalStorageDirectory()
                    + "/DownloadedImages");

            if (!storagepath.exists()) {
                storagepath.mkdirs();
            }
            OutputStream output = new FileOutputStream(storagepath + "/" + filewithoutextension + ".bmp");
            Intent intent =
                    new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            intent.setData(Uri.fromFile(storagepath));
            sendBroadcast(intent);

            byte data[] = new byte[1024];

            long total = 0;

            while ((count = input.read(data)) != -1) {
                total += count;
                publishProgress(""+(int)((total*100)/lenghtOfFile));
                output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();
            Savetogallery(storagepath);
        } catch (Exception e) {}
        return null;

    }

现在我的问题是下载的图像,当我使用意图打开时,特别是文件夹中没有显示在画廊中,如下所示:

btnOpenpath.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Build.VERSION.SDK_INT <= 19) {
                    Intent i = new Intent();
                    i.setType("image/*");
                    i.setAction(Intent.ACTION_GET_CONTENT);
                    i.addCategory(Intent.CATEGORY_OPENABLE);
                    startActivityForResult(i, 10);
                } else if (Build.VERSION.SDK_INT > 19) {
                    Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(intent, 10);
                }

            }
 });

如果我使用如下所示的下载管理器,那么它会显示文件夹名称。

    public void downloadFile(String uRl) {
    File direct = new File(Environment.getExternalStorageDirectory()
            + "/SampleFolder");

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

    DownloadManager mgr = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);

    Uri downloadUri = Uri.parse(uRl);
    DownloadManager.Request request = new DownloadManager.Request(
            downloadUri);

    request.setAllowedNetworkTypes(
            DownloadManager.Request.NETWORK_WIFI
                    | DownloadManager.Request.NETWORK_MOBILE)
            .setAllowedOverRoaming(false).setTitle("Demo")
            .setDescription("Something useful. No, really.")
            .setDestinationInExternalPublicDir("/SampleFolder", "fileName.jpg");

    mgr.enqueue(request);
}

【问题讨论】:

  • @downvoter-想发表评论吗?您是否了解我的问题或您不了解的内容,请告诉我将更新我的问题
  • 嗨。您的错误是画廊屏幕不显示您存储在文件夹中的下载图像?
  • @GiapLee- 是的..那么我如何使用图库屏幕查看下载的图像
  • 是的,我明白了,所以我为你投了赞成票。等我支持。

标签: android url download gallery android-download-manager


【解决方案1】:

阅读并尝试以下示例代码:

注意:我使用 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE 通知操作系统扫描并更新最新文件

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
        Intent imageScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File newestFile = new File("file://"+ Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)); //you can replace with particular folder here
        Uri contentUri = Uri.fromFile(newestFile );
        imageScanIntent.setData(contentUri);
        mycontext.sendBroadcast(imageScanIntent);
}
else
{
        mycontext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,    Uri.parse("file://" + Environment.getExternalStorageDirectory()))); //you can replace or add/concat particular folder which you download image into
}

您可以参考更多here

【讨论】:

  • @GiapLee- 这里的 mediaScanIntent 是什么?
  • 而不是文件夹,如果您直接传递下载的文件,它会更快地扫描并显示在图库中。
猜你喜欢
  • 1970-01-01
  • 2011-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-13
  • 1970-01-01
  • 2021-04-21
相关资源
最近更新 更多