2017-07-25 57 views
0

我正在創建一個應用程序,可以從互聯網下載圖像!圖像下載沒有問題!但問題是,他們被下載到一個未知的文件夾中,並且下載的圖片在圖庫應用程序中看不到! 如何下載特定圖庫文件夾中的圖像,以便在圖庫中可見? 在此先感謝!從Android中的特定位置的URL保存圖像

下載方法:

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

回答

0

圖像網址爲位圖

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

問題中的代碼沒有「位圖」。 – CommonsWare

+0

@CommonsWare將圖像url轉換爲bitmap.check編輯後的ans – susaine

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

這將刷新您的畫廊。

+0

我可以將文件夾的路徑放在「savedImagePath」中,這樣​​整個文件夾就會刷新,並且該文件夾中的所有圖像都出現在圖庫中? –

+0

無需放置文件夾的路徑。這將刷新您的畫廊。所以它適用於所有圖像 –

0
0

你需要轉換圖像的URI到文件並將其保存在內部或外部存儲,轉換的URI到文件這樣

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

在本地保存文件請參考此鏈接 saving files in storage android

我希望它有幫助。