在活动中使用URI显示图像

3
我正在编写一个应用程序,使用Intent(MediaStore.ACTION_IMAGE_CAPTURE)来捕获图像。在捕获图像的过程中,我注意到了图像URI的输出。在完成相机活动之后,我希望使用这个特定的URI来显示图像。
我使用的捕获图像的方法是:
    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);
}

这是从Reto Meier的书《Professional Android 2 Application Development》中提取的一种方法。

这个方法很好用,我假设刚拍摄的图片的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

  }
}

我不确定该怎么做。

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

我看过这里的示例here,它从URI创建位图,但不显示它?

我的问题只是如何在正在运行的活动中显示图像?我需要获取文件路径(例如this)才能显示它吗?如果我从URI创建位图,我该如何显示位图?

我可能只是缺少一些简单的东西...所以任何帮助都将不胜感激!

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

谢谢!

3个回答

2
这个问题很久以前就有了。我希望这个答案能帮助那些有同样问题的人。 我刚刚测试了这段代码,它运行得很好。
Java代码:
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代码:

<?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>

1
  1. 通过URI创建一个位图
  2. 创建一个新的BitmapDrawable实例,传入该位图
  3. 将BitmapDrawable添加到窗口(setBackgroundDrawable)、视图(setBackgroundDrawable)或ImageView(setImageDrawable)中

好的,我会尽快在这里返回结果。谢谢。 - evkwan

1
这是正确的做法:
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);
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接