2015-10-13 33 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