Android Bitmap从其他类返回null

时间:2016-06-21 15:21:31

标签: android

我尝试创建一个获取图像并将其缓存在内存中的类,但它不会将值返回到我的列表适配器类。当我调试时,我可以在GetImagesBitmap中看到位图值,但在返回ListTestAdapter之后,该值为null。任何人都可以指导我。感谢。

这是我的GetImagesBitmap类,它将返回ListTestAdapter的值

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;

import java.net.URL;


public class GetImagesBitmap extends AsyncTask<Void, Void, Bitmap> {
    private final String TAG = "MY_MESSAGE";
    private Bitmap bitmap;
    private String url;

    public GetImagesBitmap() {
    }

    public GetImagesBitmap(String url) {
        this.url = url;
    }

    @Override
    protected Bitmap doInBackground(Void... params) {
        Bitmap bitmap1 = (Bitmap) ImageCache.getInstance().getLru().get(url);
        if (bitmap == null) {
            try {
                URL newurl = new URL(url);
                bitmap1 = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
                ImageCache.getInstance().getLru().put(url, bitmap1);
            } catch (Exception e) {
                Log.e(TAG, e.toString());
            }
        }

        return bitmap1;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap2) {
        this.bitmap = bitmap2;
    }

    public Bitmap getBitmap() {
        return bitmap;
    }
}

ListTestAdapter.java

package com.example.chaifs.test.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.example.chaifs.test.R;
import com.example.chaifs.test.controller.GetImagesBitmap;
import com.example.chaifs.test.model.*;

import java.util.List;

public class ListTestAdapter extends BaseAdapter {

    private final String TAG = "MY_MESSAGE";
    private List<Test> mItems;
    private LayoutInflater mInflater;

    public ListTestAdapter(Context context, List<Test> listTest) {
        this.setItems(listTest);
        this.mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return (getItems() != null && !getItems().isEmpty()) ? getItems().size() : 0;
    }

    @Override
    public Test getItem(int position) {
        return (getItems() != null && !getItems().isEmpty()) ? getItems().get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return (getItems() != null && !getItems().isEmpty()) ? getItems().get(position).getId() : position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final ViewHolder holder;
        View v = convertView;
        if (v == null) {
            v = mInflater.inflate(R.layout.list_test, parent, false);
            holder = new ViewHolder();
            holder.imgView = (ImageView) v.findViewById(R.id.imgView);
            holder.txtId = (TextView) v.findViewById(R.id.txt_id);
            holder.button1 = (ToggleButton) v.findViewById(R.id.tgl_status);
            holder.button1.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    ((ListView) parent).performItemClick(v, position, 0);
                }
            });
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }

        // fill row data
        Test currentItem = getItem(position);
        if (currentItem != null) {
            holder.txtId.setText(currentItem.getId().toString());
            holder.button1.setChecked(currentItem.getBookmarkStatus().equals("true") ? true : false);

            //this part which cannot get the bitmap return value
            GetImagesBitmap getImagesBitmap = new GetImagesBitmap(currentItem.getImageURL());
            getImagesBitmap.execute();
            Bitmap bitmap = getImagesBitmap.getBitmap();
            Log.e(TAG, "size of bitmap" + String.valueOf(bitmap.getByteCount()));
            holder.imgView.setImageBitmap(bitmap);

        }
        return v;
    }

    public List<Test> getItems() {
        return mItems;
    }

    public void setItems(List<Test> mItems) {
        this.mItems = mItems;
    }

    class ViewHolder {
        ImageView imgView;
        TextView txtId;
        ToggleButton button1;

    }

}

2 个答案:

答案 0 :(得分:0)

问题是你不是在等待AysncTask完成。这就是你拥有的东西

//this part which cannot get the bitmap return value
GetImagesBitmap getImagesBitmap = new GetImagesBitmap(currentItem.getImageURL());
getImagesBitmap.execute();
Bitmap bitmap = getImagesBitmap.getBitmap();

但是execute()调用是非阻塞的,所以它会立即返回,这意味着你的图像还没有被提取。

答案 1 :(得分:0)

您使用从服务器获取位图的方式稍有不妥。

        //this part which cannot get the bitmap return value
        GetImagesBitmap getImagesBitmap = new GetImagesBitmap(currentItem.getImageURL());
        getImagesBitmap.execute();
        Bitmap bitmap = getImagesBitmap.getBitmap();
        Log.e(TAG, "size of bitmap" + String.valueOf(bitmap.getByteCount()));
        holder.imgView.setImageBitmap(bitmap);

好的,你正在进行网络调用以从服务器获取位图,在下一行中你要求的是位图。 Asynchtask在不同的线程上运行,因此最好使用回调,这样无论何时下载完成,它都会通过回调通知你。