0

私はこのコードを試しました:

public class pantalla8 extends Activity {

    private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
    private Uri fileUri;
    public static final int MEDIA_TYPE_IMAGE = 1;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera);

        final Button camera = (Button) findViewById(R.id.boton12);
        camera.setOnClickListener(new OnClickListener(){
            public void onClick(View arg0){
                Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
                startActivityForResult(cameraIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
            }
        });
    }

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


    private static File getOutputMediaFile(int type){
        String state = Environment.getExternalStorageState();

        if (Environment.MEDIA_MOUNTED.equals(state)) {
            File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyCameraApp");
            if (! mediaStorageDir.exists()){
                if (! mediaStorageDir.mkdirs()){
                    Log.d("MyCameraApp", "failed to create directory");
                    return null;
                }
            }

            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            File mediaFile;
            if (type == MEDIA_TYPE_IMAGE){
                mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "IMG_"+ timeStamp + ".jpg");
            } else {
                return null;
            }

            return mediaFile;
        } else {
            return null;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                Toast.makeText(this, "Image saved to:\n" +
                         data.getData(), Toast.LENGTH_LONG).show();
            } else if (resultCode == RESULT_CANCELED) {
                Toast.makeText(this, "Cancelado", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "Falla", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

MyCameraAppファイルは正しく作成され、写真はこのディレクトリに正しく保存されますが、ランタイム例外が発生します。

10-04 14:22:16.398: E/AndroidRuntime(6952): FATAL EXCEPTION: main
10-04 14:22:16.398: E/AndroidRuntime(6952): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {tian.proto/tian.proto.pantalla8}: java.lang.NullPointerException
10-04 14:22:16.398: E/AndroidRuntime(6952):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
10-04 14:22:16.398: E/AndroidRuntime(6952):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
10-04 14:22:16.398: E/AndroidRuntime(6952):     at android.app.ActivityThread.access$2000(ActivityThread.java:117)
10-04 14:22:16.398: E/AndroidRuntime(6952):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
10-04 14:22:16.398: E/AndroidRuntime(6952):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-04 14:22:16.398: E/AndroidRuntime(6952):     at android.os.Looper.loop(Looper.java:130)
10-04 14:22:16.398: E/AndroidRuntime(6952):     at android.app.ActivityThread.main(ActivityThread.java:3687)
10-04 14:22:16.398: E/AndroidRuntime(6952):     at java.lang.reflect.Method.invokeNative(Native Method)
10-04 14:22:16.398: E/AndroidRuntime(6952):     at java.lang.reflect.Method.invoke(Method.java:507)
10-04 14:22:16.398: E/AndroidRuntime(6952):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
10-04 14:22:16.398: E/AndroidRuntime(6952):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-04 14:22:16.398: E/AndroidRuntime(6952):     at dalvik.system.NativeStart.main(Native Method)
10-04 14:22:16.398: E/AndroidRuntime(6952): Caused by: java.lang.NullPointerException
10-04 14:22:16.398: E/AndroidRuntime(6952):     at tian.proto.pantalla8.onActivityResult(pantalla8.java:95)
10-04 14:22:16.398: E/AndroidRuntime(6952):     at android.app.Activity.dispatchActivityResult(Activity.java:3908)
10-04 14:22:16.398: E/AndroidRuntime(6952):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
10-04 14:22:16.398: E/AndroidRuntime(6952):     ... 11 more

理由はわかりません...誰かが私が間違っていることを教えてもらえますか?AsyncTaskスレッド内でカメラインテントを呼び出そうとすべきですか?

前もって感謝します!

4

1 に答える 1

0

確かにブレークポイントとデバッグを使用しますが、あなたが行う唯一のオブジェクト呼び出しは、データがnullでonActivityResultあるdata.getData()と推測することです。

于 2012-10-04T19:16:45.807 に答える