How to share http image directly to twitter in android?
Asked Answered
M

4

6

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.

Mercola answered 17/2, 2016 at 12:7 Comment(4)
Why don't you use a Twitter API?Histone
the only embedded images with the web intent are the ones with the pic.twitter.com domains.Alveolate
@Alveolate can you please elaborate more.I am not get you.Mercola
@Naruto onlinejournalismblog.com/2015/02/11/…Alveolate
P
5

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;
}
Pamela answered 25/6, 2016 at 8:19 Comment(0)
O
1

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;
 }
}
Oleo answered 8/8, 2016 at 8:38 Comment(5)
you are showing image from file path right? I have to load http image from api.so it is not working with this codeMercola
I'm loading http image with Glide in local /path/to/image before use itOleo
Without getting image from filepath I have to share the image directly to twitter.is it possible to do?Mercola
look at the post below maybe what you need :)Oleo
Thanks for that answer. I will check that one and tell you later onMercola
B
0

This is what you need

intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file);
Bombe answered 8/8, 2016 at 6:13 Comment(0)
O
0

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();
    }
}
Oleo answered 9/8, 2016 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.