2015-06-21 95 views
3

我有一個列表視圖圖像。我想更改列表項目圖像onActivityResult 圖像捕獲。我得到的圖像文件位圖,但當我更改列表項目圖像的列表視圖圖像不會改變。動態改變從內存Android的列表視圖圖像

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

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

//這是不會改變列表image.why ??????? 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 
} 

我已經失去了我的日子。請幫助我。

回答

0

經過很長時間我找到了自己的問題答案。 代碼與這個問題不完全匹配,因爲答案來自我的另一個項目。我只是給我成功的方式。

步驟1:

當圖像被捕獲我請圖象 - (位圖的位圖)。它的每個位圖轉換爲base_64字符串和該方法中增加了新的圖像的列表類型ByteDrawable的數組元素。

我再次創建新的適配器對象並將該適配器設置爲 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; 
     } 
    } 

第2步:

然後我填充我的名單從ArrayList和適配器類的構造函數我轉換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; 
    } 
} 

希望這將有助於某人,如果他們面臨同樣的問題。