从其他应用程序接收到的简单图像数据的分辨率小于或等于0
我跟着Google's guide Receiving Simple Data from Other Apps,我设法按照承诺获得uri,当共享图像时显示我的应用程序图标。从其他应用程序接收到的简单图像数据的分辨率小于或等于0
然而,有一个具体的问题,这是怎么一回事呢:
if (Intent.ACTION_SEND.equals(action) && type != null) {
if (type.startsWith("image/")) {
handleSendImage(intent); // Handle single image being sent
}
}
handleSendImage(intent);
导致
void handleSendImage(Intent intent) {
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
pfadPhoto = getRealPathFromURI_API19(this, imageUri);
i("handleSendImage "+pfadPhoto);
fBildVerkleinern(pfadPhoto);
}
}
public String getRealPathFromURI_API19(Context context, Uri uri){
String result;
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor == null) { // Source is Dropbox or other similar local file path
result = uri.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(idx);
cursor.close();
}
return result;
}
fBildVerkleinern(pfadPhoto);
导致
private void fBildVerkleinern(String bildpfad) {
...
iv = (ImageView)findViewById(R.id.mImageView);
int ivBreite = iv.getWidth();
int ivHoehe = iv.getHeight();
...
v = BitmapFactory.decodeFile(bildpfad); //bildpfad = pfadPhoto
...
bitmap2 = getResizedBitmap(v,(int)((float)ivBreite/vergroesserungsFaktor),ivBreite);
...
}
getResizedBitmap
导致
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
if (bm != null) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth)/width;
float scaleHeight = ((float) newHeight)/height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
return null;
}
和Bitmap.createBitmap
导致
Caused by: java.lang.IllegalArgumentException: width and height must be > 0
但我也有一个onActivityResult
是运行图像选择发生后,它看起来像
if (requestCode == CHOOSE_PHOTO) {
if (data == null) {
f("Fehler");
return;
}
pfadPhoto = getRealPathFromURI_API19(this, data.getData());
fBildVerkleinern(pfadPhoto);
...
}
文件管理器和这工作没有问题。
你从哪里看到错误?
我跟着谷歌指南从其他应用程序
没有接收简单的数据,你没有。该指南中没有getRealPathFromURI_API19()
方法。这是一个很好的理由:它一般不起作用。
所以,删除它。
取而代之的是,使用您最喜欢的图像加载库从Uri
加载图像。或者,在最坏的情况下,使用getContentResolver().openInputStream()
获取由Uri
标识的内容的InputStream
,然后将该流传递给BitmapFactory.decodeStream()
。只需在后台线程上执行此I/O操作(其中包括哪些映像加载库将为您处理,以及其他优点)。
,这工作不会有问题
那是因为你没有测试它非常。
我不太确定“用文件管理器进行图片选择”的含义。无论如何,你的破getRealPathFromURI_API19()
假设之一:
如果
query()
的Uri
并获得null
回来,然后getPath()
是一个文件系统路径。对此没有要求。例如,Uri
可能由ContentProvider
提供服务,但不提供query()
。或者,它可能是一个android.resource
方案(各种开发人员尝试)。这两个代表发件人的错误,但是你的应用程序仍然会失败。如果
query()
的Uri
并获得Cursor
回首,那光标将有MediaStore.Images.ImageColumns.DATA
列。对此没有要求。例如,FileProvider
不这样做。如果
query()
的Uri
并获得Cursor
回来,有一个非null
DATA
列,它代表的是你可以使用一个文件系统路径。对此没有要求。例如,路径可能是可移动存储上的文件,您无法直接通过文件系统访问该文件。
幸运的是,使用openInputStream()
解决了所有这些,如使用维护良好的图像加载库。