@TargetApi(19) private void handleImageOnKitkat(Intent data){ String imagePath=null; Uri uri=data.getData(); if (DocumentsContract.isDocumentUri(this,uri)){ //如果是document類型的Uri。則通過document id 處理 String docId=DocumentsContract.getDocumentId(uri); if ("com.android.providers.media.documents".equals(uri.getAuthority())){ String id=docId.split(":")[1];//解析出文字格式的id String selection=MediaStore.Images.Media._ID+"="+id; imagePath=getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection); } else if("com.android.providers.downloads.documents".equals(uri.getAuthority())){ Uri contentUri= ContentUris.withAppendedId(Uri.parse("content://downloads/public" + "_downloads"),Long.valueOf(docId)); } }else if ("content".equalsIgnoreCase(uri.getScheme())){ //如果是content類型的Uri,則是用普通方式處理 imagePath=getImagePath(uri,null); }else if ("file".equalsIgnoreCase(uri.getScheme())){ //如果是file類型的Uri,直接獲取圖片路徑即可 imagePath=uri.getPath(); } displayImage(imagePath);//根據圖片路徑顯示圖片 }
@SuppressLint("NewApi") private String getRealPath(Uri uri){ String filePath = null; String uriString = uri.toString(); if(uriString.startsWith("content://media")){ filePath = getDataColumn(context, uri, null, null); } else if (uriString.startsWith("file")){ filePath = uri.getPath(); } else if (uriString.startsWith("content://com")){ String docId = DocumentsContract.getDocumentId(uri); String[] split = docId.split(":"); Uri contentUri = null; contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; String selection = "_id=?"; String[] selectionArgs = new String[] {split[1]}; filePath = getDataColumn(context, contentUri, selection, selectionArgs); } return filePath; }
private void handleImage(Uri uri){ String imagePath = null; if(DocumentsContract.isDocumentUri(getActivity(), uri)){ String docId = DocumentsContract.getDocumentId(uri); if("com.android.providers.media.documents".equals(uri.getAuthority())){ String id = docId.split(":")[1]; String selection = MediaStore.Images.Media._ID+"="+id; imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection); } else if("com.android.providers.downloads.documents".equals(uri.getAuthority())){ Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId)); imagePath = getImagePath(contentUri, null); } } else if("content".equalsIgnoreCase(uri.getScheme())){ imagePath = getImagePath(uri, null); } else if("file".equalsIgnoreCase(uri.getScheme())){ imagePath = uri.getPath(); } filePath = imagePath; Log.i(TAG, "handleImage: Album filepath"+filePath); getQiniuUpToken(); }
/** * 执行打开相册逻辑 */ @TargetApi(19) private String handleImageOnKitKat(Intent data) { String imagePath = null; Uri uri = data.getData(); if (DocumentsContract.isDocumentUri(this, uri)) { String docId = DocumentsContract.getDocumentId(uri); if ("com.android.providers.media.documents".equals(uri.getAuthority())) { String id = docId.split(":")[1]; String selection = MediaStore.Images.Media._ID + "=" + id; imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection); } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) { Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId)); imagePath = getImagePath(contentUri, null); } } else if ("content".equalsIgnoreCase(uri.getScheme())) { imagePath = getImagePath(uri, null); } else if ("file".equalsIgnoreCase(uri.getScheme())) { imagePath = uri.getPath(); } return imagePath; }
@TargetApi(Build.VERSION_CODES.KITKAT) public void handleImageOnKitKat(Intent data) { String imagePath=null; Uri uri=data.getData(); if (DocumentsContract.isDocumentUri(this,uri)){ //如果是document类型的Uri,则通过document id处理 String docId=DocumentsContract.getDocumentId(uri); if ("com.android.providers.media.documents".equals(uri.getAuthority())){ String id=docId.split(":")[1];//解析数字可是的id String selection=MediaStore.Images.Media._ID+"="+id; imagePath=getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection); }else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())){ Uri contentUri= ContentUris.withAppendedId(Uri.parse("content://downloads/punlic_downloads"),Long.valueOf(docId)); imagePath=getImagePath(contentUri,null); } }else if ("content".equalsIgnoreCase(uri.getScheme())){ //如果是content类型的Uri,则使用普通方法 imagePath=getImagePath(uri,null); }else if ("file".equalsIgnoreCase(uri.getScheme())){ imagePath=uri.getPath(); } displayImage(imagePath);//根据图片路径显示图片 }
@SuppressLint("NewApi") public static String getRealPathFromURI_API19(Context context, Uri uri){ String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the array String id = wholeID.split(":")[1]; String[] column = { MediaStore.Images.Media.DATA }; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{ id }, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; }
@TargetApi(19) private void handleImageOnKitKat(Intent data){ String imagePath=null; Uri uri=data.getData(); if (DocumentsContract.isDocumentUri(getActivity(),uri)){ //如果是 document 类型的 Uri,则通过document id处理 String docId=DocumentsContract.getDocumentId(uri); if ("com.android.providers.media.documents".equals(uri.getAuthority())){ String id=docId.split(":")[1]; //解析出数字格式的id String selection=MediaStore.Images.Media._ID+"="+id; imagePath=getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection); }else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())){ Uri contentUri= ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId)); imagePath=getImagePath(contentUri,null); } }else if ("content".equalsIgnoreCase(uri.getScheme())){ //如果是content类型的Uri 则使用普通方式处理 imagePath=getImagePath(uri,null); }else if ("file".equalsIgnoreCase(uri.getScheme())){ //如果是file类型的Uri,直接获取图片路径即可 imagePath=uri.getPath(); } displayImage(imagePath); //根据图片路径显示图片 }
public static boolean canWrite(Context context, Uri self) { // Ignore if grant doesn't allow write if (context.checkCallingOrSelfUriPermission(self, Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != PackageManager.PERMISSION_GRANTED) { return false; } final String type = getRawType(context, self); final int flags = queryForInt(context, self, DocumentsContract.Document.COLUMN_FLAGS, 0); // Ignore documents without MIME if (TextUtils.isEmpty(type)) { return false; } // Deletable documents considered writable if ((flags & DocumentsContract.Document.FLAG_SUPPORTS_DELETE) != 0) { return true; } if (DocumentsContract.Document.MIME_TYPE_DIR.equals(type) && (flags & DocumentsContract.Document.FLAG_DIR_SUPPORTS_CREATE) != 0) { // Directories that allow create considered writable return true; } else if (!TextUtils.isEmpty(type) && (flags & DocumentsContract.Document.FLAG_SUPPORTS_WRITE) != 0) { // Writable normal files considered writable return true; } return false; }
public static Uri[] listFiles(Context context, Uri self) { final ContentResolver resolver = context.getContentResolver(); final Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(self, DocumentsContract.getDocumentId(self)); final ArrayList<Uri> results = new ArrayList<Uri>(); Cursor c = null; try { c = resolver.query(childrenUri, new String[] { DocumentsContract.Document.COLUMN_DOCUMENT_ID }, null, null, null); while (c.moveToNext()) { final String documentId = c.getString(0); final Uri documentUri = DocumentsContract.buildDocumentUriUsingTree(self, documentId); results.add(documentUri); } } catch (Exception e) { Log.w(TAG, "Failed query: " + e); } finally { closeQuietly(c); } return results.toArray(new Uri[results.size()]); }
private static String getPicturePathFromUriAboveApi19(Context context, Uri uri) { String filePath = null; if (DocumentsContract.isDocumentUri(context, uri)) { // 如果是document类型的 uri, 则通过document id来进行处理 String documentId = DocumentsContract.getDocumentId(uri); if (isMediaDocument(uri)) { // MediaProvider // 使用':'分割 String id = documentId.split(":")[1]; String selection = MediaStore.Images.Media._ID + "=?"; String[] selectionArgs = {id}; filePath = getDataColumn(context, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection, selectionArgs); } else if (isDownloadsDocument(uri)) { // DownloadsProvider Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(documentId)); filePath = getDataColumn(context, contentUri, null, null); } } else if ("content".equalsIgnoreCase(uri.getScheme())) { // 如果是 content 类型的 Uri filePath = getDataColumn(context, uri, null, null); } else if ("file".equals(uri.getScheme())) { // 如果是 file 类型的 Uri,直接获取图片对应的路径 filePath = uri.getPath(); } return filePath; }
@TargetApi(19) private void handleImageOnKitKat(Intent data){ String imagePath = null; Uri uri = data.getData(); if(DocumentsContract.isDocumentUri(this,uri)){ String docId = DocumentsContract.getDocumentId(uri); if("com.android.provider.media,documents".equals(uri.getAuthority())){ String id = docId.split(":")[1]; String selection = MediaStore.Images.Media._ID + "=" + id; imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection); }else if("com.android.provider.downloads.documents".equals(uri.getAuthority())){ Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),Long.valueOf(docId)); imagePath = getImagePath(contentUri,null); } }else if("content".equalsIgnoreCase(uri.getScheme())){ imagePath = getImagePath(uri,null); }else if("file".equalsIgnoreCase(uri.getScheme())){ imagePath = uri.getPath(); } displayImage(imagePath);//根据图片路径显示图片 }
@Nullable public static String getPath(@NonNull Context context, @NonNull Uri uri) { String filePath = null; try { String wholeID = DocumentsContract.getDocumentId(uri); String id = wholeID.split(":")[1]; String[] column = {MediaStore.Images.Media.DATA}; String sel = MediaStore.Images.Media._ID + "=?"; try (Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{id}, null)) { if (cursor != null) { int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } } } } catch (Exception ignored) {} return filePath; }
private String handleImage(Intent data) { imagePath = null; Uri uri = data.getData(); if (DocumentsContract.isDocumentUri(this, uri)) { String docId = DocumentsContract.getDocumentId(uri); if ("com.android.providers.media.documents".equals(uri.getAuthority())) { String id = docId.split(":")[1]; String selection = MediaStore.Images.Media._ID + "=" + id; imagePath = getPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection); } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) { Uri contentUri = ContentUris.withAppendedId( Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId)); imagePath = getPath(contentUri, null); } } else if ("content".equalsIgnoreCase(uri.getScheme())) { imagePath = getPath(uri, null); } else if ("file".equalsIgnoreCase(uri.getScheme())) { imagePath = uri.getPath(); } return imagePath; }
@TargetApi(19) private void handleImageOnKitKat(Intent data) { String imagePath = null; Uri uri = data.getData(); if (DocumentsContract.isDocumentUri(mView.getActivity(), uri)) { // 如果是documentlent类型的URI,则通过docment id处理 String docId = DocumentsContract.getDocumentId(uri); if ("com.android.providers.media.documents".equals(uri.getAuthority())) { String id = docId.split(":")[1]; String selection = MediaStore.Images.Media._ID + "=" + id; imagePath = getImagePatch(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection); } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) { Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId)); imagePath = getImagePatch(contentUri, null); } } else if ("content".equalsIgnoreCase(uri.getScheme())) { // 如果是content类型的uri的话,则使用普通方式处理 imagePath = getImagePatch(uri, null); } else if ("file".equalsIgnoreCase(uri.getScheme())) { // 若果是file类型的uri,则直接获取图片路径 imagePath = uri.getPath(); } copyFileInOtherThread(imagePath); }
public static Uri[] listFiles(Context context, Uri self) { ContentResolver resolver = context.getContentResolver(); Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(self, DocumentsContract.getDocumentId(self)); ArrayList<Uri> results = new ArrayList(); Cursor c = null; try { c = resolver.query(childrenUri, new String[]{"document_id"}, null, null, null); while (c.moveToNext()) { results.add(DocumentsContract.buildDocumentUriUsingTree(self, c.getString(0))); } } catch (Exception e) { Log.w(TAG, "Failed query: " + e); } finally { closeQuietly(c); } return (Uri[]) results.toArray(new Uri[results.size()]); }
@Override public void setUri(Uri uri_) { if (this.uri == null && isTreeUri(uri_)) { try { ctx.getContentResolver().takePersistableUriPermission(uri_, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); } catch (Exception e) { e.printStackTrace(); } this.uri = DocumentsContract.buildDocumentUriUsingTree(uri_, DocumentsContract.getTreeDocumentId(uri_)); } else { this.uri = uri_; } }
private static int getDirItemsNumber(Context ctx, Uri u) { int itemsNumber = 0; Cursor c = null; try { ContentResolver cr = ctx.getContentResolver(); String dirId = DocumentsContract.getDocumentId(u); Uri dirUri = DocumentsContract.buildChildDocumentsUriUsingTree(u, dirId); c = cr.query(dirUri, projection, null, null, null); if (c != null) { itemsNumber = c.getCount(); } } catch (Exception e) { // notning to do; } finally { if (c != null) { c.close(); } } return itemsNumber; }
@Override public boolean renameItem(int position, String newName) { ContentResolver cr = ctx.getContentResolver(); Item item = items[position - 1]; Uri new_uri = null; try { new_uri = DocumentsContract.renameDocument(cr, (Uri) item.origin, newName); } catch (FileNotFoundException e) { e.printStackTrace(); return false; } if (new_uri == null) { return false; } item.origin = new_uri; notifyRefr(newName); return true; }
@Override public void createFolder(String new_name) { try { Uri new_uri = DocumentsContract.createDocument(ctx.getContentResolver(), uri, Document.MIME_TYPE_DIR, new_name); if (new_uri != null) { notifyRefr(new_name); return; } } catch (Exception e) { e.printStackTrace(); } notify(ctx.getString(R.string.fman_create_folder_error, new_name), CommanderIf.OPERATION_FAILED); }
/** * @param data * @return 图片路径 */ public static String handleImageOnKitKat(Intent data) { String imagePath = ""; Log.i(TAG, "handleImageOnKitKat: "); Uri uri = data.getData(); if (DocumentsContract.isDocumentUri(MyApplication.getContext(), uri)) { //如果是document类型的uri,则通过document id处理 String docId = DocumentsContract.getDocumentId(uri); if ("com.android.providers.media.documents".equals(uri.getAuthority())) { String id = docId.split(":")[1];//解析出数字格式的ID String selection = MediaStore.Images.Media._ID + "=" + id; imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection); } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) { Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId)); imagePath = getImagePath(contentUri, null); } } else if ("content".equalsIgnoreCase(uri.getScheme())) { //如果是content类型的uri,则使用普通的方式处理 imagePath = getImagePath(uri, null); } else if ("file".equalsIgnoreCase(uri.getScheme())) { //如果是file类型的uri,直接获取图片路径即可 imagePath = uri.getPath(); } return imagePath; }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) public static boolean checkPersistUriPermissionsAndUpdate(Context context, DocumentFolder documentFolder) { Uri uri = documentFolder.getFolder().getUri(); List<UriPermission> permissions = context.getContentResolver().getPersistedUriPermissions(); for (UriPermission permission : permissions) { String permissionTreeId = DocumentsContract.getTreeDocumentId(permission.getUri()); String uriTreeId = DocumentsContract.getTreeDocumentId(uri); if (uriTreeId.startsWith(permissionTreeId)) { // update permissions - after a restart this is necessary... updatePersistUriPermissions(context, uri); documentFolder.updateDocument(DocumentFile.fromTreeUri(context, permission.getUri())); return true; } } return false; }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) public static boolean checkPersistUriPermissionsAndUpdate(Context context, StorageDocument storageDocument) { Uri uri = storageDocument.getUri(); List<UriPermission> permissions = context.getContentResolver().getPersistedUriPermissions(); for (UriPermission permission : permissions) { String permissionTreeId = DocumentsContract.getTreeDocumentId(permission.getUri()); String uriTreeId = DocumentsContract.getTreeDocumentId(uri); if (uriTreeId.startsWith(permissionTreeId)) { // update permissions - after a restart this is necessary... updatePersistUriPermissions(context, uri); storageDocument.setWrapped(DocumentFile.fromTreeUri(context, permission.getUri())); return true; } } return false; }
@TargetApi(Build.VERSION_CODES.KITKAT) public void handleImageBeforeKitkat(Intent data) { String imagePath=null; Uri uri=data.getData(); if(DocumentsContract.isDocumentUri(mActivity, uri)){ String docid=DocumentsContract.getDocumentId(uri); if("com.android.providers.media.documents".equals(uri.getAuthority())){ String id=docid.split(":")[1]; String selection= MediaStore.Images.Media._ID+"="+id; imagePath=getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection); }else if("com.android.providers.downloads.documents".equals(uri.getAuthority())){ Uri contentUri= ContentUris.withAppendedId( Uri.parse("content://downloads/public_downloads"), Long.valueOf(docid)); imagePath=getImagePath(contentUri,null); } }else if("content".equalsIgnoreCase(uri.getScheme())){ imagePath=getImagePath(uri,null); } //剪裁图片 cropImage(imagePath); }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) public static NoobFile buildTreeFile(Activity activity, Uri uri) throws SecurityException { if (uri == null) return null; ContentResolver contentResolver = activity.getContentResolver(); Uri docUri = DocumentsContract.buildDocumentUriUsingTree(uri, DocumentsContract.getTreeDocumentId(uri)); Cursor docCursor = contentResolver.query(docUri, new String[]{ DocumentsContract.Document.COLUMN_DISPLAY_NAME, DocumentsContract.Document.COLUMN_MIME_TYPE, DocumentsContract.Document.COLUMN_DOCUMENT_ID}, null, null, null); if (docCursor != null && docCursor.moveToNext()) { Log.d(TAG, "found doc =" + docCursor.getString(0) + ", mime=" + docCursor .getString(1)); NoobFile file = new NoobFile(activity, docCursor, uri, true); docCursor.close(); return file; } return null; }
@TargetApi(Build.VERSION_CODES.KITKAT) public static String getPath(final Context context, final Uri uri) { final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; try { // DocumentProvider if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { return handleKitKatVersion(context, uri); } // MediaStore (and general) else if ("content".equalsIgnoreCase(uri.getScheme())) { return getDataColumn(context, uri, null, null); } // File else if ("file".equalsIgnoreCase(uri.getScheme())) { return uri.getPath(); } } catch (Exception e) { DebugLog.e(e.getMessage()); } return null; }
@TargetApi(Build.VERSION_CODES.KITKAT) @Override protected void onActivityResult(final int requestCode, int resultCode, final Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { // Grab the selected permission so we can generate valid test files paths case REQUEST_CODE_WRITE_PERMISSION: if (resultCode == RESULT_OK && data != null) { Snackbar.make(findViewById(android.R.id.content), data.getDataString(), Snackbar.LENGTH_SHORT).show(); String root = DocumentsContract.getTreeDocumentId(data.getData()); test1Uri = DocumentsContract.buildDocumentUriUsingTree(data.getData(), root + documentId1); test2Uri = DocumentsContract.buildDocumentUriUsingTree(data.getData(), root + documentId2); } break; } }
/** * Returns a uri to a neighbor file within the same folder. This can be used to get an assumed uri * to a neighbor within a folder. This avoids heavy calls to DocumentFile.listFiles or * write-locked createFile * * This will only work with a uri that is an hierarchical tree similar to SCHEME_FILE * @param hierarchicalTreeUri folder to install into * @param filename filename of child file * @return Uri to the child file */ @Nullable public static Uri getNeighborUri(@NonNull Uri hierarchicalTreeUri, String filename) { String documentId = getDocumentId(hierarchicalTreeUri); if (documentId == null) return null; String root = getRoot(documentId); if (root == null) return null; String[] parts = getPathSegments(documentId); if (parts == null) return null; parts[parts.length-1] = filename; // replace the filename String path = TextUtils.join("/", parts); String neighborId = createNewDocumentId(root, path); return DocumentsContract.buildDocumentUriUsingTree(hierarchicalTreeUri, neighborId); }
public String getDocumentId() { try { if (DocumentsContract.isDocumentUri(mContext, mUri)) { return DocumentsContract.getDocumentId(mUri); } else { return DocumentsContract.getTreeDocumentId(mUri); } } catch (IllegalArgumentException e) { // This is not a document uri, for now I'll try to handle this gracefully. // While it may be convenient for a user to be able to use this object for all uri, // it may be difficult to manage all aspects gracefully. return null; } }
@TargetApi(19) private void handleImageOnKitKat(Intent data) { String imagePath = null; Uri uri = data.getData(); if (DocumentsContract.isDocumentUri(this,uri)) { // 如果是document类型的Uri,则通过document id处理 String docId = DocumentsContract.getDocumentId(uri); if ("com.android.providers.media.documents".equals(uri.getAuthority())) { String id = docId.split(":")[1]; // 解析出数字格式的ID String selection = MediaStore.Images.Media._ID+"="+id; imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection); } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) { Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),Long.valueOf(docId)); imagePath = getImagePath(contentUri,null); } } else if ("content".equalsIgnoreCase(uri.getScheme())) { // 如果不是document类型的Uri,则使用普通方式处理 imagePath = getImagePath(uri,null); } // 根据图片路径显示图片 displayImage(imagePath); }
public MatrixCursor.RowBuilder queryFile(AbstractTransport transport, MatrixCursor result, String id) { FEchoFile file_entry = transport.getFileMeta(id); String filename = file_entry.filename; if (!filename.contains(".") || !file_entry.existsLocally()) return null; else { MatrixCursor.RowBuilder row = result.newRow(); int dotindex = filename.lastIndexOf(".") + 1; String extension = filename.substring(dotindex).toLowerCase(); String mimetype = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); row.add(DocumentsContract.Document.COLUMN_DOCUMENT_ID, "idecfile://" + id); row.add(DocumentsContract.Document.COLUMN_DISPLAY_NAME, filename); row.add(DocumentsContract.Document.COLUMN_FLAGS, null); row.add(DocumentsContract.Document.COLUMN_MIME_TYPE, mimetype); row.add(DocumentsContract.Document.COLUMN_SIZE, file_entry.serverSize); row.add(DocumentsContract.Document.COLUMN_LAST_MODIFIED, null); } return null; }
@SuppressLint("NewApi") public static String getRealPathFromUriApi19(Context context, Uri uri){ String filePath = ""; String wholeId = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the array String id = wholeId.split(":")[1]; String[] column = { MediaStore.Images.Media.DATA }; // where id is equal to String selection = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, selection, new String[]{ id }, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; }
@TargetApi(19) private void handleImageOnKitKat(Intent data){ String imagePath=null; Uri uri=data.getData(); if(DocumentsContract.isDocumentUri(this,uri)){ String docId=DocumentsContract.getDocumentId(uri); if("com.android.providers.media.documents".equals(uri.getAuthority())){ String id=docId.split(":")[1]; String selection=MediaStore.Images.Media._ID+"="+id; imagePath=getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection); }else if("com.android.providers.downloads.documents".equals(uri.getAuthority())){ Uri contentUri=ContentUris.withAppendedId(Uri.parse("cintent://downloads/public_downloads"),Long.valueOf(docId)); imagePath=getImagePath(contentUri,null); } }else if("content".equalsIgnoreCase(uri.getScheme())){ imagePath=getImagePath(uri,null); } displayImage(imagePath); }
@TargetApi(Build.VERSION_CODES.KITKAT) private void handleImageAfterKitKat(Intent data) { String imagePath = null; Uri uri = data.getData(); if (DocumentsContract.isDocumentUri(mContext, uri)) { //如果是document类型的Uri,则通过document id处理 String docId = DocumentsContract.getDocumentId(uri); if ("com.android.providers.media.documents".equals(uri.getAuthority())) { String id = docId.split(":")[1]; //解析出数字格式的id String selection = MediaStore.Images.Media._ID + "=" + id; imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection); } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) { Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId)); imagePath = getImagePath(contentUri, null); } } else if ("content".equalsIgnoreCase(uri.getScheme())) { //如果是content类型的Uri,则使用普通方式处理 imagePath = getImagePath(uri, null); } else if ("file".equalsIgnoreCase(uri.getScheme())) { //如果是file类型的Uri,直接获取图片路径即可 imagePath = uri.getPath(); } displayImage(imagePath); }
@TargetApi(19) private void handleImageOnKitKat(Intent data){ String imagePath = null; Uri uri = data.getData(); Log.i("test","Uri="+uri); if(DocumentsContract.isDocumentUri(getContext(), uri)){ String docId = DocumentsContract.getDocumentId(uri); if("com.android.providers.media.documents".equals(uri.getAuthority())){ String id = docId.split(":")[1]; String selection = MediaStore.Images.Media._ID+"="+id; imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection); Log.i("test","4.4以上的imagePath="+imagePath); }else if("com.android.providers.downloads.documents".equals(uri.getAuthority())){ Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId)); imagePath = getImagePath(contentUri,null); Log.i("test","4.4以上的imagePath="+imagePath); } }else if("content".equalsIgnoreCase(uri.getScheme())){ imagePath = getImagePath(uri,null); Log.i("test","4.4以上的imagePath="+imagePath); } displayImage(imagePath); }