2016-08-14 7 views
3

AsyncTaskで画像をダウンロードして、ImageViewに表示したい場合は正常に処理できますが、 SDカードにファイルを保存しなくてもすべてを実行できます。AsyncTask経由で画像をダウンロードし、SDカードに保存せずにImageviewに表示

これまで私がこれまで行ってきたことは次のとおりです。

class DownloadFileFromURL extends AsyncTask<String, String, String> { 

/** 
* Before starting background thread 
* Show Progress Bar Dialog 
* */ 
@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    showDialog(progress_bar_type); 
} 

/** 
* Downloading file in background thread 
* */ 
@Override 
protected String doInBackground(String... f_url) { 
    int count; 
    try { 
     URL url = new URL(f_url[0]); 
     URLConnection conection = url.openConnection(); 
     conection.connect(); 
     // getting file length 
     int lenghtOfFile = conection.getContentLength(); 

     // input stream to read file - with 8k buffer 
     InputStream input = new BufferedInputStream(url.openStream(), 8192); 

     // Output stream to write file 
     OutputStream output = new FileOutputStream("/sdcard/downloadedfile.jpg"); 

     byte data[] = new byte[1024]; 

     long total = 0; 

     while ((count = input.read(data)) != -1) { 
      total += count; 
      // publishing the progress.... 
      // After this onProgressUpdate will be called 
      publishProgress(""+(int)((total*100)/lenghtOfFile)); 

      // writing data to file 
      output.write(data, 0, count); 
     } 

     // flushing output 
     output.flush(); 

     // closing streams 
     output.close(); 
     input.close(); 

    } catch (Exception e) { 
     Log.e("Error: ", e.getMessage()); 
    } 

    return null; 
} 

/** 
* Updating progress bar 
* */ 
protected void onProgressUpdate(String... progress) { 
    // setting progress percentage 
    pDialog.setProgress(Integer.parseInt(progress[0])); 
} 

/** 
* After completing background task 
* Dismiss the progress dialog 
* **/ 
@Override 
protected void onPostExecute(String file_url) { 
    // dismiss the dialog after the file was downloaded 
    dismissDialog(progress_bar_type); 

    // Displaying downloaded image into image view 
    // Reading image path from sdcard 
    String imagePath = Environment.getExternalStorageDirectory().toString() + "/downloadedfile.jpg"; 
    // setting downloaded into image view 
    my_image.setImageDrawable(Drawable.createFromPath(imagePath)); 
} 

} 
+0

あなたのコードで何が問題なのか説明できますか? –

+0

@ L.Swifterダウンロードしたイメージをローカルに保存すると、正常に動作しますが、代わりにビットマップを使用すると、プログレスバーは正常に動作しますが、イメージは表示されません。 – Ezio

+0

'ByteArrayOutputStream'と' BitmapFactory.decodeByteArray'あなたの目的。 –

答えて

1

をローカルにイメージをダウンロードしたくない場合は、あなたがByteArrayOutputStream代わりのFileOutputStreamを使用する必要があります。

そして、これはキーコードです:

ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 

byte data[] = new byte[1024]; 
long total = 0; 
while ((count = input.read(data)) != -1) { 
    total += count; 
    publishProgress(""+(int)((total*100)/lenghtOfFile)); 

    outputStream.write(data, 0, count); 
} 

//after downloading the image 
byte[] imageData = outputStream.toByteArray(); 
Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length); 
my_image.setImageBitmap(bitmap); 

私はそれをテストしていないが、私は、これはあなたを助けることができると信じています。

+0

あまりにも魅力的に働いてくれてありがとうございます(評判の欠如のためにupvoteできません:) – Ezio

+0

:) –

0

代わりGlideを使用することができます。

Glide.with(this).load("http://server.com/image.jpg").into(imageView); 
+0

私たちはこのライブラリについて知っていただきありがとうございます – Ezio

0

参照Best method to download image from url in Android

private Bitmap downloadBitmap(String url) { 
     HttpURLConnection urlConnection = null; 
     try { 
      URL uri = new URL(url); 
      urlConnection = (HttpURLConnection) uri.openConnection(); 

      int statusCode = urlConnection.getResponseCode(); 
      if (statusCode != HttpStatus.SC_OK) { 
       return null; 
      } 

      InputStream inputStream = urlConnection.getInputStream(); 
      if (inputStream != null) { 

       Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 
       return bitmap; 
      } 
     } catch (Exception e) { 
      Log.d("URLCONNECTIONERROR", e.toString()); 
      if (urlConnection != null) { 
       urlConnection.disconnect(); 
      } 
      Log.w("ImageDownloader", "Error downloading image from " + url); 
     } finally { 
      if (urlConnection != null) { 
       urlConnection.disconnect(); 

      } 
     } 
     return null; 
    } 
+0

私は同じことをしていますが、私はまた、ユーザーに進捗状況を表示する必要があり、そこに私は問題に直面しています。 – Ezio

+0

createTempFileを使用するか、外部/内部のディレクトリにファイルを書き込んでください – Ramit

関連する問題