Android 位图在以前工作的代码中变为空
Android Bitmap becomes null in previously working code
四年前,我开始开发一个允许用户选择图像的应用程序,然后该应用程序为图像制作动画。我在 Eclipse 中完成了工作,我 100% 确定该应用程序可以正常工作。
快进到 2018 年,我还有旧的源代码,我想改进它。我将它导入 Android Studio,它仍然可以构建和运行。
但是,从那时起 Android 的工作方式肯定发生了变化,因为图像现在变成了空的。
在调试中,我发现 selectedImagePath
是计算出来的,它不是空的,因此我认为它是正确的。
模拟器中 运行 时的一个示例:/document/primary:Download/x.gif
(当然该图像存在)。
运行 在 phone 上的一个示例:/document/image:2375
(令人困惑的是,它实际上称为 x.png
并且位于标准下载文件夹中)。
不过,bitmap
始终为空。有什么想法吗?
Uri selectedImageUri;
selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath);
public String getPath(Uri uri) {
String selectedImagePath;
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if(cursor != null){
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
selectedImagePath = cursor.getString(column_index);
}else{
selectedImagePath = null;
}
if(selectedImagePath == null){
//2:OI FILE Manager --- call method: uri.getPath()
selectedImagePath = uri.getPath();
}
return selectedImagePath;
}
Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath);
改为
InputStream is = getContentResolver().openInputStream(data.getData());
Bitmap bitmap = BitmapFactory.decodeStream(is);
尝试检查您的 uri 是否从 file://
开始
private String getImagePathFromUri(Uri imageUri) {
if (imageUri != null) {
if (imageUri.toString().startsWith("file://")) {
return imageUri.getEncodedPath();
} else {
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(imageUri,
filePathColumn, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String imageEncoded = null;
try {
imageEncoded = cursor.getString(columnIndex);
} catch (IllegalStateException e) {
e.printStackTrace();
}
return imageEncoded;
}
cursor.close();
}
}
}
return null;
}
四年前,我开始开发一个允许用户选择图像的应用程序,然后该应用程序为图像制作动画。我在 Eclipse 中完成了工作,我 100% 确定该应用程序可以正常工作。
快进到 2018 年,我还有旧的源代码,我想改进它。我将它导入 Android Studio,它仍然可以构建和运行。
但是,从那时起 Android 的工作方式肯定发生了变化,因为图像现在变成了空的。
在调试中,我发现 selectedImagePath
是计算出来的,它不是空的,因此我认为它是正确的。
模拟器中 运行 时的一个示例:/document/primary:Download/x.gif
(当然该图像存在)。
运行 在 phone 上的一个示例:/document/image:2375
(令人困惑的是,它实际上称为 x.png
并且位于标准下载文件夹中)。
不过,bitmap
始终为空。有什么想法吗?
Uri selectedImageUri;
selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath);
public String getPath(Uri uri) {
String selectedImagePath;
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if(cursor != null){
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
selectedImagePath = cursor.getString(column_index);
}else{
selectedImagePath = null;
}
if(selectedImagePath == null){
//2:OI FILE Manager --- call method: uri.getPath()
selectedImagePath = uri.getPath();
}
return selectedImagePath;
}
Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath);
改为
InputStream is = getContentResolver().openInputStream(data.getData());
Bitmap bitmap = BitmapFactory.decodeStream(is);
尝试检查您的 uri 是否从 file://
开始private String getImagePathFromUri(Uri imageUri) {
if (imageUri != null) {
if (imageUri.toString().startsWith("file://")) {
return imageUri.getEncodedPath();
} else {
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(imageUri,
filePathColumn, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String imageEncoded = null;
try {
imageEncoded = cursor.getString(columnIndex);
} catch (IllegalStateException e) {
e.printStackTrace();
}
return imageEncoded;
}
cursor.close();
}
}
}
return null;
}