昨天本人使用'com.github.LuckSiege.PictureSelector:picture_library:v2.6.0'获取图片地址,通过打印path,发现地址不完整
//隔离存储沙盒
AndroidManifest中Application下
1、android:requestLegacyExternalStorage="true"
通过判断版本获取真实图片地址
2、public static Bitmap getBitmapFromPath(Context contex, String path) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
return getBitmapFromUri(contex, Uri.parse(path));
} else {
return BitmapFactory.decodeFile(path);
}
}
// 通过uri加载图片 沙盒
public static Bitmap getBitmapFromUri(Context context, Uri uri) {
try {
ParcelFileDescriptor parcelFileDescriptor =
context.getContentResolver().openFileDescriptor(uri, "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
return image;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
3、最后上传图片是一个file文件,转换后拿到了file
//bitmap转换file
public static File getFile(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
File file = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");
try {
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
InputStream is = new ByteArrayInputStream(baos.toByteArray());
int x = 0;
byte[] b = new byte[1024 * 100];
while ((x = is.read(b)) != -1) {
fos.write(b, 0, x);
}
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return file;
}
我是天堂,祝你一分钟解决问题。