11

I'm trying to add an image to my twitter share intent. I save an image locally in one class and then in another I get the image and try to attach to my intent.

Here is my code

private void shareTwitter(){

    try {

        FileInputStream fis;
        fis = getActivity().openFileInput("photo.jpg");
        Bitmap shot = BitmapFactory.decodeStream(fis);
        File file = new File(MapView.path, "snapshot.jpg");
        if(file.exists()){
            Log.i("FILE", "YES");
        }else{
            Log.i("FILE", "NO");
        }
        Uri uri = Uri.parse(file.getAbsolutePath());
        //Uri uri = Uri.parse("android.resource://com.gobaby.app/drawable/back");             
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("/*");
            intent.setClassName("com.twitter.android", "com.twitter.android.PostActivity");
            intent.putExtra(Intent.EXTRA_TEXT, "Thiws is a share message");
            intent.putExtra(Intent.EXTRA_STREAM, uri);
            startActivity(intent);            

    } catch (final ActivityNotFoundException e) {
        Toast.makeText(getActivity(), "You don't seem to have twitter installed on this device", Toast.LENGTH_SHORT).show();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
}

At the moment there is no exception in my logcat my app just displays a toast saying image failed to load.

Please what an I doing wrong?

6
  • Run through a debug run step by step, and check your variables. Oct 1, 2013 at 15:34
  • which variable should I be checking? for starters is this the correct approach? I've checked to see if the file is found and it is.
    – Hugo Boss
    Oct 1, 2013 at 15:57
  • Have you been able to successfully save anything to twitter? Oct 1, 2013 at 16:02
  • 3
    as of May 2014 this approach is not working :(
    – agamov
    May 19, 2014 at 7:36
  • @agamov, do you know the solution since May 2014 ?
    – Seb83
    Jun 20, 2014 at 9:31

3 Answers 3

11

This is what you need

intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file);
1
  • 1
    i have already twitter app, then also it goes to catch "You don't seem to have twitter installed on this device" Aug 29, 2014 at 10:03
7

This might be helpful for somebody:

private void sendShareTwit() {
    try {
        Intent tweetIntent = new Intent(Intent.ACTION_SEND);

        String filename = "twitter_image.jpg";
        File imageFile = new File(Environment.getExternalStorageDirectory(), filename);

        tweetIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.twitter_share_text));
        tweetIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
        tweetIntent.setType("image/jpeg");
        PackageManager pm = getActivity().getPackageManager();
        List<ResolveInfo> lract = pm.queryIntentActivities(tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);
        boolean resolved = false;
        for (ResolveInfo ri : lract) {
            if (ri.activityInfo.name.contains("twitter")) {
                tweetIntent.setClassName(ri.activityInfo.packageName,
                        ri.activityInfo.name);
                resolved = true;
                break;
            }
        }

        startActivity(resolved ?
                tweetIntent :
                Intent.createChooser(tweetIntent, "Choose one"));
    } catch (final ActivityNotFoundException e) {
        Toast.makeText(getActivity(), "You don't seem to have twitter installed on this device", Toast.LENGTH_SHORT).show();
    }
}
0
0

Here is solution:

private fun shareOnTwitter() {
    val file = File(context!!.filesDir, FILENAME_SHARE_ON_TWITTER)
    val uriForFile = FileProvider.getUriForFile(context!!, com.yourpackage.activity.YourActivity, file)

    val intent = Intent(Intent.ACTION_SEND).apply {
        type = "image/jpeg"
        putExtra(Intent.EXTRA_STREAM, uriForFile)
    }
    startActivity(intent)
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

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