How to share http image directly to twitter in android?

2.2k Views Asked by At

So Far I searched in stackoverflow post and I can share the text directly to twitter without showing the popup dialog for share.That means when I click the button it is directly redirect to twitter app and shows the text.

My only issue is I have to share http image directly to twitter.

Below I have posted the code what I tried so far:

UsersAdapter.java:

// Create intent using ACTION_VIEW and a normal Twitter url:
        String tweetUrl = String.format("https://twitter.com/intent/tweet?text=%s&url=%s",
                urlEncode(strShareText), 
                urlEncode(strShareImageUrl));
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(tweetUrl));

        // Narrow down to official Twitter app, if available:
        List<ResolveInfo> matches = context.getPackageManager().queryIntentActivities(intent, 0);
        for (ResolveInfo info : matches) {
            if (info.activityInfo.packageName.toLowerCase().startsWith("com.twitter")) {
                intent.setPackage(info.activityInfo.packageName);
            }
        }

        context.startActivity(intent);

In that above code Text is showing correctly in twitter.But image is showing in http url.

Anyone know how to share the image directly to twitter app without showing link.

4

There are 4 best solutions below

0
On

You can use TwitterComposer dialog.

Here is sample code.

TweetComposer.Builder builder = new TweetComposer.Builder(mActivity)
                .text("hello text");
                .image(Uri.parse("https://media-mediatemple.netdna-ssl.com/wp-content/uploads/2016/01/07-responsive-image-example-castle-7-opt.jpg"));
builder.show();

Add dependencies in gradle file

compile('com.twitter.sdk.android:twitter:1.8.0@aar') {
        transitive = true;
}
0
On

This is what you need

intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file);
5
On

you can try this its work for me

The image Uri :

File myImageFile = new File("/path/to/image");
Uri myImageUri = Uri.fromFile(myImageFile);

Intent Builder

TweetComposer.Builder builder = new TweetComposer.Builder(this)
   .text("just setting up my Fabric.")
   .image(myImageUri);
builder.show();

Add this to your build.gradle dependencies :

dependencies {
 compile('com.twitter.sdk.android:tweet-composer:1.0.5@aar') {
    transitive = true;
 }
}
0
On

To Call :

new PostPicture(MyActivity.this).execute("IMAGE_URL");

Post picture :

private class PostPicture extends AsyncTask<String, Void, File> {
    private final Context context;

    public PostPicture(Context context) {
        this.context = context;
    }

    @Override
    protected File doInBackground(String... params) {

        FutureTarget<File> future = Glide.with(getApplicationContext())
                .load(params[0])
                .downloadOnly(600, 600);

        File file = null;
        try {
            file = future.get();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return file;
    }

    @Override
    protected void onPostExecute(File result) {
        if (result == null) {
            return ;
        }

        Uri uri = FileProvider.getUriForFile(getApplicationContext(), 
         getApplicationContext().getPackageName(), result);
        postIt(uri);
    }

    private void postIt(Uri result) {
        TweetComposer.Builder builder = new TweetComposer.Builder(this)
                .text("Post Image from URl.")
                .image(result);
        builder.show();
    }
}