2013-10-01 37 views
8

我试图将图像添加到我的Twitter共享意图。我在一个班级中本地保存图像,然后在另一个图像中获取图像并尝试附加到我的意图。将图像添加到Twitter共享意图Android

这里是我的代码

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也不例外我的应用程序只显示举杯说图像加载失败。

请问我做错了什么?

+0

运行通过'调试run'一步一步,并检查你的变量。 –

+0

我应该检查哪个变量?对于初学者来说这是正确的做法吗?我检查过是否找到了该文件。 –

+0

你能成功保存任何内容到Twitter吗? –

回答

10

这是你所需要的

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

非常感谢,现在正常工作 –

+1

我已经推出了twitter应用程序,然后它也去捕捉**“你似乎没有安装在这个设备上的twitter”** –

7

这可能对别人有所帮助:

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

weldone @agamov它工作正常 –

1

您可以下载完整的源代码here

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”));