Android 拍照,保存图片并在imageview中加载,错误

Android 拍照,保存图片并在imageview中加载,错误,android,image,save,loading,photo,Android,Image,Save,Loading,Photo,我想当你按下按钮时,它会打开相机模式。那你拍张照片确认一下。然后,照片将存储在硬盘上,然后返回正常布局,在图像视图中查看图像 我的问题是,在照片确认应用程序崩溃后 package de.example.camera; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import andro

我想当你按下按钮时,它会打开相机模式。那你拍张照片确认一下。然后,照片将存储在硬盘上,然后返回正常布局,在图像视图中查看图像

我的问题是,在照片确认应用程序崩溃后

    package de.example.camera;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;



public class Camera extends Activity {


ImageView iv;
public File mediaFile;
public Uri fileUri;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.j_camera);
    iv=(ImageView) findViewById(R.id.imageView);

    Button btn = (Button) findViewById(R.id.photo);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            fileUri = getOutputMediaFileUri(1);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
            intent.putExtra("return-data", true);
            startActivityForResult(intent, 0);
        }
    });

}

private File getTempFile(Context context){
    //it will return /sdcard/image.tmp
    final File path = new File( Environment.getExternalStorageDirectory(), context.getPackageName() );
    if(!path.exists()){
        path.mkdir();
    }
    return new File(path, "image.tmp");


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        switch(requestCode){
            case 0:
                final File file = getTempFile(this);
                try {
                    Bitmap captureBmp = MediaStore.Images.Media.getBitmap(getContentResolver(), Uri.fromFile(file));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
        }

}
        Bitmap theImage = (Bitmap) data.getExtras().get("data");
        iv.setImageBitmap(theImage);

    }
    private Uri getOutputMediaFileUri(int type){
        return Uri.fromFile(getOutputMediaFile(type));

    }private File getOutputMediaFile(int type){
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "YourFolderName");
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Toast.makeText(null, "failed to create directory", Toast.LENGTH_SHORT).show();
            return null;
        }
    }
    if (type == 1){
        Long tsLong = System.currentTimeMillis()/1000;
        String ts = tsLong.toString();
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "photo"+ ts + ".jpg");

    }
    else {
        return null;
    }

    return mediaFile;
}
    }
错误:

03-14 11:57:34.973  14565-14565/de.example.camera E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {de.example.camera/de.example.camera.Camera}: java.lang.NullPointerException
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3406)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3449)
            at android.app.ActivityThread.access$1100(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1343)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:150)
            at android.app.ActivityThread.main(ActivityThread.java:5162)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at de.example.camera.Camera.onActivityResult(Camera.java:87)
            at android.app.Activity.dispatchActivityResult(Activity.java:5322)
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3402)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3449)
            at android.app.ActivityThread.access$1100(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1343)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:150)
            at android.app.ActivityThread.main(ActivityThread.java:5162)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

使用这种方式启动itent

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
    startActivityForResult(takePictureIntent,REQUEST_IMAGE_CAPTURE);
}
在onActivityResult中

Bundle extras = data.getExtras();
Bitmap bmp = (Bitmap) extras.get("data");
图像自动存储在sd中,您也可以将其保存在bmp中

编辑:

private static final int REQUEST_IMAGE_CAPTURE = 1
它错过了这个,
您将在onActivity结果中获得该值,它将为您提供已发出的witch请求的信息,在这种情况下,您将在requestCode中收到1,从静态文件getOutputMediaFile(…)中删除静态文件getOutputMediaFileUri(…)Thx@Simple Plan。相机保存图片,但当我按下相机的“确定”按钮时,应用程序崩溃。您的静态错误是否已解决?当我使用您的方式启动意图时,“
请求图像捕获
”标记为红色,并显示“
无法解析符号“请求图像捕获”
”您是否将权限放入清单?