从内存

时间:2015-06-21 04:26:14

标签: java android listview bitmap

我有一个带有image的列表视图。我想更改onActivityResult上的列表项图像 图像捕获。我得到图像文件位图,但当我更改列表项图像时,列表视图图像不会改变。

final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
                    options);
            imageView1.setImageBitmap(bitmap);//there a bitmap exist---

            dataAdapter.taskList.get(0).setImgBitmap(bitmap);

//这不是改变列表图像。为什么???????                 dataAdapter.notifyDataSetChanged(); 这里imageview1有一个位图,我使用CustomAdapter类的ArrayList<Task> taskList参数将其设置为列表的第一项。

这里有完整的示例代码

NotifyAcitivity.java

package com.aci.notification;
public class NotifyActivity extends Activity {

    // Activity request codes
    private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
    public static final int MEDIA_TYPE_IMAGE = 1;

    // directory name to store captured images and videos
    private static final String IMAGE_DIRECTORY_NAME = "Hello Camera";

    private Uri fileUri; // file url to store image/video

    private ImageView imageView1;
    Bitmap bm, bm2;
    private Button btnCapturePicture;
    ArrayList<Task> taskList = new ArrayList<Task>();
    TextView tv;
    CustomAdapter dataAdapter = null;
    Button btnSave;
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notify);

        btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture);

        imageView1 = (ImageView) findViewById(R.id.imageView1);
        tv = (TextView) findViewById(R.id.textViewtest);

        btnSave = (Button) findViewById(R.id.button1);
        listView = (ListView) findViewById(R.id.listView1);

        new BitmapFactory();
        bm = BitmapFactory.decodeResource(getApplicationContext()
                .getResources(), R.drawable.ic_launcher);
        bm2 = BitmapFactory.decodeResource(getApplicationContext()
                .getResources(), R.drawable.bydefault);

        Task task;
        task = new Task(0, "This is task1", "/pictures/hello camera/", bm,false);
        taskList.add(task);
        task = new Task(1, "This is task2", "/pictures/hello camera/", bm,false);
        taskList.add(task);
        task = new Task(0, "This is task3", "/pictures/hello camera/", bm,false);
        taskList.add(task);
        task = new Task(0, "This is task4", "/pictures/hello camera/", bm2,false);
        taskList.add(task);
        task = new Task(0, "This is task5", "/pictures/hello camera/", bm,false);
        taskList.add(task);

        dataAdapter = new CustomAdapter(this, R.layout.list_style, taskList);

        listView.setAdapter(dataAdapter);

    }

    /**
     * Receiving activity result method will be called after closing the camera
     * */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // if the result is capturing Image
        if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                // successfully captured the image
                // display it in image view
                previewCapturedImage();
            } 
        }
    }

    /**
     * Display image from a path to ImageView
     */
    private void previewCapturedImage() {
        try {
            imageView1.setVisibility(View.VISIBLE);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 8;
            final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
                    options);
            imageView1.setImageBitmap(bitmap);

            dataAdapter.taskList.get(0).setImgBitmap(bitmap);
            dataAdapter.notifyDataSetChanged();

        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }

    public Uri getOutputMediaFileUri(int type) {
        return Uri.fromFile(getOutputMediaFile(type));
    }

}

CustomAdapter.java

public class CustomAdapter extends ArrayAdapter<Task> {
    public ArrayList<Task> taskList;

    Context context;
    public CustomAdapter(Context context, int resource, ArrayList<Task> taskList) {
        super(context, resource, taskList);
        this.context=context;
        // TODO Auto-generated constructor stub
        this.taskList = new ArrayList<Task>();
        this.taskList.addAll(taskList);
    }

    private class ViewHolder {
           CheckBox task;
           ImageView imageView;
           int task_id;
           Button imageCaptureBtnButton;
          }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder viewHolder = null;
        Log.v("ConvertView", String.valueOf(position));

        if (convertView == null) {
            LayoutInflater vi =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.list_style, null);

            viewHolder = new ViewHolder();

            viewHolder.task_id=taskList.get(position).getTask_id();//set task id of that task

            viewHolder.task = (CheckBox) convertView.findViewById(R.id.checkBox1);
            viewHolder.imageView=(ImageView) convertView.findViewById(R.id.imgPreview);
            viewHolder.imageCaptureBtnButton=(Button) convertView.findViewById(R.id.btnCapturePicture);

            viewHolder.imageView.setImageBitmap(taskList.get(position).getImgBitmap());

            convertView.setTag(viewHolder);

            viewHolder.task.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v;
                    Task aTask =(Task) cb.getTag();
                    Toast.makeText(context,
                            "Clicked on Checkbox: " + cb.getText() + " is "
                                    + cb.isChecked(), Toast.LENGTH_LONG).show();
                    aTask.setSelected(cb.isChecked());
                }
            });

            viewHolder.imageCaptureBtnButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(context, "dddd", Toast.LENGTH_LONG).show();

                }
            });
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Task aTask = taskList.get(position);
        viewHolder.task.setText(taskList.get(position).getTask());
        viewHolder.task.setChecked(taskList.get(position).isSelected());
        viewHolder.task.setTag(aTask);
        return convertView;
    }
    @Override
    public long getItemId(int arg0) {
        return 0;
    }
    @Override
    public int getCount() {
        return taskList.size();
    }
    @Override
    public Task getItem(int arg0) {
        return null;
    }

}

Task.java

public class Task {
    int task_id = 0;
     String task = null;
     String imageURL=null;
     Bitmap imgBitmap;
     boolean selected=false;

     public Task(int task_id, String task, String imageURL,Bitmap imgBitmap, boolean selected) {
      super();
      this.task_id = task_id;
      this.task= task;
      this.imageURL=imageURL;
      this.imgBitmap=imgBitmap;
      this.selected = selected;
     }
//all getters and setters here
}

我已经失去了我的日子。请帮助我。

1 个答案:

答案 0 :(得分:0)

很长一段时间后,我找到了自己的问题答案。 代码与这个问题并不完全匹配,因为答案来自我的另一个项目。我只是按照我成功的方式。

第1步:

当捕获图像时,我调用imagedata(位图位图)。它将每个位图转换为base_64字符串,此方法将新图像作为ByteDrawable类型的数组元素添加到列表中。

  

我再次创建新的适配器对象并将该适配器设置为   列表视图。

private void imagedata(Bitmap bitmap) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        // Must compress the Image to reduce image size to make upload easy
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte[] byte_arr = stream.toByteArray();
        // Encode Image to String
        image_data = Base64.encodeToString(byte_arr, 0);
        byteDrawableList.add(new ByteDrawable(image_data));

        listAdapter = new CustomAdapter(this);
        listView.setAdapter(listAdapter );
    }

    public class ByteDrawable {
        String imageAsBytes;
        public ByteDrawable(String imageAsBytes) {
            this.imageAsBytes = imageAsBytes;
        }
    }

第2步:

然后我从该arraylist填充我的列表,在Adapter类的构造函数中,我将base_64字符串转换为位图并将元素添加到本地arraylist items<Item>,然后在我使用的getview()方法中要创建列表视图的项目。

private class CustomAdapter extends ArrayAdapter<ByteDrawable>{
    public CustomAdapter(Context context, int resource, ArrayList<ByteDrawable > byteDrawableList) {
        super(context, resource, byteDrawableList);
        this.context=context;
        this.byteDrawableList= new ArrayList<ByteDrawable >();
        this.byteDrawableList.addAll(byteDrawableList);
            inflater = LayoutInflater.from(context);
            long k = 0;
            for (ByteDrawable bd : byteDrawableList) {
                String base = bd.getImageByteString();
                byte[] imageAsBytes = Base64.decode(base.getBytes(),
                        Base64.DEFAULT);

                items.add(new Item(k, BitmapFactory.decodeByteArray(
                        imageAsBytes, 0, imageAsBytes.length)));
                k++;
            }

    }
   private class Item {
        final long itmeid;
        final Bitmap drawabledata;

        Item(long id, Bitmap bitmap) {
            this.itmeid = id;
            this.drawabledata = bitmap;
        }
    }
    public View getView(final int position, View view, ViewGroup viewGroup) {
        /* follow normal way to set data to list item*/
    }
    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int i) {
        return items.get(i);
    }

    @Override
    public long getItemId(int i) {
        return items.get(i).itmeid;
    }
}

希望如果他们遇到同样的问题,这会有所帮助。