Mostafa Addam
Mostafa Addam

Reputation: 7296

Image could not be loaded on twitter

I'm trying to share an image through twitter everything worked fine with facebook but twitter the image could not be loaded! I dont know why here is my code:

enter code here


public void share(String nameApp) 
{
    try
    {
        List<Intent> targetedShareIntents = new ArrayList<Intent>();
        Intent share = new Intent(android.content.Intent.ACTION_SEND);
        share.setType("image/jpg");
        List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
        if (!resInfo.isEmpty())
        {
            for (ResolveInfo info : resInfo) 
            {
                Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND);
                targetedShare.setType("image/jpg"); // put here your mime type
                if (info.activityInfo.packageName.toLowerCase().contains(nameApp) || info.activityInfo.name.toLowerCase().contains(nameApp)) {
                    targetedShare.putExtra(Intent.EXTRA_SUBJECT, "Sample Photo");
                 targetedShare.putExtra(Intent.EXTRA_TEXT,"This photo is created by Me");
                 ClassGlobal.selectedPagerURLs = new String[ClassGlobal.selectedAlbum.album_Thumbnail_Images.size()];
                //This path worked fine with facebook sdk but in twitter everything is fine except the image could not be loaded    
                    Uri screenshotUri = Uri.parse(ClassGlobal.selectedPagerURLs[currentItem]=ClassGlobal.selectedAlbum.album_Thumbnail_Images.get(currentItem).thumbnail_Url);

                    targetedShare.putExtra(Intent.EXTRA_STREAM,screenshotUri);
                    targetedShare.setPackage(info.activityInfo.packageName);
                    targetedShareIntents.add(targetedShare);
                }
            }
            Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
            startActivity(chooserIntent);
        }
    }
    catch(Exception e)
     {
         Log.v("VM","Exception while sending image on" + nameApp + " "+  e.getMessage());
     }
}

Any help would be appreciated Thank you

Upvotes: 0

Views: 3822

Answers (2)

Nikhil Borad
Nikhil Borad

Reputation: 2085

 Uri imagePathUri = Uri.parse("android.resource://com.example.test.social_integration/"+R.drawable.share);
            Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.share);
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            String path = MediaStore.Images.Media.insertImage(getContentResolver(), b, "Title", null);
            Uri imageUri = Uri.parse(path);
            Intent shareIntent = new Intent();
            shareIntent = new Intent(android.content.Intent.ACTION_SEND);
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_TEXT, "Test_Tweet");
            shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
            shareIntent.setType("image/jpeg");

            PackageManager pm = v.getContext().getPackageManager();
            List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
            for (final ResolveInfo app : activityList)
            {
                if (app.activityInfo.name.contains("twitter"))
                {
                    final ActivityInfo activity = app.activityInfo;
                    final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
                    shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                    shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                    shareIntent.setComponent(name);
                    v.getContext().startActivity(shareIntent);
                    break;
                }
            }


        }

Upvotes: 0

alisonc
alisonc

Reputation: 115

It seems to be a bug on the twitter app. I get the same error when trying to share an image from Gallery to Twitter on my Nexus 4 with Android 4.3. Weirdly enough, this worked on 4.2.2 before I ota upgraded today.

Upvotes: 2

Related Questions