我有一个活动,它有一个按钮。当我点击按钮时,它会将我重定向到图库。我想使用图像视图在下一个活动中显示所选图像。但它没有显示图像。设置图像后,视图不在屏幕上。
下面给出了我选择图像和下一步的代码。我在活动中没有使用历史真实。
@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();
if (!(picturePath.equals(""))) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, ImageInGellary.class);
intent.putExtra("picturePath", picturePath);
startActivity(intent);
}
}
}
public class ImageInGellary extends Activity {
Button cancel;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.load_image);
cancel = (Button) findViewById(R.id.buttonCancelPicture);
Intent in = getIntent();
savedInstanceState = in.getExtras();
String picturePath = savedInstanceState.getString("picturePath");
ImageView imageView = (ImageView) findViewById(R.id.img_view);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
/*
* Intent i = new Intent(Intent.ACTION_PICK,
* android.provider.MediaStore
* .Images.Media.EXTERNAL_CONTENT_URI);
*
* startActivityForResult(i, RESULT_LOAD_IMAGE);
*/
Intent intent = new Intent();
intent.setClass(ImageInGellary.this, MainActivity.class);
startActivity(intent);
}
});
}
}
答案 0 :(得分:19)
public class ImageGalleryDemoActivity extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
@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();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
答案 1 :(得分:8)
Picasso.with(MainActivity.this).load(data.getData()).noPlaceholder().centerCrop().fit().into((ImageView) findViewById(R.id.imageView1));
完整解决方案: -
选择图片
public void pickImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
}
加载图片
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
//Get ImageURi and load with help of picasso
//Uri selectedImageURI = data.getData();
Picasso.with(MainActivity1.this).load(data.getData()).noPlaceholder().centerCrop().fit()
.into((ImageView) findViewById(R.id.imageView1));
}
}
}
在您的情况下,您需要将ImageURI传递给下一个活动
Uri selectedImageURI = data.getData();
答案 2 :(得分:5)
public void onClick(View view) {
Intent gallery = new Intent(Intent.ACTION_GET_CONTENT);
gallery.setType("image/*");
startActivityForResult(gallery, RESULT_LOAD_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK){
Uri imageUri = data.getData();
imgView.setImageURI(imageUri);
}
}
答案 3 :(得分:2)
如果无效,以下代码将起作用:
triggersEnter
答案 4 :(得分:1)
我已经解决了你的问题。拾取图像后,将图像Uri转换为String,在第二个类上,将字符串转换回图像。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case SELECT_PHOTO:
if(resultCode == RESULT_OK){
Uri selectedImage = data.getData();
if(selectedImage !=null){
Intent intent=new Intent(MainActivity.this, Activity2.class);
Bundle extras = new Bundle();
extras.putString("image", selectedImage.toString());
intent.putExtras(extras);
startActivity(intent);
}
}
}
<强> Activity2.class 强>
Bundle extras = getIntent().getExtras();
String name = extras.getString("image");
Uri imageFinal=Uri.parse(name);