Android camera photo take FileNotFoundException

半世苍凉 提交于 2020-01-07 04:51:07

问题


ORIGINAL :In my application i am calling native camera from code, my intent to take a picture from camera and get its file path also show it in the image view.

code below is calling from AlertDialog.Builder

//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();


                }
            });

in the onActivityResult

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();
            }
     } 

and its always going for FileNotFoundException, how can i solve this?

since i am using try catch , it doesn't give much log

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

after changing onActivityResult

 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();
            }
     }

my log here

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:


i have changed my perspective to this problem , simple is the better

in some global

int TAKE_PHOTO_CODE = 1888;

in buttononclick or where you call the camera intent

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

in the onActivityResult

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);

        }}



回答2:


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

Try this method create your temp file.



来源:https://stackoverflow.com/questions/23865604/android-camera-photo-take-filenotfoundexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!