2013-08-04 45 views
1

我想從我正在開發的應用程序發送圖像,我有下面的代碼,但堅持如何從我的/可繪製文件夾中的文件創建Uri,任何想法? (在這一點上很困惑)Android ShareActionProvider圖像問題

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    getMenuInflater().inflate(R.menu.second, menu); 
    MenuItem shareitem = menu.findItem(R.id.menu_item_share); 
    myShareActionProvider = (ShareActionProvider) shareitem.getActionProvider(); 
    myShareActionProvider.setShareIntent(createShareIntent()); 

    ActionBar actionBar = getActionBar(); 
    actionBar.show(); 
    return true; 

} 

public Intent createShareIntent() { 
    image = ??? 
    Uri uri = Uri.fromFile(image); 
    Intent shareIntent = new Intent(Intent.ACTION_SEND); 
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri); 
    shareIntent.setType("image/png"); 
    return shareIntent; 
} 

回答

1
private Intent createShareIntent() { 
     Intent shareIntent = new Intent(Intent.ACTION_SEND); 
     shareIntent.setType("image/*"); 
     Uri uri = Uri.parse("android.resource://your.package.name/" + R.drawable.image); 
     shareIntent.putExtra(Intent.EXTRA_STREAM, uri); 
     return shareIntent; 
    } 
+0

非常感謝,我只是想通了這一點,這個解決方案的工作雖然:) –

0

,並進一步我的問題,如果我有以下的(編號是根據被按下的按鈕變化的變量)。爲什麼以下不工作?

public Intent createShareIntent() { 
    Intent intent = getIntent(); 
    int number = intent.getIntExtra("BUTTON NUMBER", 1); 
    Uri imageUri = Uri.parse("android.resource://" + getPackageName() + "/"+ R.raw."BUTTON NUMBER"); 
    Intent shareIntent = new Intent(Intent.ACTION_SEND); 
    shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri); 
    shareIntent.setType("image/*"); 
    return shareIntent; 

} 
+1

這應該問作爲一個單獨的問題,因爲它不是一個答案,由你自己也承認。 – ianhanniballake

+0

對不起會問一個單獨的問題 –