添加图像到twitter分享意图android

我试图添加一个图像到我的twitter共享意图。 我在一个class上在本地保存图像,然后在另一个图像上获得图像,并尝试附加到我的意图。

这是我的代码

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(); } } 

目前在我的logcat中没有例外我的应用程序只显示吐司说图像加载失败。

请问我做错了什么?

Solutions Collecting From Web of "添加图像到twitter分享意图android"

这是你需要的

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

这可能对某人有帮助:

 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(); } } 

你可以在这里下载完整的源代码

 Bitmap b =BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher); Intent share = new Intent(Intent.ACTION_SEND); share.setType(“image/jpeg”); 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); share.putExtra(Intent.EXTRA_STREAM, imageUri); startActivity(Intent.createChooser(share, “Select”));