How to upload a bitmap directly using picasso library as shown below.

Picasso.with(context).load("url").into(imageView); 

Here, instead of url, I want a bitmap, how can I achieve this. as below -

 Picasso.with(context).load(bitmap).into(imageView); 
+12
source share
3 answers

This should work for you. Use the returned URI with Picasso.

(taken from: is it possible to get the uri of a bitmap without saving it to an SDCard? )

 public Uri getImageUri(Context inContext, Bitmap inImage) { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes); String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null); return Uri.parse(path); } 
+10
source

You are using an old version of Picasso.

Update the version in your Gradle file:

 implementation 'com.squareup.picasso:picasso:2.71828' 

Java:

 Picasso.get().load(R.drawable.landing_screen).into(imageView1); Picasso.get().load("file:///android_asset/DvpvklR.png").into(imageView2); Picasso.get().load(new File(...)).into(imageView3); 

See details on the Picasso website

+1
source

My kotlin solution

create bitmap from data

  val inputStream = getContentResolver().openInputStream(data.data) val bitmap = BitmapFactory.decodeStream(inputStream) val stream = ByteArrayOutputStream() bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream) 

IMPORTANT: if you do not need to store the image, you can avoid Picasso and upload the image immediately

  imageView.setImageBitmap(bitmap) 

otherwise save the file and upload it using Picasso

  val jpegData = stream.toByteArray() val file = File(cacheDir, "filename.jpg") file.createNewFile() val fileOS = FileOutputStream(file) fileOS.write(jpegData) fileOS.flush() fileOS.close() Picasso.get().load(Uri.parse(file.path)).into(imageView) 
+1
source

Source: https://habr.com/ru/post/1239973/


All Articles