2015-06-23 65 views
8

我有一個問題,使用畢加索試圖從當地的Uri格式加載大圖像content://com.android.providers.media.documents/document/ imageXXYYZZ都來自Gallery和Camera Intent。畢加索無法加載大圖像(從相機和本地Uri)

我加載圖像與標準電話:

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)裝載大圖像,從相機的意圖說發生相機智能手機(在我的情況下,它是運行在Android 5.0.1上的Moto G)。我沒有得到這個錯誤在Android 4.4上使用三星S2。

任何幫助將非常感謝!由於

回答

-1

試試這個:

Uri uri = Uri.parse(imageUri); 
Picasso.with(context) 
    .load(uri) 
    .fit() 
    .resize(600, 240) 
    .skipMemoryCache() 
    .transform(new DocumentExifTransformation(this, uri)) 
    .into(imageView); 

如果我不工作到這裏看看https://github.com/square/picasso/issues/539

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; 
} 
+0

感謝您的回答,但使用這種方法畢加索不加載任何圖像。我想這個問題可能是因爲MediaStore.Images.Media.DATA應該根據我保存圖像的位置來設置? –

+0

正確。列名稱可能會有所不同。 – Moritz

+0

我應該看看哪一列? 我將圖像保存到'File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)'中,圖像類型爲'MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE' –

2

我花了很多時間搞清楚如何使用畢加索將大圖像加載到圖像視圖中。它正在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

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; 
}