0

これは私のコードです:

 String _path = Environment.getExternalStorageDirectory() + File.separator +  "temp.jpg";
                           File file = new File( _path );
                           Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);             
                           Uri pictureUri = Uri.fromFile(file);
                           cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);
                           startActivityForResult(cameraIntent, CAMERA_REQUEST);

protected void onActivityResult(int requestCode, int resultCode, Intent data) {       
            switch(requestCode){
                case CAMERA_REQUEST:{
                    Bitmap bmp = (Bitmap) data.getExtras().get("data");    

そして私はマニフェストに以下を追加しました:

<uses-permission android:name="android.permission.CAMERA" />
 <uses-feature android:name="android.hardware.camera" />
 <uses-feature android:name="android.hardware.camera.autofocus" />

私は何が間違っているのですか?最初に欲しいのは画像を保存することですか?

Logcat:

04-20 10:08:38.547: E/AndroidRuntime(219): Uncaught handler: thread main exiting due to uncaught exception
04-20 10:08:38.557: E/AndroidRuntime(219): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=null} to activity {com.vimateam.android.bestgreekbeaches/com.vimateam.android.bestgreekbeaches.Gallery1}: java.lang.NullPointerException
04-20 10:08:38.557: E/AndroidRuntime(219):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3329)
04-20 10:08:38.557: E/AndroidRuntime(219):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3371)
04-20 10:08:38.557: E/AndroidRuntime(219):  at android.app.ActivityThread.access$2700(ActivityThread.java:119)
04-20 10:08:38.557: E/AndroidRuntime(219):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1893)
04-20 10:08:38.557: E/AndroidRuntime(219):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 10:08:38.557: E/AndroidRuntime(219):  at android.os.Looper.loop(Looper.java:123)
04-20 10:08:38.557: E/AndroidRuntime(219):  at android.app.ActivityThread.main(ActivityThread.java:4363)
04-20 10:08:38.557: E/AndroidRuntime(219):  at java.lang.reflect.Method.invokeNative(Native Method)
04-20 10:08:38.557: E/AndroidRuntime(219):  at java.lang.reflect.Method.invoke(Method.java:521)
04-20 10:08:38.557: E/AndroidRuntime(219):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-20 10:08:38.557: E/AndroidRuntime(219):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-20 10:08:38.557: E/AndroidRuntime(219):  at dalvik.system.NativeStart.main(Native Method)
04-20 10:08:38.557: E/AndroidRuntime(219): Caused by: java.lang.NullPointerException
4

2 に答える 2

0

intent.setDataAndType()Acitivtyを開始するときに設定していないデータ部分nullを取得しています。データを取得する場合は、次のBitmap ようにcamraアクティビティを開始します。

public static final String IMAGE_UNSPECIFIED = "image/*"; 
    Intent intent = new Intent(Intent.ACTION_PICK, null);  
    intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);  
    startActivityForResult(intent, 3);  

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == 0)  
       return; 
    if (requestCode == 2) {  
       Uri uri=data.getData();  //YOU GET DATA HERE
        }  
//OR
if (requestCode == 3) {  
 Bundle extras = data.getExtras();  
 if (extras != null) {  
 Bitmap photo = extras.getParcelable("data");  
 ByteArrayOutputStream stream = new ByteArrayOutputStream();  
 photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0 - 100)????  
 imageView.setImageBitmap(photo);  
  }  
}  

または、イメージパスを使用する場合は、次のようにします。

if (requestCode == CAMERA_REQUEST) {  
     //pic path
      File picture = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");  
   } 
于 2012-04-20T10:51:30.333 に答える
0

これはカメラのコードであり、画像をSDカードに保存します。

private static final int CAMERA_REQUEST = 1888;  
    buttonTakePicture.setOnClickListener(new Button.OnClickListener() {
                @Override
                public void onClick(View arg0) {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());

    startActivityForResult(intent, CAMERA_REQUEST);
                    }
            });

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == CAMERA_REQUEST) {
                    photo = (Bitmap) data.getExtras().get("data");
                }
    }

    private Uri getImageUri() {


            File file1 = new File(Environment.getExternalStorageDirectory() + "/Camerafolder");
            if (!file1.exists())
            {
                file1.mkdirs();
            }

            File file = new File(Environment.getExternalStorageDirectory() + "/Camerafolder/"+"img"+".png");

    Uri imgUri = Uri.fromFile(file);

            return imgUri;
        }
于 2012-04-20T11:13:41.900 に答える