有没有办法可以通过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)
答案 0 :(得分:0)
我认为你不能。您只能控制当前活动的布局及其组件。由于组件不在您当前活动的视图中(在您的情况下是Gallery),因此访问它将抛出NullPointerException。你可以做的是,将Gallery上所选图像的路径传递给Main类,然后检索图像路径并将其设置为ImageView。
答案 1 :(得分:0)
我认为你最好的选择是在Gallery.class中设置一个界面,比如
public class Gallery extends Activity {
public interface MyClickListener {
public void OnClick();
}
private MyClickListener listener;
然后在你的onClick中,设置你的OnClick(listener.OnClick())。在Main.class中,您必须导入“MyClickListener”并定义您想要执行的任何操作。
答案 2 :(得分:0)
用于捕获图像
这是从Android 4.0开始发生的
在返回图像uri路径之前,现在它从图库中返回Bitmap。
所以要解决这个问题。
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);
}
}
}
从Gallary中选择图片
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);
}
}
}