Camera for Android Camera FileNotFoundException

ORIGINAL: In my application, I call my own camera from the code, my intention is to take a picture from the camera and get its path to the file and also show it in the image view.

below is the code 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 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 it always happens for a FileNotFoundException, how can I solve this?

since i use 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 magazine is 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 
0
source share
2 answers

I changed my point of view on this problem, simpler - better

in some global

 int TAKE_PHOTO_CODE = 1888; 

in buttononclick or where you invoke camera intent

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

in 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); }} 
+1
source
 File outputDir = context.getCacheDir(); // context being the Activity pointer File outputFile = File.createTempFile("prefix", "extension", outputDir); 

Try this method to create a temporary file.

0
source

All Articles