在activity里: 如果是文本的话String shareText = getIntent().getStringExtra(Intent.EXTRA_TEXT); 这样应该可以分享的内容。 图片应该是用Intent.EXTRA_STREAM,这样理论上获取到的是图片的uri。
String shareText = getIntent().getStringExtra(Intent.EXTRA_TEXT);
第一步:
<activity android:name=".activity.ContactGroupShareActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:label="@string/app_name"> <intent-filter> <!--调用共享时,过滤共享文件,此处默认全部 --> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" />
<!-- 允许所有类型文件--> <data android:mimeType="*/*" /> </intent-filter> </activity>
第二步:
/** * 其他应用调用本分享功能 */ private void shareFormOtherProg() { /*比如通过Gallery方式来调用本分享功能*/ Intent intent = getIntent(); Bundle extras = intent.getExtras(); String action = intent.getAction(); if (Intent.ACTION_SEND.equals(action)) { if (extras.containsKey(Intent.EXTRA_STREAM)) { try { // Get resource path from intent Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
// 返回路径 String path = csc.getRealPathFromURI(this, uri); System.out.println("path-->" + path); return; } catch (Exception e) { Log.e(this.getClass().getName(), e.toString()); } } else if (extras.containsKey(Intent.EXTRA_TEXT)) { return; } }
}
/** * 通过Uri获取文件在本地存储的真实路径 * @param act * @param contentUri * @return */ public String getRealPathFromURI(Activity act, Uri contentUri) { // can post image String[] proj = { MediaStore.Images.Media.DATA }; Cursor cursor = act.managedQuery(contentUri, proj, // Which columns to return null, // WHERE clause; which rows to return (all rows) null, // WHERE clause selection arguments (none) null); // Order-by clause (ascending by name) int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); }
|