在单独的类中将图像设置为下载的位图

时间:2014-03-17 20:28:02

标签: android bitmap

我正在尝试将imageview传递给非活动类,该类将处理从在线查找image,然后再设置图像。我收到的错误是,在尝试设置image时,bitmap即使已收到,也会被视为空。

我的构造函数

ImageView iview_image;
public ImageLoader(String url, Context newContext, ItemsDto items, ImageView imageView)
{
    try
    {
        Log.e("IMGURL",url);
        str_img=url;
        context=newContext;
        this.context=newContext;
        itemsDto=items;
        iview_image=imageView;
        iview_image.findViewById(R.id.icon);
    }

我的异步任务

protected Void doInBackground(Void... arg0) 
{
    try 
    {
        Log.e("Async Img Load", "Before the try");
        //Sets image status to help other classes know when image is ready

        //image lookups
        URL url = new URL(str_img);
        HttpGet httpRequest = null;
        httpRequest = new HttpGet(url.toURI());

        //http get 
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
        HttpEntity entity = response.getEntity();
        BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
        InputStream input = b_entity.getContent();
        bm_productImage = BitmapFactory.decodeStream(input);

        setBm_productImage(bm_productImage);

        str_imgStatus="Image recieved";
    } 
    catch (Exception e) 
    {
        Log.e("IMG_ERROR",e.toString());
    }       
    try{iview_image.setImageBitmap(getBm_productImage());}
    catch(Exception e){ Log.e("more errors wheee", e.toString());}
    doPendingActivity();
    return null;
}

@Override
protected void onPostExecute(Void result) 
{
    // TODO Auto-generated method stub
    super.onPostExecute(result);

    try
    {

    }
    catch(Exception e)
    {
        Log.e("Post_ERROR", e.toString());
    }
}

1 个答案:

答案 0 :(得分:0)

我一直在你身边,我会给你一个推荐。但是,首先,我会回答你的问题。我从网址下载流并将其保存到Bitmap的方式是:

try {
     URL url_obj = new URL(url);
     Bitmap imageToReturn =  BitmapFactory.decodeStream(url_obj.openConnection().getInputStream());
     return imageToReturn; // or do whatever you want
 } catch (MalformedURLException e) {
     return null;
 } catch (SocketTimeoutException e) {
     return null;
 } catch (IOException e) {
     return null;
 }

然而,在处理大量图像时,这种解决方案远非理想。 Android不能很好地处理内存中的Bitmap,并且在内存中分配了大量Bitmap之后,在你面前抛出OutOfMemoryError异常是一个时间问题。

我建议使用像Picasso(http://square.github.io/picasso/)这样的图像下载/缓存库。您可以下载JAR文件并将其包含在您的项目中(它真的就是这么简单!)。默认使用方案是:

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

但这不是唯一的可能性。如果你想保存位图或用它做其他事情,你可以这样做:

Picasso.with(context).load(url).into(new Target() {
     @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
         // do what you want to do with the bitmap
         // if it has already been downloaded, it will be loaded from
         // cache (memory or disk). The loaded bitmap is in the parameter
    }

    @Override
    public void onBitmapFailed(Drawable drawable) {
        // whatever you want to do if if could not load the image
    }

    @Override
    public void onPrepareLoad(Drawable drawable) {
        // do whatever you want to do right before your request is submitted
    }
});

如果需要,任何方法都可以为空,根据需要使用它(所有方法都需要在那里,或者接口实现会给你一个编译错误)。

我认为你会发现这个解决方案会变得更好,并且当你开始耗尽内存时它将会让你头疼。我希望它很有用!

相关问题