首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将图片从Android中的URL保存到特定位置

将图片从Android中的URL保存到特定位置
EN

Stack Overflow用户
提问于 2017-07-25 20:06:57
回答 4查看 5.4K关注 0票数 2

我正在创建一个可以从互联网上下载图片的应用程序!图像下载没有问题!但问题是,它们被下载到一个未知的文件夹中,并且这些下载的图像在图库应用程序中看不到!如何下载特定图库文件夹中的图像,使其在图库中也可见?提前感谢!

下载方法:

代码语言:javascript
运行
复制
    DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    Uri uri = Uri.parse(url);
    DownloadManager.Request request = new DownloadManager.Request(uri);
    request.setDescription("Downloading Wallpaper").setTitle("Downloading");
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    myDownloadReference = dm.enqueue(request);

    Toast.makeText(Wallpaper.this, "Downloading..", Toast.LENGTH_SHORT).show();
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-07-25 20:16:38

位图的图像url

代码语言:javascript
运行
复制
 try {
        URL url = new URL("http://....");
        Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
    } catch(IOException e) {
        System.out.println(e);
    } 




 public void saveImageToExternal(String imgName, Bitmap bm) throws IOException {
    //Create Path to save Image
    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES+appFolder); //Creates app specific folder
    path.mkdirs();
    File imageFile = new File(path, imgName+".png"); // Imagename.png
    FileOutputStream out = new FileOutputStream(imageFile);
    try{
        bm.compress(Bitmap.CompressFormat.PNG, 100, out); // Compress Image
        out.flush();
        out.close();

        // Tell the media scanner about the new file so that it is
        // immediately available to the user.
        MediaScannerConnection.scanFile(context,new String[] { imageFile.getAbsolutePath() }, null,new MediaScannerConnection.OnScanCompletedListener() {
            public void onScanCompleted(String path, Uri uri) {
                Log.i("ExternalStorage", "Scanned " + path + ":");
                Log.i("ExternalStorage", "-> uri=" + uri);
            }
        });
    } catch(Exception e) {
        throw new IOException();
    }
    }
票数 5
EN

Stack Overflow用户

发布于 2017-07-25 20:10:03

下载文件后尝试:

代码语言:javascript
运行
复制
 // refresh gallery
            try {
                MediaScannerConnection.scanFile(getActivity(), new String[]{savedImagePath}, null, new MediaScannerConnection.OnScanCompletedListener() {
                    @Override
                    public void onScanCompleted(String path, Uri uri) {
             //   ApplicationUtil.showToast(getActivity(), "onScanCompleted!");
                   }
               });
            } catch (Exception e) {
            }

这将刷新您的图库。

票数 0
EN

Stack Overflow用户

发布于 2017-07-25 20:31:23

请查看以下链接:

要设置下载文件的位置,请执行以下操作:

https://developer.android.com/reference/android/app/DownloadManager.Request.html#setDestinationUri(android.net.Uri)(https://developer.android.com/reference/android/app/DownloadManager.Request.html#setDestinationUri(android.net.Uri%29)

http://www.programcreek.com/java-api-examples/index.php?class=android.app.DownloadManager.Request&method=setDestinationUri

要使下载的文件可由MediaScanner扫描,请执行以下操作:

https://developer.android.com/reference/android/app/DownloadManager.Request.html#allowScanningByMediaScanner()(https://developer.android.com/reference/android/app/DownloadManager.Request.html#allowScanningByMediaScanner(%29)

https://www.codota.com/android/methods/android.app.DownloadManager.Request/allowScanningByMediaScanner

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45302805

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档