Search code examples
androidtwittersharepicasso

In Android how can i share an http image to twitter as a tweet from my application


I have gone through many articles. But somehow i could not achieve what i wanted to achieve. I have a http image link in my app and i want to share this as an image attachment from Twitter application. I have tried following. But it seems not to be working as Twitter expects it to be a local url.

TweetComposer.Builder builder = new TweetComposer.Builder(context)
                                    .text(text)
                                    .image(Uri.parse("https://dummyimage.com/300/09f/fff.png"));
                            builder.show();

So now that i know it requires a local path, i tried to download the image to phone using Picasso as below.

Picasso.get().load(CONSTANT.IAMGE_URI + list.get(position).getEvent_model().getPhoto_link())
                        .placeholder(R.drawable.placeholder)
                        .error(R.drawable.placeholder)
                        .into(getTarget(list.get(position).getEvent_model().getPhoto_link()));

                File myImageFile = new File(getFileFullPath(list.get(position).getEvent_model().getPhoto_link()));
                Picasso.get().load(myImageFile).into(holder.iv_album_image);

And this is code to get the target.

private static Target getTarget(final String fileName) {
    Target target = new Target() {
        //This method in target is called by picasso when image downloaded
        @Override
        public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        File file = new File(getFilename(fileName));
                        if (file.exists()) {
                            file.delete();
                        }
                        file.createNewFile();
                        FileOutputStream fileoutputstream = new FileOutputStream(file);
                        ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.PNG, 60, bytearrayoutputstream);
                        fileoutputstream.write(bytearrayoutputstream.toByteArray());
                        fileoutputstream.close();
                    } catch (IOException e) {
                        Log.e("IOException", e.getLocalizedMessage());
                    }
                }
            }).start();

        }

        @Override
        public void onBitmapFailed(Exception e, Drawable errorDrawable) {

        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {

        }
    };
    return target;
}

public static String getFilename(String fileName) {
    File file = new File(Environment.getExternalStorageDirectory().getPath(), "Images");
    if (!file.exists()) {
        file.mkdirs();
    }
    return (file.getAbsolutePath() + "/" + fileName);
}

In this case photo is not loading in to imageview and also not being downloaded to local.


Solution

  • You need to download the image to your phone, and use a local path.

    https://docs.fabric.io/javadocs/tweet-composer/2.0.0/com/twitter/sdk/android/tweetcomposer/TweetComposer.Builder.html#image(android.net.Uri)

    Ultimately for image: The Uri should be a file Uri to a local file

    Uri.fromFile(someExternalStorageFile)
    

    Can you change your getFilename to match as such:

    public static String getFilename(String fileName, Context context) {
        int code = context.getPackageManager().checkPermission(
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                context.getPackageName());
        if (code == PackageManager.PERMISSION_GRANTED) {
            File file = new File(Environment.getExternalStorageDirectory().getPath(), "Images");
            if (!file.exists()) {
                file.mkdirs();
            }
            return (file.getAbsolutePath() + "/" + fileName);
        }
    
        return "";
    }
    

    And set a breakpoint at each return. My assumption is you're returning "";