现在的位置: 首页 > 综合 > 正文

在Android系统的”图库”中点击某张图片进行分享,在自己的应用程序中如何获取那张图片的路径?

2013年01月13日 ⁄ 综合 ⁄ 共 1561字 ⁄ 字号 评论关闭

第一步:

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

 

 

抱歉!评论已关闭.