Getting Uri Null in capture photo from camera intent in samsung mobile

后端 未结 3 1573
情歌与酒
情歌与酒 2021-01-03 12:35

I am getting null in contenturi in samsung phones while capturing photo from camera but rest of others phones its working fine.

@Override 
    protected void         


        
3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-03 13:17

    Hi i am also facing this issue to like I am checking app on MOTO G its not working but on Samsung devices its working So i do Below coding please check:-

    Uri selectedImageUri = data.getData();

                    try {
                        selectedImagePath = getPathBelowOs(selectedImageUri);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (selectedImagePath == null) {
                        try {
                            selectedImagePath = getPathUpperOs(selectedImageUri);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
    
    
    public String getPathBelowOs(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    
    /**
     * Getting image from Uri
     * 
     * @param contentUri
     * @return
     */
    public String getPathUpperOs(Uri contentUri) {// Will return "image:x*"
        String wholeID = DocumentsContract.getDocumentId(contentUri);
    
        // Split at colon, use second item in the array
        String id = wholeID.split(":")[1];
    
        String[] column = { MediaStore.Images.Media.DATA };
    
        // where id is equal to
        String sel = MediaStore.Images.Media._ID + "=?";
    
        Cursor cursor = getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel,
                new String[] { id }, null);
    
        String filePath = "";
    
        int columnIndex = cursor.getColumnIndex(column[0]);
    
        if (cursor.moveToFirst()) {
            filePath = cursor.getString(columnIndex);
        }
    
        cursor.close();
        return filePath;
    }
    

提交回复
热议问题