用户
 找回密码
 立即注册

2

主题

4

帖子

96

积分

注册会员

Rank: 2

积分
96
发表于 2021-12-2 14:12:23
应用场景:比如一般查看图片都有一个分享(安卓系统内置)功能,把你们app加入这个列表,点击分享时,可在app内部接收图片路径并上传到服务器,很常见的需要,希望管理员能增加进新版本,谢谢!
配置清单
  1.     <application
  2.         android:allowBackup="true"
  3.         android:icon="@drawable/ic_launcher"
  4.         android:label="@string/app_name"
  5.         android:theme="@style/AppTheme" >
  6.         <activity
  7.             android:name="com.example.share.MainActivity"
  8.             android:label="@string/app_name" >
  9.             <intent-filter>
  10.                 <action android:name="android.intent.action.MAIN" />

  11.                 <category android:name="android.intent.category.LAUNCHER" />
  12.             </intent-filter>
  13.             
  14.             <intent-filter>
  15.                                        <action android:name="android.intent.action.SEND"/>
  16.                                         <category android:name="android.intent.category.DEFAULT"/>
  17.                                                 <!--         指定分享类型,我这边只能分享video/mp4,image/* -->
  18.                                     <data android:mimeType="video/mp4"/>
  19.                         <data android:mimeType="image/*" />
  20.                             </intent-filter>  
  21.         </activity>
  22.     </application>
复制代码
接收图片路径
  1. public class MainActivity extends Activity {
  2.         @Override
  3.         protected void onCreate(Bundle savedInstanceState) {
  4.             super.onCreate(savedInstanceState);
  5.             setContentView(R.layout.activity_main);
  6.                
  7.             TextView pathTextView=(TextView) findViewById(R.id.path);
  8.                
  9.             Intent intent = getIntent();
  10.             String action = intent.getAction();//action
  11.             String type = intent.getType();//类型
  12.         
  13.             //类型
  14.             if (Intent.ACTION_SEND.equals(action) && type != null /*&& "video/mp4".equals(type)*/) {
  15.                 Uri uri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
  16.                 //如果是媒体类型需要从数据库获取路径
  17.                 String filePath=getRealPathFromURI(uri);
  18.                 pathTextView.setText("文件路径:"+filePath);
  19.             }
  20.         }
  21.        
  22.         /**
  23.          * 通过Uri获取文件在本地存储的真实路径
  24.          */
  25.         private String getRealPathFromURI(Uri contentUri) {
  26.                 String[] proj = {MediaStore.MediaColumns.DATA};
  27.                 Cursor cursor=getContentResolver().query(contentUri, proj, null, null, null);
  28.                 if(cursor.moveToNext()){
  29.                         return cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA));
  30.                 }
  31.                 cursor.close();
  32.                 return null;
  33.         }
  34. }
复制代码


分享至 : QQ空间
0 人收藏
使用道具 举报 回复
发新帖
您需要登录后才可以回帖 登录 | 立即注册