Reputation: 586
I am using a custom camera and when the user clicks the camera button, it should call this method. The data
parameter is the image taken in bytes. I expect this method to make a bitmap out of the image, put it in a bundle then put that bundle in a fragment that will display the image. This is called from my 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();
}
In the onCreateView()
method inside the DisplayImageFragment
I get the bitmap from the bundle and try to load it using Picasso like so:
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);
}
}
});
For some reason, this doesn't display the image taken - instead, it displays a black ImageView. Does anyone know why this might be happening? Appreciate any efforts.
EDIT: if anyone's interested, the getImageUri()
method is here:
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);
}
I have also tried loading from file (new File("path")
) instead of directly passing in the Uri.
Upvotes: 0
Views: 150
Reputation: 7944
use something like this, If you are unable to load URI then make a file for the URI.
File f = new File("path-to-file/file.png")
or
File f = new File(uri)
Picasso.with(getActivity()).load(f).into(imageView);
Upvotes: 0