2013-10-29 77 views
6

如何獲取ShareActionProvider菜單項的onClick事件?Handle ShareActionProvider onClick事件

我的應用程序顯示了圖庫中的一些圖像(帶有imageView的片段),我需要獲取onClick事件以從緩存中加載當前圖像,生成臨時文件並共享它。

我的分享按鈕:

<item android:id="@+id/menu_item_share" 
    android:showAsAction="ifRoom" 
    android:title="@string/menu_share" 
    android:actionProviderClass="android.widget.ShareActionProvider" /> 

我已經試過:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case android.R.id.home: 
     NavUtils.navigateUpFromSameTask(this); 
     return true; 
    case R.id.manage_keywords: 
     startActivity(new Intent(this, KeywordActivity.class)); 
     return true; 
    case R.id.menu_item_share: 
     /*it should handle share option*/ 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

而且

menuShare = menu.findItem(R.id.menu_item_share); 
    menuShare.setOnActionExpandListener(new OnActionExpandListener() { 

     @Override 
     public boolean onMenuItemActionExpand(MenuItem item) { 
      // TODO Auto-generated method stub 
      return false; 
     } 

     @Override 
     public boolean onMenuItemActionCollapse(MenuItem item) { 
      // TODO Auto-generated method stub 
      return false; 
     } 
    }); 

任何其他建議? 謝謝!

回答

0

我沒有檢查,但我會嘗試OnMenuItemClickListener事件,而不是

+0

它沒有工作... menuShare = menu.findItem(R.id.menu_item_share); \t \t menuShare.setOnMenuItemClickListener(新OnMenuItemClickListener(){ \t \t \t \t \t \t @覆蓋 \t \t \t公共布爾onMenuItemClick(菜單項項){ \t \t \t \t/*應該停在這裏*/ \t \t \t \t return false; \t \t \t} \t \t}); –

1

你提出的流動不被ShareActionProvider支持。您需要分叉或者想出其他共享解決方案。

或者,更好,實現ContentProvider是能容納的形象,這樣你就可以在第一時間使用適當的IntentShareActionProvider,而不是需要基於用戶選擇的共享目標去改變它。通過使用你自己的ContentProvider,你完全可以避免臨時文件。取決於你的圖像存儲在哪裏,FileProvider may be sufficient for your needs

0

您可以使用OnShareTargetSelectedListenener的ShareActionProvider ,然後在那裏添加圖片到你的意圖 (你不能修改這裏你itent):

mShareActionProvider = (ShareActionProvider) menuItemShare.getActionProvider(); 
mShareActionProvider.setOnShareTargetSelectedListener(new OnShareTargetSelectedListener() { 
     @Override 
     public boolean onShareTargetSelected(ShareActionProvider actionProvider, Intent intent) { 
      return false; 
     } 
    }); 

所以其他的解決辦法是,那

mShareActionProvider.setShareIntent(createIntentForCurrentImage()); 
8

保存你想每次都分享到SD卡上ShareActionProvider用戶點擊圖片,完整的例子:當你改變當前圖像更新你的意圖

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getSupportMenuInflater().inflate(R.menu.menu, menu); 

    ShareActionProvider shareActionProvider = (ShareActionProvider) menu.findItem(R.id.shareactionprovider).getActionProvider(); 
    shareActionProvider.setShareIntent(getShareIntent()); 
    shareActionProvider.setOnShareTargetSelectedListener(new OnShareTargetSelectedListener() { 
     @Override 
     public boolean onShareTargetSelected(ShareActionProvider actionProvider, Intent intent) { 
      saveImageToSD(); 
      return false; 
     } 
    }); 

    return true; 
} 




private Intent getShareIntent() { 
    Intent shareIntent = new Intent(Intent.ACTION_SEND); 

    File sdCard = Environment.getExternalStorageDirectory(); 

    File sharedFile = new File(sdCard+"/yourPath/yourImage.jpg"); 
    Uri uri = Uri.fromFile(sharedFile); 

    shareIntent.setType("image/*"); 
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri); 
    return shareIntent; 
} 


private void saveImageToSD() { 

    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.yourimage); 

    OutputStream outStream = null; 
    try { 
     outStream = new FileOutputStream(getTempFile()); 
     bm.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
     outStream.flush(); 
     outStream.close(); 
     bm.recycle(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

private File getTempFile() { 

    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 

     File directory = new File(Environment.getExternalStorageDirectory() + "/yourPath/"); 
     directory.mkdirs(); 

     File file = new File(directory ,"yourImage.jpg"); 

     try { 
      file.createNewFile(); 
     } catch (IOException e) {} 

     return file; 
    } else { 
     return null; 
    } 
} 
+1

這是有效的! –