Imageviewer更新不更改图标

时间:2014-12-22 05:53:17

标签: android imageview

所以这里我的代码在MainActivity中,当点击它是一个ImageView的appliance_icon时,它会引导我到我的画廊,我可以选择图片。

 final Dialog addingDialog = new Dialog(MainActivity.this);
 addingDialog.setTitle("Adding New Appliance");
 addingDialog.setContentView(R.layout.adding_appliance);

 final Button done = (Button)addingDialog.findViewById(R.id.done);
 final Button appliance_color = (Button)addingDialog.findViewById(R.id.appliance_color);
 final ImageView appliance_icon = (ImageView)addingDialog.findViewById(R.id.appliance_icon);



 appliance_icon.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                // TODO Auto-generated method stub
                                Intent i = new Intent(
                                        Intent.ACTION_PICK,
                                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                                startActivityForResult(i, RESULT_LOAD_IMAGE);
                                appliance_icon.setImageBitmap(BitmapFactory.decodeFile(picturePath));          

                            }                               
                        });

我的问题是,无论何时我选择新的图片,它都不会更新我的图像视图,而且图像视图只会在第二次按下时更新。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            picturePath = cursor.getString(columnIndex);
            cursor.close();


        }
    }

3 个答案:

答案 0 :(得分:1)

你能在onActivityResult方法中设置图像吗?

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        picturePath = cursor.getString(columnIndex);
        cursor.close();

        updatePicture();

    }
}


public void updatePicture() {

      appliance_icon.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 

}

答案 1 :(得分:1)

在课程开始前声明图像视图,

ImageView appliance_icon;

在click(),

中的imageview中使用它
 appliance_icon = (ImageView) findViewById(R.id.xxxx);
     appliance_icon .setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent,100);
        }
        });

在onActivityResult中使用它,

@Override
    public void onActivityResult(int requestCode, int resultCode, final Intent data) {
    if (requestCode == 100 && resultCode == RESULT_OK && data != null) {

                new AsyncTask<Void, Void, Void>() {     
                Bitmap bmImg=null;
                @Override
                protected Void doInBackground(Void... params) {
                    final Uri selectedImage = data.getData();
                    final Intent intent = new Intent("com.android.camera.action.CROP");
                    intent.setDataAndType(selectedImage, "image/*");
                    String[] filePathColumn = { MediaStore.Images.Media.DATA };

                    Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
                    if (cursor != null) {
                        cursor.moveToFirst();
                        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);

                        bmImg = BitmapFactory.decodeFile(cursor.getString(columnIndex));

                        cursor.close();
                    }
                    return null;
                }

                @Override
                protected void onPostExecute(Void result) {

                    appliance_icon .setImageBitmap(bmImg);


                }

            }.execute();


        }
}

答案 2 :(得分:0)

你可能想看看这个问题,它可能会满足你的需要。 get selected image from gallery into imageview