Tell camera intent to save captured image

I'm doing it:

intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); final File storage = Environment.getExternalStorageDirectory(); final Uri uri = Uri.fromFile(new File(storage, System.currentTimeMillis() + ".jpg")); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); startActivityForResult(intent, id); 

and process this photo, I do this:

 private String getLastImagePath() { final String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA }; final String imageOrderBy = MediaStore.Images.Media._ID + " DESC"; final Cursor imageCursor = getContentResolver().query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, null, null, imageOrderBy); if (imageCursor.moveToFirst()) { final String fullPath = imageCursor.getString(imageCursor .getColumnIndex(MediaStore.Images.Media.DATA)); return fullPath; } else { throw new RuntimeException(); } } 

However, I keep getting messages like this:

 07-02 14:46:54.751: E/BitmapFactory(23119): Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/DCIM/Camera/IMG_20130702_144653.jpg: open failed: ENOENT (No such file or directory) 

If I checked the Gallery, the photo is not there, so I assume that Intent ignores the value of MediaStore.EXTRA_OUTPUT .

Is there anything I can do that is not related to writing my own Camera solution?

+3
source share
2 answers

Use as follows:

Goal:

  Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(takePicture, 0); 

To get this result:

  protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { super.onActivityResult(requestCode, resultCode, imageReturnedIntent); switch(requestCode) { case 0: if(resultCode == RESULT_OK){ Uri selectedImage = imageReturnedIntent.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); //file path of captured image filePath = cursor.getString(columnIndex); //file path of captured image File f = new File(filePath); filename= f.getName(); Toast.makeText(SiteViewFieldCreate.this, "Your Path:"+filePath, 2000).show(); Toast.makeText(SiteViewFieldCreate.this, "Your Filename:"+filename, 2000).show(); cursor.close(); //Convert file path into bitmap image using below line. // yourSelectedImage = BitmapFactory.decodeFile(filePath); //put bitmapimage in your imageview //yourimgView.setImageBitmap(yourSelectedImage); } break; } } 

Add this to your .xml manifest .

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

Hope this gives you some solution.

+3
source

Nirmals solution works fine on all devices except Android 4.x (Apilevel 15). This seems to be a mistake, but imageReturnedIntent.getData() always returns null! (test on the emulator AND on the device) The captured photo is not even saved to the external drive, so any attempt to get the latest id or the highest date_added from the ContentResolver will fail (returns the last photo from the map, but not the captured photo)! I currently have no solution for this problem ... it seems impossible to get the path for the taken picture on Apilevel 15 (while you can still get the option to draw and save by yourself) ..

+1
source

Source: https://habr.com/ru/post/948675/


All Articles