gpt4 book ai didi

android - 如何使用内容解析器从图库中的图像获取路径数据

转载 作者:行者123 更新时间:2023-11-29 00:58:20 25 4
gpt4 key购买 nike

我已经成功地从图库中获取了图像,但是我无法将图像上传到服务器,因为该文件为空。有没有我想添加的代码?我添加了 imageView.getPath,但我只获取从相机图像到服务器的路径,并从图库中获取空图像。

我得到了这条路径,content://com.android.providers.media.documents/document/image%3A5755但还是无法上传到服务器

private void getImageFromGallery(Intent data) {

mSelectedImgURI = data.getData();

mimeType = getImageExt(mSelectedImgURI);
uploadDialog.dismiss();

imgCategory.setImageURI(mSelectedImgURI);
}

private void getImageFromCamera(Intent data) {

Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

String tempFileName = System.currentTimeMillis() + ".jpg";
File destination = new File(Environment.getExternalStorageDirectory(),tempFileName);
FileOutputStream fo;

try {

destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();

mSelectedImgURI = Uri.fromFile(destination);
uploadDialog.dismiss();
imgCategory.setImageBitmap(thumbnail);

} catch (IOException e) {
Log.d(TAG, "Internal error - " + e.getLocalizedMessage());
}
}

public String getImageExt(Uri uri){
ContentResolver contentResolver = getApplicationContext().getContentResolver();
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
return mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(uri));
}

最佳答案

这是我常用的方法

 public String getRealPathFromURI(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}

这会返回文件位置,您可以通过调用获取文件

File realFile = new File(getRealPathFromURI(mSelectedImgUri));

关于android - 如何使用内容解析器从图库中的图像获取路径数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52924892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com