2013-10-01 39 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」));