Get Path and Filename from Camera intent result

前端 未结 3 2035
生来不讨喜
生来不讨喜 2020-12-14 09:20

I want to make a picture with the camera intent and save it to the default DCIM folder. Then I want to get the path/filename where the picture is stored.

I am trying

相关标签:
3条回答
  • 2020-12-14 10:00

    When you are starting the ACTION_IMAGE_CAPTURE you can pass an extra MediaStore.EXTRA_OUTPUT as the URI of the file where you want to save the picture.

    Here is a simple example:

       File file = new File(path);
       Uri outputFileUri = Uri.fromFile(file);
    
       Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
       intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    
       startActivityForResult(intent, TAKE_PICTURE); 
    

    EDIT: I just tried on my device and file.createNewFile() solved the problem for me.

    0 讨论(0)
  • 2020-12-14 10:02

    The picture will be stored twice, first on gallery folder, and after on the file you especified on putExtra(MediaStore.EXTRA_OUTPUT, path) method.

    You can obtain the last picture taken doing that:

    /**
     * Gets the last image id from the media store
     * @return
     */
    private int getLastImageId(){
        final String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA };
        final String imageOrderBy = MediaStore.Images.Media._ID+" DESC";
        Cursor imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, null, null, imageOrderBy);
        if(imageCursor.moveToFirst()){
            int id = imageCursor.getInt(imageCursor.getColumnIndex(MediaStore.Images.Media._ID));
            String fullPath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
            Log.d(TAG, "getLastImageId::id " + id);
            Log.d(TAG, "getLastImageId::path " + fullPath);
            imageCursor.close();
            return id;
        }else{
            return 0;
        }
    }
    

    This sample was based on post: Deleting a gallery image after camera intent photo taken

    0 讨论(0)
  • 2020-12-14 10:05

    you can use like this in onActivityResult()

    if(requestCode==CAMERA_CAPTURE)
        {
    
            Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION}, Media.DATE_ADDED, null, "date_added ASC");
            if(cursor != null && cursor.moveToFirst())
            {
                do 
                {
                    String picturePath =cursor.getString(cursor.getColumnIndex(Media.DATA));
                   Uri selectedImage = Uri.parse(picturePath);
    
                }
                while(cursor.moveToNext());
                    cursor.close();
                File out = new File(picturePath);
                try 
                {
                    mOriginal = decodeFile(out);
                }
                catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
    
                mSelected.setImageBitmap(mOriginal);
            }
        }
    
    0 讨论(0)
提交回复
热议问题