使用URI在活动中显示图像

时间:2010-12-06 02:46:54

标签: java android

我正在编写一个使用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的专业Android 2应用程序开发一书中采用的方法。

该方法工作正常,我假设我刚刚拍摄的图片的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创建一个Bitmap对象并将其设置为ImageView,但我收到一个意外错误并强制退出。

我看过这里的例子here从URI创建了一个Bitmap,但它没有显示它?

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

我可能只是错过了一些简单的东西...所以任何帮助都会非常感激!

另外还有一个问题:如果我要拍多张照片,你会建议我使用SimpleCursorAdapter吗?

谢谢!

3 个答案:

答案 0 :(得分:1)

  1. 您可以使用URI
  2. 创建位图
  3. 您创建了一个在Bitmap中传递的BitmapDrawable的新实例
  4. 您将BitmapDrawable添加到窗口(setBackgroundDrawable), View(setBackgroundDrawable)]或ImageView(setImageDrawable)

答案 1 :(得分:0)

这是正确的做法:

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);
        }
    }
}

答案 2 :(得分:0)

这个问题已经有很长时间了。我希望这个答案可以帮助有相同问题的人。 我只测试了这段代码,就可以正常工作。

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>