Android从gallery或google照片中获取实际文件路径

Android从gallery或google照片中获取实际文件路径,android,Android,我正在尝试获取图像的实际路径,以便可以作为多部分/表单数据上传或发送到服务器。所以,一旦我得到Uri,我就有下面的代码 Uri selectedImageUri = data.getData(); File file = new File(selectedImageUri.getPath());//create path from uri final String[] split = file.getPath().split(&qu

我正在尝试获取图像的实际路径,以便可以作为多部分/表单数据上传或发送到服务器。所以,一旦我得到Uri,我就有下面的代码

Uri selectedImageUri = data.getData();
                File file = new File(selectedImageUri.getPath());//create path from uri
                final String[] split = file.getPath().split(":");//split the path.
                String path = split[1];//assign it to a string(your choice).

   
uri value = content://com.android.providers.media.documents/document/image%3A32 
path value = 32

我不明白为什么path是一个数字,即使我尝试使用getcontentResolver(),我也得到path=32。我使用的是API 29,所以我不能使用MediaStore.Images.Media.DATA,所以我必须使用MediaStore.Images.Media.\u ID。如果有人也能告诉我如何将图像作为表单数据上传,那就太好了。

您不必有文件系统访问图像,也不需要文件系统路径来上传图像。只需使用
Uri
。再见,谢谢。我正试图将uri作为inputstream发送到服务器,但从服务器上获取的表单密钥或值长度限制2048超出时,看起来字节有问题,但若我从邮递员上传相同的图像到服务器,则效果良好。OutputStream OutputStream=conn.getOutputStream();字节[]缓冲区=新字节[maxBufferSize];字节读取=-1;InputStream InputStream=getContentResolver().openInputStream(uri);while((bytesRead=inputStream.read(buffer))!=-1{outputStream.write(buffer,0,bytesRead);}outputStream.close();inputStream.close();