[英]Picasso can't load bitmap from Uri - ImageView shows up as a black view
我使用的是自定義相機,當用戶單擊相機按鈕時,它應調用此方法。 data
參數是以字節為單位拍攝的圖像。 我希望此方法可以從圖像中生成位圖,將其放入捆綁包中,然后將該捆綁包放入將顯示圖像的片段中。 這是從我的Camera.PictureCallback
。
public static void displayCameraImage(byte[] data) {
BitmapFactory.Options scalingOptions = new BitmapFactory.Options();
final Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length, scalingOptions);
FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction();
Fragment displayImageFragment = new DisplayImageFragment();
Bundle bundle = new Bundle();
bundle.putParcelable("image", bmp);
displayImageFragment.setArguments(bundle);
fragmentTransaction.add(R.id.flMainContainer, displayImageFragment, "confirmselfie");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
在DisplayImageFragment
內部的onCreateView()
方法中,我從包中獲取了位圖,然后嘗試使用Picasso加載位圖,如下所示:
Bitmap bmp = getArguments().getParcelable("image");
final Uri uri = getImageUri(getContext(), bmp);
Picasso.with(getActivity()).load(uri).into(ivImage, new ImageLoadedCallback(progressBar) {
@Override
public void onSuccess() {
if (this.progressBar != null) {
this.progressBar.setVisibility(View.GONE);
}
}
});
由於某些原因,它不會顯示所拍攝的圖像-而是顯示黑色的ImageView。 有誰知道為什么會這樣? 感謝任何努力。
編輯:如果有人感興趣,則getImageUri()
方法在這里:
public Uri getImageUri(Context context, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), inImage, "confirmSelfie", null);
return Uri.parse(path);
}
我也嘗試過從文件( new File("path")
)加載,而不是直接傳遞Uri。
使用類似的方法,如果您無法加載URI,請為URI創建一個文件。
File f = new File("path-to-file/file.png")
要么
File f = new File(uri)
Picasso.with(getActivity()).load(f).into(imageView);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.