Android Studio中仅通过Uri删除外部存储文件遇文件找不到问题求助
Hey there! Let's work through this file not found issue together—since you mentioned the URI worked for video playback before, we can narrow down the problem pretty quickly.
The core issue here is that content://downloads/all_downloads/644 isn't a direct file path—it's a content provider URI pointing to an entry in the system Downloads content provider. If you're trying to use this string directly with File class methods (like new File(uriString)), that will fail every time, because the system doesn't recognize it as a valid filesystem path.
1. Get the Real Filesystem Path (For Legacy Use Cases)
If you absolutely need a traditional file path, you can query the ContentResolver to get the actual path associated with the download URI:
private String getDownloadFilePath(Context context, Uri downloadUri) { String[] projection = {MediaStore.Downloads.DATA}; Cursor cursor = null; try { cursor = context.getContentResolver().query(downloadUri, projection, null, null, null); if (cursor != null && cursor.moveToFirst()) { int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Downloads.DATA); return cursor.getString(columnIndex); } } catch (Exception e) { Log.e("FileUtils", "Error getting download file path", e); } finally { if (cursor != null) cursor.close(); } return null; }
Call this method with your content://downloads/all_downloads/644 URI, and the returned string will be a valid path you can use with File operations.
2. Use ContentResolver to Access the File (Recommended for Modern Android)
You mentioned you used this URI to play video before—chances are the video player didn't use a file path at all. Instead, it used the ContentResolver to open an input stream directly. This is the preferred method, especially on Android 10+ with scoped storage:
try { InputStream inputStream = getContentResolver().openInputStream(yourDownloadUri); // Use the input stream for your needs (e.g. pass to a media player, read file content) } catch (FileNotFoundException e) { Log.e("FileAccess", "Could not open input stream", e); }
This avoids dealing with file paths entirely and works within Android's security model.
3. Double-Check Permissions & URI Grants
- Runtime Permissions: On Android 6.0+, make sure you've requested and been granted
READ_EXTERNAL_STORAGE(orREAD_MEDIA_VIDEO/READ_MEDIA_IMAGESfor Android 13+ if the file is a media type). - URI Permission Flags: If you got this URI from an Intent (like
ACTION_GET_CONTENT), ensure you addedFLAG_GRANT_READ_URI_PERMISSIONwhen launching the Intent. Without this, your app loses access to the URI once the original Activity finishes.
4. Scoped Storage Considerations (Android 10+)
If your app targets API level 29 or higher, direct access to external storage paths is restricted. In this case, using the ContentResolver input stream method (option 2) is the most reliable. If you need to modify the file, consider copying it to your app's private storage directory first.
内容的提问来源于stack exchange,提问作者Naman Jain

