英文:
ImageView doesn't show image from intent in java android
问题
我尝试了几种不同的方式来在imageView中显示从图库中选择的图像。在MainActivity中,我让用户从图库中选择图像,并在MainActivity中使用意图(Intent)将图像的 URI 发送到NextActivity,并将 URI 设置为imageView,但这并没有起作用。我看不到图像。在调试模式下,我可以在我设置为imageView的变量中看到 URI,但它不显示图像。
我的代码
MainActivity.java 的 onCreate 方法
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
imageUri = data.getData();
Intent intent = new Intent(this, NextActivity.class);
if(imageUri != null){
intent.putExtra("imageUri", imageUri.toString());
startActivity(intent);
}
}
NextActivity.java 的 onCreate 方法
ImageView imgView = (ImageView) findViewById(R.id.imageView1);
Intent intent = getIntent();
String image_path = intent.getStringExtra("imageUri");
Uri fileUri = Uri.parse(image_path);
imgView.setImageURI(fileUri);
我尝试过将 Uri 以意图的形式发送,转换为字符串并重新转换等等。我不知道为什么会出现这种情况。
NextActivity 的 XML
<ImageView
android:id="@+id/imageView1"
android:layout_width="396dp"
android:layout_height="450dp"
android:adjustViewBounds="true"
android:contentDescription="@string/app_name"
android:scaleType="fitXY"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.466"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
英文:
I've tried a couple different ways to show a chosen image from the gallery in a imageView. In the MainActivity I let the user choose an Image from Gallery and use an intent in the MainActivity, to send the uri of the image to the NextActivity and set the uri to the imageView, but it's not working. I can't see the Image. In the debug mode I see the uri in the variable which I set to the ImageView but it doesn't show the image.
My code
MainActivity.java onCreate method
<!-- language: java -->
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
imageUri = data.getData();
Intent intent = new Intent (this, NextActivity.class );
if(imageUri != null){
intent.putExtra("imageUri", imageUri.toString());
startActivity(intent);
}
}
NextActivity.java onCreate method
<!-- language: java -->
ImageView imgView = (ImageView) findViewById(R.id.imageView1);
Intent intent = getIntent();
String image_path = intent.getStringExtra("imageUri");
Uri fileUri = Uri.parse(image_path);
imgView.setImageURI(fileUri);
I've tried to send the Uri as it is with intent, converted as a String and reconverted and so on. I don't know.
NextActivity XML
<!-- language: xml -->
<ImageView
android:id="@+id/imageView1"
android:layout_width="396dp"
android:layout_height="450dp"
android:adjustViewBounds="true"
android:contentDescription="@string/app_name"
android:scaleType="fitXY"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.466"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
答案1
得分: 0
尝试一下:
Uri fileUri = Uri.parse(image_path);
imgView.setImageURI(null);
imgView.setImageURI(fileUri);
英文:
Try this:
Uri fileUri = Uri.parse(image_path);
imgView.setImageURI(null);
imgView.setImageURI(fileUri);
答案2
得分: 0
Ideally, this would be just one activity (perhaps using two fragments), rather than two activities.
But, if you want to stick with this structure, you will need to change this:
imageUri = data.getData();
Intent intent = new Intent(this, NextActivity.class);
if (imageUri != null) {
intent.putExtra("imageUri", imageUri.toString());
startActivity(intent);
}
to:
imageUri = data.getData();
if (imageUri != null) {
Intent intent = new Intent(this, NextActivity.class);
intent.setData(imageUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}
and change:
String image_path = intent.getStringExtra("imageUri");
Uri fileUri = Uri.parse(image_path);
to:
Uri fileUri = intent.getData();
Without that, NextActivity
will not have rights to use the content identified by the Uri
.
Also, please replace setImageURI()
with an image-loading library, such as Glide or Picasso.
英文:
Ideally, this would be just one activity (perhaps using two fragments), rather than two activities.
But, if you want to stick with this structure, you will need to change this:
imageUri = data.getData();
Intent intent = new Intent (this, NextActivity.class );
if(imageUri != null){
intent.putExtra("imageUri", imageUri.toString());
startActivity(intent);
}
to:
imageUri = data.getData();
if (imageUri != null){
Intent intent = new Intent (this, NextActivity.class);
intent.setData(imageUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}
and change:
String image_path = intent.getStringExtra("imageUri");
Uri fileUri = Uri.parse(image_path);
to:
Uri fileUri = intent.getData();
Without that, NextActivity
will not have rights to use the content identified by the Uri
.
Also, please replace setImageURI()
with an image-loading library, such as Glide or Picasso.
专注分享java语言的经验与见解,让所有开发者获益!
评论