Android 如何将从照相机拍摄的图像设置为ImageView?

Android 如何将从照相机拍摄的图像设置为ImageView?,android,image,android-intent,Android,Image,Android Intent,在我的应用程序中,用户可以从相机中拍摄图像,然后我想将该图像返回到图像视图。我该怎么做 以下是我的相机意图: Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); startActivityForResult(intent, TAKE_PICTURE); 和onActivityResult protected void onActivityResult(int requestCode, int resultCode,

在我的应用程序中,用户可以从相机中拍摄图像,然后我想将该图像返回到图像视图。我该怎么做

以下是我的相机意图:

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();
有人能帮忙吗?

你是说这个吗

image.setImageBitmap(bitmap);
要将位图旋转回其原始方向,可以使用以下代码

        ExifInterface exif = new ExifInterface(path);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        int angle = 0;

        if (orientation == ExifInterface.ORIENTATION_ROTATE_90)
            angle = 90;
        else if (orientation == ExifInterface.ORIENTATION_ROTATE_180)
            angle = 180;
        else if (orientation == ExifInterface.ORIENTATION_ROTATE_270)
            angle = 270;

        Matrix mat = new Matrix();
        mat.postRotate(angle);
        Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight, mat, true);
这里的字符串“path”是存储图片的文件的路径。这是目前我唯一可以用来解决这个问题的代码。我希望这对你有帮助。 在这种情况下,可能有趣的是,您还可以为意图提供一个文件。照片将存储在该文件中

intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destinationFile));

下面是一个示例活动,它将启动camera应用程序,然后检索图像并显示它

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);
    }  
} 
} 请注意,照相机应用程序本身使您能够查看/重新拍摄图像,一旦图像被接受,活动就会显示它

以下是上述活动使用的布局。它只是一个包含id为button1的按钮和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。

谢谢您的帮助!!然而,我现在有一个奇怪的问题,如果我在肖像中拍摄图像,它会以风景缩略图的形式返回?