使用Picasso尝试从Gallery和Camera Intent中的本地Uri格式 content://com.android.providers.media.documents/document/imageXXYYZZ 加载大图像时出现问题
我正在使用标准调用加载图像:
Picasso.load(image_url)
.resize(600, 240)
.centerCrop()
.into(imageTarget);
我附加了Target
,当我收到onBitmapFailed(Drawable errorDrawable)
错误时。另外,当我登录毕加索时,我得到了:
06-23 12:13:54.267 22393-22393/it.b3lab.friendipity D/Picasso﹕ Main created [R100] Request{content://com.android.providers.media.documents/document/image%3A13345 resize(600,240) centerCrop}
06-23 12:13:54.277 22393-23010/it.b3lab.friendipity D/Picasso﹕ Dispatcher enqueued [R100]+9ms
06-23 12:13:54.285 22393-23038/it.b3lab.friendipity D/Picasso﹕ Hunter executing [R100]+15ms
06-23 12:13:54.813 22393-23010/it.b3lab.friendipity D/Picasso﹕ Dispatcher batched [R100]+546ms for error
06-23 12:13:55.014 22393-23010/it.b3lab.friendipity D/Picasso﹕ Dispatcher delivered [R100]+746ms
06-23 12:13:55.024 22393-22393/it.b3lab.friendipity I/picasso﹕ failed to load bitmap
06-23 12:13:55.024 22393-22393/it.b3lab.friendipity D/Picasso﹕ Main errored [R100]+756ms
这种情况只发生在我上面说的当我尝试从图库中加载大图像(大约1 MB以上)和使用高分辨率相机智能手机时的相机意图(在我的情况下它是' Moto G在Android 5.0.1上运行)。我在Android 4.4上使用Samsung S2没有收到此错误。
任何帮助都会非常感激!感谢
答案 0 :(得分:3)
您需要将内容uri解析为绝对的uri。像这样:
public String getAbsolutePathFromURI( Uri contentUri ) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = activity.getContentResolver().query( contentUri, proj, null, null, null );
int columnIndex = cursor.getColumnIndexOrThrow( MediaStore.Images.Media.DATA );
cursor.moveToFirst();
String path = cursor.getString( columnIndex );
cursor.close();
return path;
}
答案 1 :(得分:2)
我花了很多时间弄清楚如何使用Picasso将大图像加载到图像视图中。它正在研究genymotion,但没有在我的moto x上工作。所以我最终决定在异步任务中手动调整图像大小,然后将其加载到图像视图中,而不使用Picasso 。
new AsyncTask<String, Void, Void>() {
@Override
protected Void doInBackground(String... params) {
String path = params[0];
final Bitmap resizedBitmap = ImageUtils.getResizedBitmap(200, 200, PATH_TO_IMAGE);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(resizedBitmap);
}
});
return null;
}
}.execute(imageLoadPath);
您可以找到ImageUtils.getResizedBitmap()
方法here
答案 2 :(得分:0)
毕加索失败但这完美无缺
public static void resizeImage(String file, int maxTargetWidth, int maxTargetHeight) {
try {
InputStream in = new FileInputStream(file);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, options);
close(in);
int inWidth = options.outWidth;
int inHeight = options.outHeight;
in = new FileInputStream(file);
options = new BitmapFactory.Options();
options.inSampleSize = Math.max(inWidth / maxTargetWidth, inHeight / maxTargetHeight);
Bitmap roughBitmap = BitmapFactory.decodeStream(in, null, options);
Matrix m = new Matrix();
RectF inRect = new RectF(0, 0, roughBitmap.getWidth(), roughBitmap.getHeight());
RectF outRect = new RectF(0, 0, maxTargetWidth, maxTargetHeight);
m.setRectToRect(inRect, outRect, CENTER);
float[] values = new float[9];
m.getValues(values);
Bitmap resizedBitmap = createScaledBitmap(roughBitmap,
(int) (roughBitmap.getWidth() * values[0]), (int) (roughBitmap.getHeight() * values[4]),
true);
resizedBitmap = rotateBitmap(file, resizedBitmap);
FileOutputStream out = new FileOutputStream(file);
resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
close(out);
} catch (Exception e) {
error(e);
}
}
private static Bitmap rotateBitmap(String src, Bitmap bitmap) {
try {
int orientation = new ExifInterface(src).getAttributeInt(TAG_ORIENTATION, 1);
Matrix matrix = new Matrix();
if (orientation == ORIENTATION_NORMAL) return bitmap;
if (orientation == ORIENTATION_FLIP_HORIZONTAL) matrix.setScale(-1, 1);
else if (orientation == ORIENTATION_ROTATE_180) matrix.setRotate(180);
else if (orientation == ORIENTATION_FLIP_VERTICAL) {
matrix.setRotate(180);
matrix.postScale(-1, 1);
} else if (orientation == ORIENTATION_TRANSPOSE) {
matrix.setRotate(90);
matrix.postScale(-1, 1);
} else if (orientation == ORIENTATION_ROTATE_90) {
matrix.setRotate(90);
} else if (orientation == ORIENTATION_TRANSVERSE) {
matrix.setRotate(-90);
matrix.postScale(-1, 1);
} else if (orientation == ORIENTATION_ROTATE_270) {
matrix.setRotate(-90);
} else return bitmap;
try {
Bitmap oriented = createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), matrix, true);
bitmap.recycle();
return oriented;
} catch (OutOfMemoryError e) {
error(e);
}
} catch (IOException e) {
error(e);
}
return bitmap;
}
答案 3 :(得分:-1)
试试这个:
Uri uri = Uri.parse(imageUri);
Picasso.with(context)
.load(uri)
.fit()
.resize(600, 240)
.skipMemoryCache()
.transform(new DocumentExifTransformation(this, uri))
.into(imageView);