Tell the Camera Intent to save the taken image

不问归期 提交于 2019-11-28 14:06:41

Use Like this:

Intent:

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

To fetch that 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 in your manifest.xml.

<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 will give you some solution.

Nirmals solution works fine on all devices except on Android 4.x (Apilevel 15). Seems to be a bug, but imageReturnedIntent.getData() always returns null! (testet on emulator AND on device) The taken photo is not even saved on externaldir, so any attempt to get latest id or greatest date_added from ContentResolver will fail (returns the latest photo from card but not the taken photo)! Currently I have no solution for this problem...seems to be impossible to get the path for a taken picture on Apilevel 15 (while it's still possible to get the drawable and save by your own)...

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