Java 使用 URI 在活动中显示图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4362760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 16:30:45  来源:igfitidea点击:

Displaying an Image in an activity using URI

javaandroid

提问by evkwan

I'm writing an application that uses Intent(MediaStore.ACTION_IMAGE_CAPTURE) to capture and image. On the process of capturing the image, I noted the output of the image's URI. Right after finishing the camera activity, I wish to display the image using this specific URI.

我正在编写一个使用 Intent(MediaStore.ACTION_IMAGE_CAPTURE) 捕获和图像的应用程序。在捕获图像的过程中,我记录了图像 URI 的输出。在完成相机活动后,我希望使用此特定 URI 显示图像。

The method I used to capture images is:

我用来捕捉图像的方法是:

    private void saveFullImage() {
  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  File file = new File(Environment.getExternalStorageDirectory(), 
                       "test.jpg");
  outputFileUri = Uri.fromFile(file); 
  intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
  startActivityForResult(intent, TAKE_PICTURE);
}

Which is a method taken from Reto Meier's book Professional Android 2 Application Development.

这是一种取自 Reto Meier 的书 Professional Android 2 Application Development 的方法。

The method works fine, and I assume that the URI of the picture I just took is stored in the outputFileUri variable. Then at this point of the code is where I want to display the picture:

该方法工作正常,我假设我刚刚拍摄的图片的 URI 存储在 outputFileUri 变量中。然后在代码的这一点是我要显示图片的地方:

    @Override
protected void onActivityResult(int requestCode, 
                                int resultCode, Intent data) {
  if (requestCode == TAKE_PICTURE) {

      //I want to display the picture I just took here
      //using the URI

  }
}

I'm not sure how to do it.

我不知道该怎么做。

I tried creating a new layout object and a new ImageView object using the method setImageURI(outputFileUri). My main layout (xml) did not have a ImageView object. But even when I set the contentView to the new layout with the ImageView attached to it, it doesn't display anything. I tried creating a Bitmap object from the URI and set it to the ImageView, but I get an unexpected error and forced exit.

我尝试使用 setImageURI(outputFileUri) 方法创建一个新的布局对象和一个新的 ImageView 对象。我的主布局 (xml) 没有 ImageView 对象。但即使我将 contentView 设置为附加了 ImageView 的新布局,它也不会显示任何内容。我尝试从 URI 创建一个 Bitmap 对象并将其设置为 ImageView,但出现意外错误并强制退出。

I have seen examples from here herewhich creates a Bitmap from URI, but it's not displaying it?

我在这里看到了从 URI 创建位图的示例,但它没有显示它?

My question is just how to display an image in the middle of a running activity? Do I need to get the File Path (like this) in order to display it? If I make a Bitmap out of the URI, how do I display the Bitmap?

我的问题是如何在跑步活动中显示图像?我是否需要获取文件路径(像这样)才能显示它?如果我从 URI 中制作位图,我如何显示位图?

I'm just probably missing something simple...so any help would be a greatly appreciated!

我可能只是遗漏了一些简单的东西……所以任何帮助都将不胜感激!

Also additional question for thought: If I were to take multiple pictures, would you recommend me to use the SimpleCursorAdapter instead?

还有一个需要思考的问题:如果我要拍摄多张照片,您会建议我改用 SimpleCursorAdapter 吗?

Thanks!

谢谢!

回答by mibollma

  1. You create a Bitmap out of the URI
  2. You create a new instance of BitmapDrawable passing in the Bitmap
  3. You add the BitmapDrawable to a Window (setBackgroundDrawable), View (setBackgroundDrawable)] or ImageView (setImageDrawable)
  1. 您从 URI 创建位图
  2. 您创建一个新的 BitmapDrawable 实例,传入 Bitmap
  3. 您将 Bi​​tmapDrawable 添加到 Window (setBackgroundDrawable)、View (setBackgroundDrawable)] 或 ImageView (setImageDrawable)

回答by Mark Ingram

Here's the correct way of doing it:

这是正确的做法:

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK)
    {
        Uri imageUri = data.getData();
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
        }
    }
}

回答by Mamad

This question is for long time ago. I hope this answer help somebody who has same question. I just test this code and it works fine.

这个问题是很久以前的了。我希望这个答案能帮助有同样问题的人。我只是测试了这段代码,它工作正常。

java code :

代码:

package com.mywebsite.mohammad.openfiledialog;
import android.content.Intent;    
import android.net.Uri;   
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
    ImageView ImageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView = (ImageView) findViewById(R.id.imageView);
        Intent intent = new Intent()
                .setType("image/*")
                .setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select a file"), 123);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==123 && resultCode==RESULT_OK) {
            Uri selectedfile = data.getData(); //The uri with the location of the file
            ImageView.setImageURI(selectedfile);
        }
    }
}

xml code :

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>