0

拍照后,我的照片显示在活动中,但是当我签入文件夹时,我的照片大小为 0kb,我不知道为什么会发生这种情况,当我再次拍照时,新照片没有保存在文件夹中我想要的,这是我的代码:

 {


                    File sdCard= Environment.getExternalStorageDirectory();
            File path = new File (sdCard.getAbsolutePath() + "/android/data/spaj_foto");
            path.mkdir();
            File file= new File (path,"spaj_foto.png");          
             outputFileUri = Uri.fromFile(file); 
              Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
              cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri  );
              startActivityForResult(cameraIntent,TAKE_PHOTO_CODE ); 

                }

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

            if (resultCode == RESULT_OK) {
                 if (requestCode == TAKE_PHOTO_CODE) {
                Bundle extras = data.getExtras();

                Bitmap photo = (Bitmap) extras.get("data"); 
                image_spaj.setImageBitmap(photo);
            }
            else if (resultCode == RESULT_CANCELED) {
                Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
               } 
            }


        }

编辑

这是我的日志:

    09-26 17:57:38.072: E/AndroidRuntime(12498): FATAL EXCEPTION: main
09-26 17:57:38.072: E/AndroidRuntime(12498): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {org.example.touch/org.example.touch.FormSpaj}: java.lang.NullPointerException
09-26 17:57:38.072: E/AndroidRuntime(12498):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3179)
09-26 17:57:38.072: E/AndroidRuntime(12498):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3222)
09-26 17:57:38.072: E/AndroidRuntime(12498):    at android.app.ActivityThread.access$1100(ActivityThread.java:140)
09-26 17:57:38.072: E/AndroidRuntime(12498):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1276)
09-26 17:57:38.072: E/AndroidRuntime(12498):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 17:57:38.072: E/AndroidRuntime(12498):    at android.os.Looper.loop(Looper.java:137)
09-26 17:57:38.072: E/AndroidRuntime(12498):    at android.app.ActivityThread.main(ActivityThread.java:4895)
09-26 17:57:38.072: E/AndroidRuntime(12498):    at java.lang.reflect.Method.invokeNative(Native Method)
09-26 17:57:38.072: E/AndroidRuntime(12498):    at java.lang.reflect.Method.invoke(Method.java:511)
09-26 17:57:38.072: E/AndroidRuntime(12498):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
09-26 17:57:38.072: E/AndroidRuntime(12498):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
09-26 17:57:38.072: E/AndroidRuntime(12498):    at dalvik.system.NativeStart.main(Native Method)
09-26 17:57:38.072: E/AndroidRuntime(12498): Caused by: java.lang.NullPointerException
09-26 17:57:38.072: E/AndroidRuntime(12498):    at org.example.touch.FormSpaj.onActivityResult(FormSpaj.java:990)
09-26 17:57:38.072: E/AndroidRuntime(12498):    at android.app.Activity.dispatchActivityResult(Activity.java:5347)
09-26 17:57:38.072: E/AndroidRuntime(12498):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3175)
09-26 17:57:38.072: E/AndroidRuntime(12498):    ... 11 more
4

1 回答 1

4

您已经创建了一个新文件。(空白的)

然后开始了一个相机意图。

但是你还没有做的是告诉相机意图将图像保存在哪里。

因此,给您留下一个空白文件。

  File file= new File (path,"spaj_foto.png");


  outputFileUri = Uri.fromFile(file); 
  Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
  cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri  );
  startActivityForResult(cameraIntent,TAKE_PHOTO_CODE );  

请注意,您不需要创建文件,android camera intent 会为您完成。你需要做的是传递额外的,所以意图知道在哪里保存。

(您仍然需要创建目录)

于 2013-09-26T10:40:57.917 回答