android 调用微信QQ的其他应用打开列表中添加自己的应用,并且获取uri
清单文件配置指定的activity配置如下:
<!--调用微信的其他应用打开列表添加自己应用 --> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="content"/> <!-- 允许所有类型文件--> <data android:mimeType="*/*" /> <!-- 只允许图片文件--> <!--<data android:mimeType="image/*"/>--> </intent-filter> <!--调用QQ的其他应用打开列表添加自己应用 --> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="file"/> <!-- 允许所有类型文件--> <data android:mimeType="*/*" /> <!-- 只允许图片文件--> <!--<data android:mimeType="image/*"/>--> </intent-filter>
获取uri
//获取uri Intent intent = getIntent(); Uri uri = intent.getData(); String url = intent.getDataString(); Log.e("url", url);
将uri转为文件路径
/** * 根据Uri获取文件的绝对路径,解决Android4.4以上版本Uri转换 * * @param fileUri */ @TargetApi(19) public static String getPathFormUri(Activity context, Uri fileUri) { if (context == null || fileUri == null) return null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(context, fileUri)) { if (isExternalStorageDocument(fileUri)) { String docId = DocumentsContract.getDocumentId(fileUri); String[] split = docId.split(":"); String type = split[0]; if ("primary".equalsIgnoreCase(type)) { return Environment.getExternalStorageDirectory() + "/" + split[1]; } } else if (isDownloadsDocument(fileUri)) { String id = DocumentsContract.getDocumentId(fileUri); try { long ID = Long.parseLong(id); Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), ID); return getDataColumn(context, contentUri, null, null); } catch (Exception e) { if (id.startsWith("raw:")) { final String path = id.replaceFirst("raw:", ""); return path; } } } else if (isMediaDocument(fileUri)) { String docId = DocumentsContract.getDocumentId(fileUri); String[] split = docId.split(":"); String type = split[0]; Uri contentUri = null; if ("image".equals(type)) { contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; } else if ("video".equals(type)) { contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; } else if ("audio".equals(type)) { contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; } String selection = MediaStore.Images.Media._ID + "=?"; String[] selectionArgs = new String[]{split[1]}; return getDataColumn(context, contentUri, selection, selectionArgs); } } // MediaStore (and general) else if ("content".equalsIgnoreCase(fileUri.getScheme())) { // Return the remote address if (isGooglePhotosUri(fileUri)) return fileUri.getLastPathSegment(); return getDataColumn(context, fileUri, null, null); } // File else if ("file".equalsIgnoreCase(fileUri.getScheme())) { return fileUri.getPath(); } return null; } /** * Android4.4 (<19)以下版本获取uri地址方法 * * @param context 上下文 * @param uri 返回的uri * @param selection 条件 * @param selectionArgs 值 * @return uri文件所在的路径 */ public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; String[] projection = {MediaStore.Images.Media.DATA}; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { int index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); return cursor.getString(index); } } finally { if (cursor != null) cursor.close(); } return null; } /** * @param uri The Uri to check. * @return URI权限是否为ExternalStorageProvider * Whether the Uri authority is ExternalStorageProvider. */ public static boolean isExternalStorageDocument(Uri uri) { return "com.android.externalstorage.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return URI权限是否为google图片 * Whether the Uri authority is Google Photos. */ public static boolean isGooglePhotosUri(Uri uri) { return "com.google.android.apps.photos.content".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return URI权限是否为DownloadsProvider. * Whether the Uri authority is DownloadsProvider. */ public static boolean isDownloadsDocument(Uri uri) { return "com.android.providers.downloads.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return URI权限是否为MediaProvider. * Whether the Uri authority is MediaProvider. */ public static boolean isMediaDocument(Uri uri) { return "com.android.providers.media.documents".equals(uri.getAuthority()); }
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!
posted on 2021-07-02 11:06 LoaderMan 阅读(1137) 评论(0) 编辑 收藏 举报