Dynamically change images in view mode in Android

I have a list view with an image. I want to change the image of a list item to onActivityResult

capture an image. I am getting a bitmap of an image file, but when I change the image of the list items, the image as a list does not change.

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

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

      

// this is not a change to the list image.why ??????? dataAdapter.notifyDataSetChanged (); here imageview1

has a bitmap and I am setting it to the first list item using the CustomAdapter

class parameter ArrayList<Task> taskList

.

here's a complete example code

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
}

      

I lost my days. Help me.

+3


source to share


1 answer


After a long time, I found my own question. The code doesn't fully match this question because the answer is from another project of mine. I just give how I did it.

Step 1:

When the image is captured, I call imagedata (Bitmap bitmap). It converts each bitmap to a base_64 string, and this method adds the new image to the list as an element of the ByteDrawable array.

I create a new adapter object again and set this adapter to the ListView.

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;
        }
    }

      



Step 2:

Then I populate my list from this arraylist and in the constructor of the Adapter class convert the string base_64

to bitmap and add the item to the local arraylist items<Item>

and then in the method getview()

I used the items to create the listview.

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;
    }
}

      

Hope this helps someone if they run into the same problem.

0


source







All Articles