How can I access Picasso’s cached image to emphasize my intent?

I use Picasso to help image cache.

The question is, how can I access the uploaded image to make an intention to share?

any ideas? thanks!

+7
android picasso
source share
2 answers

I hope you understand my question :-)

Sorry for the delay, I found a solution, but not very good ...

First, I really searched for a while and looked at the Picasso code. It looks like you should provide your own bootloader and other stuff. But then why should I use lib ...

And then I suppose the Picasso project / architecture just caches the file in the internal storage. Perhaps because external storage is not always available (for example, a user can connect his SD card to his computer), or maybe because external storage is not as fast as internal ... This is my guess. In other words, other applications cannot access the internal storage of the current application, so this resource cannot be executed.

So I made a really normal decision. I just wait for Picasso to give Bitmap , and compress it to a file in an external file, and then make a share. This seems like a bad solution, but it really solves the problem, yes ...

You should know if the external cache directory is accessible or not. If not, you cannot do this promotion. And you need to put the compression task into the background thread, so waiting for an external file, cached ... It seems like a bad solution? I think so...

Below is my project code, you can try ...

 private boolean mSaved; // a flag, whether the image is saved in external storage private MenuItem mShare; private Intent mIntent; private ShareActionProvider mShareActionProvider; private File mImage; // the external image file would be saved... private Target target = new Target() { @Override public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) { new Thread(new Runnable() { @Override public void run() { FileOutputStream os = null; try { String dir = CatnutUtils.mkdir(getActivity(), Constants.FANTASY_DIR); // check the exteral dir avaiable or not... String[] paths = Uri.parse(mUrl).getPath().split("/"); mImage = new File(dir + File.separator + paths[2] + Constants.JPG); // resoleve the file name } catch (Exception e) { // the external storage not available... Log.e(TAG, "create dir error!", e); return; } try { if (mImage.length() > 10) { // > 0 means the file exists // the file exists, done. mIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mImage)); mSaved = true; return; } os = new FileOutputStream(mImage); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os); mIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mImage)); mSaved = true; } catch (FileNotFoundException e) { Log.e(TAG, "io error!", e); } finally { if (os != null) { try { os.close(); } catch (IOException e) { Log.e(TAG, "io closing error!", e); } } } } }).start(); mFantasy.setImageBitmap(bitmap); } @Override public void onBitmapFailed(Drawable errorDrawable) { mFantasy.setImageDrawable(errorDrawable); } @Override public void onPrepareLoad(Drawable placeHolderDrawable) { if (placeHolderDrawable != null) { mFantasy.setImageDrawable(placeHolderDrawable); } } }; @Override public void onPrepareOptionsMenu(Menu menu) { mShare.setEnabled(mSaved); } public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.fantasy, menu); mShare = menu.findItem(R.id.action_share); mShareActionProvider = (ShareActionProvider) mShare.getActionProvider(); mShare.setActionProvider(mShareActionProvider); mShareActionProvider.setShareIntent(mIntent); } 

Finally, call Picasso.with(getActivity()).load(mUrl).into(target);

When the file is saved, the user can click on the shared menu with the shared resource.

+3
source share

I just found this guide with a very good solution.

https://guides.codepath.com/android/Sharing-Content-with-Intents

The code will look like this:

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

This basically consists of extracting the bitmap from the image and saving it in a local temp file, and then using it for sharing. I tested it and it works fine.

0
source share

All Articles