Share with ShareActionProvider by Picasso

i spand a few hours to find this solution ...

so I decided to share this information, maybe some of them will be useful :)

the first method , shown below, takes a bitmap from the view and loads it into a file.

// Get access to ImageView ImageView ivImage = (ImageView) findViewById(R.id.ivResult); // Fire async request to load image Picasso.with(context).load(imageUrl).into(ivImage); 

and then, assuming that after the image finishes loading, you can call the share:

 // Can be triggered by a view event such as a button press public void onShareItem(View v) { // Get access to bitmap image from view ImageView ivImage = (ImageView) findViewById(R.id.ivResult); // Get access to the URI for the bitmap Uri bmpUri = getLocalBitmapUri(ivImage); if (bmpUri != null) { // Construct a ShareIntent with link to image Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri); shareIntent.setType("image/*"); // Launch sharing dialog for image startActivity(Intent.createChooser(shareIntent, "Share Image")); } else { // ...sharing failed, handle error } } // Returns the URI path to the Bitmap displayed in specified ImageView public Uri getLocalBitmapUri(ImageView imageView) { // Extract Bitmap from ImageView drawable Drawable drawable = imageView.getDrawable(); Bitmap bmp = null; if (drawable instanceof BitmapDrawable){ bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap(); } else { return null; } // Store image to default external storage directory Uri bmpUri = null; try { File file = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png"); file.getParentFile().mkdirs(); FileOutputStream out = new FileOutputStream(file); bmp.compress(Bitmap.CompressFormat.PNG, 90, out); out.close(); bmpUri = Uri.fromFile(file); } catch (IOException e) { e.printStackTrace(); } return bmpUri; } 

Be sure to add the appropriate permissions for your AndroidManifest.xml:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
+6
source share
1 answer

The second way to share the image does not require you to write the image to a file. This code can be safely executed in the user interface thread. This approach has been proposed on this web page http://www.nurne.com/2012/07/android-how-to-attach-image-file-from.html .

 ImageView siv = (ImageView) findViewById(R.id.ivResult); Drawable mDrawable = siv.getDrawable(); Bitmap mBitmap = ((BitmapDrawable)mDrawable).getBitmap(); String path = Images.Media.insertImage(getContentResolver(), mBitmap, "Image Description", null); Uri uri = Uri.parse(path); return uri; 

You get Drawable from ImageView. You get a bitmap from Drawable. Put this bitmap in the media image store. This gives you a path that can be used instead of a file path or URL. Please note that the original web page has an additional problem with immutable bitmaps, which are solved by drawing a bitmap into a canvas (never displayed on the screen). See the linked page above for more details.

+4
source

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


All Articles