|
应用场景:比如一般查看图片都有一个分享(安卓系统内置)功能,把你们app加入这个列表,点击分享时,可在app内部接收图片路径并上传到服务器,很常见的需要,希望管理员能增加进新版本,谢谢!
配置清单
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.example.share.MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
-
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
-
- <intent-filter>
- <action android:name="android.intent.action.SEND"/>
- <category android:name="android.intent.category.DEFAULT"/>
- <!-- 指定分享类型,我这边只能分享video/mp4,image/* -->
- <data android:mimeType="video/mp4"/>
- <data android:mimeType="image/*" />
- </intent-filter>
- </activity>
- </application>
复制代码 接收图片路径
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- TextView pathTextView=(TextView) findViewById(R.id.path);
-
- Intent intent = getIntent();
- String action = intent.getAction();//action
- String type = intent.getType();//类型
-
- //类型
- if (Intent.ACTION_SEND.equals(action) && type != null /*&& "video/mp4".equals(type)*/) {
- Uri uri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
- //如果是媒体类型需要从数据库获取路径
- String filePath=getRealPathFromURI(uri);
- pathTextView.setText("文件路径:"+filePath);
- }
- }
-
- /**
- * 通过Uri获取文件在本地存储的真实路径
- */
- private String getRealPathFromURI(Uri contentUri) {
- String[] proj = {MediaStore.MediaColumns.DATA};
- Cursor cursor=getContentResolver().query(contentUri, proj, null, null, null);
- if(cursor.moveToNext()){
- return cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA));
- }
- cursor.close();
- return null;
- }
- }
复制代码
|
|