ImageView在Java Android中不显示来自意图的图像。

huangapple 未分类评论53阅读模式
英文:

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

&lt;!-- language: java --&gt;

if (resultCode == RESULT_OK &amp;&amp; requestCode == PICK_IMAGE){
            imageUri = data.getData();
            Intent intent = new Intent (this, NextActivity.class );
            if(imageUri != null){
                intent.putExtra(&quot;imageUri&quot;, imageUri.toString());
                startActivity(intent);
            }

        }

NextActivity.java onCreate method

&lt;!-- language: java --&gt;

ImageView imgView = (ImageView) findViewById(R.id.imageView1);
Intent intent = getIntent();
String image_path = intent.getStringExtra(&quot;imageUri&quot;);
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

&lt;!-- language: xml --&gt;

&lt;ImageView
        android:id=&quot;@+id/imageView1&quot;
        android:layout_width=&quot;396dp&quot;
        android:layout_height=&quot;450dp&quot;
        android:adjustViewBounds=&quot;true&quot;
        android:contentDescription=&quot;@string/app_name&quot;
        android:scaleType=&quot;fitXY&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintHorizontal_bias=&quot;0.466&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

答案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(&quot;imageUri&quot;, 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(&quot;imageUri&quot;);
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.

huangapple
  • 本文由 发表于 2020年4月8日 18:55:39
  • 转载请务必保留本文链接:https://java.coder-hub.com/61099031.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定