0

有没有办法可以通过 Gallery.class 更改 Main.class 类的 ImageView?

我的主类 onClick 调用 Gallery.class 的方法 setImage

public void setImage(int currentView) {

    Log.d("fgdsf","dumaan dito");
    this.currentView = currentView;
    Intent gallery = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(gallery, RESULT_LOAD_IMAGE);

}

这是 onActivityResult

@Override
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]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        LayoutInflater inflater = (LayoutInflater) mActivity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.activity_main, null);

        if (currentView == 0) {
            ImageView imageView = (ImageView) v
                    .findViewById(R.id.imageView1);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        } 
        if(currentView==1)
        {
            ImageView imageView = (ImageView) v
                    .findViewById(R.id.imageView2);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }

    }
}

我得到这个错误

08-07 08:35:08.208:E/AndroidRuntime(15689):引起:java.lang.NullPointerException 08-07 08:35:08.208:E/AndroidRuntime(15689):在 android.app.Activity.startActivityForResult(Activity .java:3370)

4

3 回答 3

0

我不认为你可以。您只能控制当前活动的布局及其组件。由于该组件不在当前活动的视图中(在您的情况下是库),因此访问它会引发 NullPointerException。您可以做的是,将您画廊上所选图像的路径传递给 Main 类,然后检索图像路径并将其设置为您的 ImageView。

于 2013-08-07T03:40:41.863 回答
0

我认为您最好的选择是在 Gallery.class 中设置一个界面,例如

public class Gallery extends Activity {

    public interface MyClickListener {
        public void OnClick();
    }

    private MyClickListener listener;

然后在您的 onClick 中,设置您的 OnClick ( listener.OnClick() )。在 Main.class 中,您必须导入“MyClickListener”并定义您要执行的任何操作。

于 2013-08-07T03:53:03.143 回答
0

用于捕获图像

这是从 android 4.0 开始发生的

在它返回图像 uri 路径之前,但现在它从画廊返回位图。

所以要解决这个问题。

File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".jpg");

Uri imgUri = Uri.fromFile(file);
String imagepath = file.getAbsolutePath();

final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imagepath);
startActivityForResult(intent, CAPTURE_IMAGE);


@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_CANCELED) {
            if (requestCode == CAPTURE_IMAGE) {
                imgUser.setImageBitmap(BitmapFactory.decodeFile(imagepath);
            } else {
                super.onActivityResult(requestCode, resultCode, data);
            }
        }

    }

从图库中挑选图片

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);


public String getAbsolutePath(Uri uri) {
        String[] projection = { MediaColumns.DATA };
        @SuppressWarnings("deprecation")
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_CANCELED) {
            if (requestCode == PICK_IMAGE) {

           imgUser.setImageBitmap(BitmapFactory.decodeFile(getAbsolutePath(data.getData()));
            }  else {
                super.onActivityResult(requestCode, resultCode, data);
            }
        }

    }
于 2013-08-07T11:40:45.393 回答