Android 相机拍照 FileNotFoundException

Android camera photo take FileNotFoundException

ORIGINAL :在我的应用程序中,我从代码中调用本机相机,我打算从相机拍摄照片并获取其文件路径也在图像视图中显示它。

下面的代码是从 AlertDialog.Builder

调用的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//run camera
            builder.setNegativeButton("Resim Cek", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //native camera
                     // here,counter will be incremented each time,and the picture taken by camera will be stored as 1.jpg,2.jpg and likewise.
                    count++;
                    String file = dir+count+".jpg";
                    File newfile = new File(file);
                    try {
                        newfile.createNewFile();
                    } catch (IOException e) {}      

                    Uri outputFileUri = Uri.fromFile(newfile);

                    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

                    startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);  
                    dialog.dismiss();


                }
            });

在 onActivityResult

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
        Log.d("CameraDemo","Pic saved");
        final File file = getTempFile(this);
        try
        {
        Media.getBitmap(getContentResolver(), Uri.fromFile(file) );
        String image_string = Uri.fromFile(file).toString();
        Bitmap bm = Bitmap.createScaledBitmap(
            BitmapFactory.decodeFile(image_string),
            getResources().getDisplayMetrics().widthPixels,
            getResources().getDisplayMetrics().heightPixels,
            true);
        bmpPhoto = bm;
        bmpPhotoPath =image_string;
        imageView.setImageBitmap(bmpPhoto);
        removePhoto.setVisibility(View.VISIBLE);
            }
        catch (FileNotFoundException e)
            {
            Toast.makeText(getApplicationContext(),"file not found exception", Toast.LENGTH_SHORT).show();
            }
        catch (IOException e)
            {
            Toast.makeText(getApplicationContext(),"ioexception", Toast.LENGTH_SHORT).show();
            }
     }

它总是出现 FileNotFoundException,我该如何解决这个问题?

因为我使用的是 try catch ,所以它不会给出太多日志

1
 05-26 10:04:12.620: D/CameraDemo(3794): Pic saved

改变onActivityResult后

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
        Log.d("CameraDemo","Pic saved");
        //final File file = getTempFile(this);
        File outputDir = ContactFormActivity.this.getCacheDir(); // context being the Activity pointer
        File file = null;
        try {
            file = File.createTempFile("prefix","extension", outputDir);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try
        {
        Media.getBitmap(getContentResolver(), Uri.fromFile(file) );
        String image_string = Uri.fromFile(file).toString();
        Bitmap bm = Bitmap.createScaledBitmap(
            BitmapFactory.decodeFile(image_string),
            getResources().getDisplayMetrics().widthPixels,
            getResources().getDisplayMetrics().heightPixels,
            true);
        bmpPhoto = bm;
        bmpPhotoPath =image_string;
        imageView.setImageBitmap(bmpPhoto);
        removePhoto.setVisibility(View.VISIBLE);
            }
        catch (FileNotFoundException e)
            {
            Toast.makeText(getApplicationContext(),"file not found exception", Toast.LENGTH_SHORT).show();
            }
        catch (IOException e)
            {
            Toast.makeText(getApplicationContext(),"ioexception", Toast.LENGTH_SHORT).show();
            }
     }

我的日志在这里

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
05-26 10:29:59.170: D/CameraDemo(6559): Pic saved
05-26 10:29:59.190: D/skia(6559): --- SkImageDecoder::Factory returned null
05-26 10:29:59.190: I/System.out(6559): Not a DRM File, opening notmally
05-26 10:29:59.190: D/AndroidRuntime(6559): Shutting down VM
05-26 10:29:59.190: W/dalvikvm(6559): threadid=1: thread exiting with uncaught exception (group=0x40c551f8)
05-26 10:29:59.230: E/AndroidRuntime(6559): FATAL EXCEPTION: main
05-26 10:29:59.230: E/AndroidRuntime(6559): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {com.company.project/com.company.project.ContactFormActivity}: java.lang.NullPointerException
05-26 10:29:59.230: E/AndroidRuntime(6559):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2992)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3035)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at android.app.ActivityThread.access$1100(ActivityThread.java:127)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1189)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at android.os.Looper.loop(Looper.java:137)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at android.app.ActivityThread.main(ActivityThread.java:4507)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at java.lang.reflect.Method.invokeNative(Native Method)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at java.lang.reflect.Method.invoke(Method.java:511)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at dalvik.system.NativeStart.main(Native Method)
05-26 10:29:59.230: E/AndroidRuntime(6559): Caused by: java.lang.NullPointerException
05-26 10:29:59.230: E/AndroidRuntime(6559):     at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:432)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at com.company.project.ContactFormActivity.onActivityResult(ContactFormActivity.java:419)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at android.app.Activity.dispatchActivityResult(Activity.java:4653)
05-26 10:29:59.230: E/AndroidRuntime(6559):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2988)
05-26 10:29:59.230: E/AndroidRuntime(6559):     ... 11 more


我已经改变了我对这个问题的看法,简单就是更好

在一些全球

1
int TAKE_PHOTO_CODE = 1888;

在 buttononclick 或您调用相机意图的地方

1
2
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);

在 onActivityResult

1
2
3
4
5
6
7
8
9
10
11
12
13
if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
 if(data != null)
        {          
            Uri selectedImageUri = data.getData();
            String filestring = selectedImageUri.getPath();

            Bitmap bm = (Bitmap) data.getExtras().get("data");
            bmpPhoto = bm;
            bmpPhotoPath =filestring;
            imageView.setImageBitmap(bmpPhoto);
            removePhoto.setVisibility(View.VISIBLE);

        }}

1
2
File outputDir = context.getCacheDir(); // context being the Activity pointer
File outputFile = File.createTempFile("prefix","extension", outputDir);

试试这个方法创建你的临时文件。