从Android中的特定位置保存图像

时间:2017-07-25 12:06:57

标签: android

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

下载方法:

    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();
}

4 个答案:

答案 0 :(得分:1)

图片网址到位图

 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();
    }
    }

答案 1 :(得分:0)

下载文件后尝试此操作:

 // 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) {
            }

这将刷新您的图库。

答案 2 :(得分:0)

答案 3 :(得分:0)

您需要将图片uri转换为文件并将其保存在内部或外部存储中,将uri转换为此文件

File myFile = new File(uri.getPath());

要在本地保存文件,请参阅此链接 saving files in storage android

我希望它有所帮助。