2015-10-13 32 views
0

我有,我會從一個基於文件夾的傳遞到AsyncMethod下載BLOB中的AsyncTask

因爲我已經做了一些改變,現在的圖像所在的數據庫上的鏈接下載圖像的方法。我在編輯我的downloadAsyn Task時遇到了一些問題,因爲它不再接收鏈接,而是一長串字符(來自數據庫的BLOB)。

我粘貼了下面的代碼,並試圖找到幫助將cImg1分配給位圖以顯示我的圖像。

謝謝

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 
     ImageView bmImage; 

     public DownloadImageTask(ImageView bmImage) { 
      this.bmImage = bmImage; 
     } 

     protected Bitmap doInBackground(String... urls) { 
      String urldisplay = urls[0];// this parameter once had url of image 

    //but now it has the image bitmap. 
      Bitmap cImg1= null; 
      try { 
      InputStream in = new java.net.URL(urldisplay).openStream(); 
      // cImg1= BitmapFactory.decodeStream(in); 
      cImg1=urldisplay;//Assign strings to BitMap? 
      } catch (Exception e) { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } 
      return cImg1; 
     } 

     protected void onPostExecute(Bitmap result) { 
      bmImage.setImageBitmap(result); 
     } 

    } 
+0

你看到的問題到底是什麼?你看到什麼錯誤? – JoxTraex

+0

@JoxTraex cImg1 = urldisplay;將位圖分配給字符串時出錯。但即使我將它從一個字符串更改爲bitap。我仍然不確定自己是否正確做到了 – Niana

回答

0

下面的代碼將正常工作。

public class DownloadImageTask extends AsyncTask<String, Integer, Bitmap> { 
    Context _context; 
    ImageView _imageView; 
    private OnResponseListener _responder; 
    private String _errorMessage; 

    public DownloadImageTask(ImageView bmImage, OnResponseListener responseListener) { 
     this._imageView = bmImage; 
     _context = bmImage.getContext(); 
     _responder = responseListener; 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 

    protected Bitmap doInBackground(String... urls) { 
     int count; 
     String urlDisplay = urls[0]; 
     Bitmap bitmap = null; 

     try { 
      InputStream in = new java.net.URL(urlDisplay).openStream(); 
      BitmapFactory.Options options = new BitmapFactory.Options(); options.inPurgeable = true; options.inInputShareable = true; 
      bitmap = BitmapFactory.decodeStream(in, null, options); 

      URLConnection urlConnection = new java.net.URL(urlDisplay).openConnection(); 
      urlConnection.connect(); 
      InputStream inputStream = urlConnection.getInputStream(); 
      int lengthOfFile = urlConnection.getContentLength(); 

      byte data[] = new byte[1024]; 
      long total = 0; 
      while ((count = inputStream.read(data)) != -1) { 
       total += count; 
       int progress = (int) total * 100/lengthOfFile; 
       publishProgress(progress); 
      } 
     } catch (Exception e) { 
      _errorMessage = e.getMessage(); 
      e.printStackTrace(); 
     } 
     return bitmap; 
    } 

    @Override 
    protected void onProgressUpdate(Integer... values) { 
     super.onProgressUpdate(values); 
    } 

    protected void onPostExecute(Bitmap result) { 
     if (result != null){ 
      _responder.onSuccess(result); 
     } 
     else 
      _responder.onFailure(_errorMessage); 
    } 

    public interface OnResponseListener { 
     void onSuccess(Bitmap result); 
     void onFailure(String message); 
    } 
} 
+0

我沒有測試過它,但它爲什麼要連接。就像我說的,我不需要連接。該位圖已經通過 – Niana

+0

@Niana //你的意思是圖像二進制接收爲Base64編碼_string_? – Youngjae

+0

是的,先生。那是正確的 – Niana