2017-07-25 10 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
+0

質問のコードには「ビットマップ」はありません。 – CommonsWare

+0

@CommonsWareは画像urlをbitmapに変換します。編集した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) { 
      } 

これはあなたのギャラリーを更新します。

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

"savedImagePath"にフォルダのパスを入れて、フォルダ全体がリフレッシュされ、そのフォルダ内のすべてのイメージがギャラリーに表示されるようにすることはできますか? –

+0

フォルダのパスを入れる必要はありません。これによりギャラリーが更新されます。したがって、すべての画像に適用されます –

0

をビットマップに

0

は、あなたがこの

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

ようなファイルへのURIを変換し、ファイルおよび内部または外部記憶装置に保存した画像のURIを変換する必要がありますローカルにファイルを保存する場合は、このリンクを参照してください。 saving files in storage android

私はそれが助けて欲しい。

関連する問題