android - 如何将从相机 Intent 拍摄的图像设置为 ImageView?

标签 android image android-intent

在我的应用程序中,用户可以从相机 Intent 中获取图像,然后我想将该图像返回到 ImageView 。我怎样才能做到这一点?

这是我的相机 Intent :

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, TAKE_PICTURE);

onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data)
    { 
        //Check that request code matches ours:
        if (requestCode == TAKE_PICTURE)
        {
            //Check if your application folder exists in the external storage, if not create it:
            File imageStorageFolder = new File(Environment.getExternalStorageDirectory()+File.separator+"Kruger National Park");
            if (!imageStorageFolder.exists())
            {
                imageStorageFolder.mkdirs();
                Log.d(TAG , "Folder created at: "+imageStorageFolder.toString());
            }

            //Check if data in not null and extract the Bitmap:
            if (data != null)
            {
                String filename = "image";
                String fileNameExtension = ".jpg";
                File sdCard = Environment.getExternalStorageDirectory();
                String imageStorageFolder1 = File.separator+"Kruger National Park"+File.separator;
                File destinationFile = new File(sdCard, imageStorageFolder1 + filename + fileNameExtension);
                Log.d(TAG, "the destination for image file is: " + destinationFile );
                if (data.getExtras() != null)
                {
                    Bitmap bitmap = (Bitmap)data.getExtras().get("data");
                    try
                    {
                        FileOutputStream out = new FileOutputStream(destinationFile);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
                        out.flush();
                        out.close();
                    }
                    catch (Exception e)
                    {
                        Log.e(TAG, "ERROR:" + e.toString());
                    }

一切正常,但现在只想将它添加到我的 ImageView 中:
ImageView image = (ImageView) v.findViewById(R.id.imageV);
        image.setImageResource();

有人可以帮忙吗?

最佳答案

这是一个示例 Activity ,它将启动相机应用程序,然后检索图像并显示它。

package edu.gvsu.cis.masl.camerademo;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MyCameraActivity extends Activity {
private static final int CAMERA_REQUEST = 1888; 
private ImageView imageView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.imageView = (ImageView)this.findViewById(R.id.imageView1);
    Button photoButton = (Button) this.findViewById(R.id.button1);
    photoButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent cameraIntent = new     Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            startActivityForResult(cameraIntent, CAMERA_REQUEST); 
        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);
    }  
} 

}
请注意,相机应用程序本身使您能够查看/重新拍摄图像,一旦图像被接受, Activity 就会显示它。

这是上述 Activity 使用的布局。它只是一个 LinearLayout,包含一个 ID 为 button1 的 Button 和一个 ID 为 imageview1 的 ImageView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/button1" android:layout_width="wrap_content"     android:layout_height="wrap_content" android:text="@string/photo"></Button>
<ImageView android:id="@+id/imageView1" android:layout_height="wrap_content"     android:src="@drawable/icon" android:layout_width="wrap_content"></ImageView>

</LinearLayout>

最后一个细节,一定要补充:
<uses-feature android:name="android.hardware.camera"></uses-feature> 

并且如果相机对于您的应用功能是可选的。确保在权限中将 require 设置为 false。像这样
<uses-feature android:name="android.hardware.camera" android:required="false"></uses-feature>

到您的 manifest.xml。

关于android - 如何将从相机 Intent 拍摄的图像设置为 ImageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27542060/

相关文章:

java - Android 应用程序 SQLiteDatabase 插入新值不起作用,代码中有 0 个错误

python - 为什么我的代码无法访问 ENTER 或 STRAIGHT 文件夹或 .png 文件

android - getBounds 返回高度和宽度 0

c# - 为照片添加时间/日期戳

android - 将文本转换委托(delegate)给 "plugin"Android 应用程序,事先不知道

android - 从拨号器打开 Android 应用程序

android - 加密 Android 设备上的信息(合理防止用户访问)

Android Web 查看下载文件 "Can' t 打开文件”消息

java - 除非通过第二个 Java Activity 向后提示,否则我的第一个 Java Activity 中的动画不起作用

android - SIP Android 应用程序,如何使用服务和 Activity