0

I'm trying to share image on Instagram in my app. I've url of the image and using Picasso library to download image.

Target target = new Target() {
            @Override
            public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
                Log.d(TAG, "Bitmap Loaded");
                File outputDir = getApplicationContext().getCacheDir(); // context being the Activity pointer
                try {
                    File outputFile = File.createTempFile("instagram", "png", outputDir);
                    outputFile.createNewFile();
                    FileOutputStream ostream = new FileOutputStream(outputFile);
                    bitmap.compress(Bitmap.CompressFormat.JPEG,100,ostream);
                    ostream.close();
                    Log.d(TAG, "Image downloaded");
                    shareOnInstagram(Uri.fromFile(outputFile));
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {

            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {

            }
        };
        Picasso.with(this).load(imageUrl).into(target);

But onBitmapLoaded is never being called. Is there any other way to share image on Instagram from a url? The intent which share on Instagram takes Intent.EXTRA_STREAM parameter which should be a media path on device. How do I convert an image from a url into that type?

1

2 Answers 2

3

Picasso only keeps weak reference to target, so in your case it will be garbage collected. As a result, onBitmapLoaded is not being called.

You should store strong reference to target (make target member of your class).

0
1

I using this approach

public class ShareToOtherApp extends AsyncTask<Bitmap, Void, Uri> {


    @Override
    protected Uri doInBackground(Bitmap... bitmaps) {

        return bitmaps.length > 0 ? BitmaptoUri(bitmaps[0]) : null;
    }

    @Override
    protected void onPostExecute(Uri uri) {


        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);

        if (uri != null) {
            shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
        }

        shareIntent.setType("image/*");
        Intent chooserIntent = Intent.createChooser(shareIntent, "Share Image");
        chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        MyApp.GetContext().startActivity(chooserIntent);

    }

    public File GetSDCardDir(){
        boolean ISSDCard;
        File[] Dirs = ContextCompat.getExternalFilesDirs(MyApp.GetContext(), null);

        ISSDCard = false;
        for (File Dir : Dirs) {
            if (Dir != null) {
                if (Dir.getPath().contains("sdcard")) {
                    ISSDCard = true;
                    break;
                }
            }
        }

        File SDCardDir;
        if(ISSDCard && Dirs[Dirs.length -1] != null){
            SDCardDir = Dirs[Dirs.length -1];
        }else{
            SDCardDir = Dirs[0];
        }

        return SDCardDir;
    }

    public Uri BitmaptoUri(Bitmap bitmap){
        Uri uri = null;
        try {
            File file =  new File(GetSDCardDir() , HConstants.IMG_FILE_NAME + ".jpg");

            if(file.getParentFile() != null){
                file.getParentFile().mkdirs();
            }else{
                GetSDCardDir().mkdirs();
            }
            file.createNewFile();

            FileOutputStream out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.close();

            uri = Uri.fromFile(file);
        } catch (IOException e) {

            e.printStackTrace();
        }

        return uri;
    }
}

and finally for using from it.

new ShareToOtherApp().execute(bitmap);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.