图库中的分享

本文介绍如何在Android应用中实现分享功能,包括在AndroidManifest.xml中注册分享操作,并通过Intent接收从其他应用发送过来的数据,如文本或图片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Android系统的"图库"中,选择一张图片之后,点击"分享",可以添加自己的应用程序.

这一步要在AndroidManifest.xml中<intent-filter>中注册一个

<action android:name="android.intent.action.SEND">就可以了.

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值