How to check if resource pointed by Uri is available?

前端 未结 6 2291
说谎
说谎 2021-02-07 00:44

I have resource (music file) pointed by Uri. How can I check if it is available before I try to play it with MediaPlayer?

Its Uri is stored in database, so when the file

相关标签:
6条回答
  • 2021-02-07 01:22

    There are few methods of DocumentProvider when applied to uri, return null if there is no document underlying uri. I chose getType(uri) which returns mime type of the document/file. If no document/file exists represented in uri, it returns null. Hence, to detect whether documennt/file exists or not, you can use this method like below.

    public static boolean exists(Context context, Uri uri)
    {
        return context.getContentResolver().getType(uri) !=null;
        
    }
    

    Other methods mentioned like querying the uri to get documentID or opening inputstream/outputstream did not work because they throw filenotfound exception if document/file does not exist, which resulted in crashing of app.

    You may attempt other methods which return null instead of throwing filenotfoundexception, if document/file does not exist.

    0 讨论(0)
  • 2021-02-07 01:26

    The reason the proposed method doesn't work is because you're using the ContentProvider URI rather than the actual file path. To get the actual file path, you have to use a cursor to get the file.

    Assuming String contentUri is equal to the content URI such as content://media/external/audio/media/192

    ContentResolver cr = getContentResolver();
    String[] projection = {MediaStore.MediaColumns.DATA}
    Cursor cur = cr.query(Uri.parse(contentUri), projection, null, null, null);
    if (cur != null) {
      if (cur.moveToFirst()) {
        String filePath = cur.getString(0);
    
        if (new File(filePath).exists()) {
          // do something if it exists
        } else {
          // File was not found
        }
      } else {
         // Uri was ok but no entry found. 
      }
      cur.close();
    } else {
      // content Uri was invalid or some other error occurred 
    }
    

    I haven't used this method with sound files or internal storage, but it should work. The query should return a single row directly to your file.

    0 讨论(0)
  • 2021-02-07 01:34

    As of Kitkat you can, and you should, persist URIs that your app uses if necessary. As far as I know, there's a 128 URI limit you can persist per app, so it's up to you to maximize usage of those resources.

    Personally I wouldn't deal with direct paths in this case, but rather check if persisted URI still exists, since when resource (a file) is deleted from a device, your app loses rights to that URI therefore making that check as simple as the following:

    getContext().getContentResolver().getPersistedUriPermissions().forEach( {element -> element.uri == yourUri});
    

    Meanwhile you won't need to check for URI permissions when a device is below Kitkat API level.

    Usually, when reading files from URIs you're going to use ParcelFileDescriptor, thus it's going to throw if no file is available for that particular URI, therefore you should wrap it with try/catch block.

    0 讨论(0)
  • 2021-02-07 01:37

    For those still looking out for a solution [works perfectly fine as of Dec 2020] and behaves as expected for all edge cases, the solution is a follows:

    boolean bool = false;
            if(null != uri) {
                try {
                    InputStream inputStream = context.getContentResolver().openInputStream(uri);
                    inputStream.close();
                    bool = true;
                } catch (Exception e) {
                    Log.w(MY_TAG, "File corresponding to the uri does not exist " + uri.toString());
                }
            }
    

    If the file corresponding to the URI exists, then you will have an input stream object to work with, else an exception will be thrown.

    Do not forget to close the input stream if the file does exist.

    NOTE:

    DocumentFile sourceFile = DocumentFile.fromSingleUri(context, uri);
    boolean bool = sourceFile.exists();
    

    The above lines of code for DocumentFile, does handle most edge cases, but what I found out was that if a file is created programmatically and stored in some folder, the user then visits the folder and manually deletes the file (while the app is running), DocumentFile.fromSingleUri wrongly says that the file exists.

    0 讨论(0)
  • 2021-02-07 01:44

    Try a function like:

    public static boolean checkURIResource(Context context, Uri uri) {
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        boolean doesExist= (cursor != null && cursor.moveToFirst());
        if (cursor != null) {
            cursor.close();
        }
        return doesExist;
    }
    
    0 讨论(0)
  • 2021-02-07 01:48

    I too had this problem - I really wanted to check if a Uri was available before trying to load it, as unnecessary failures would end up crowding my Crashlytics logs.

    Since the arrival of the StorageAccessFramework (SAF), DocumentProviders, etc., dealing with Uris has become more complicated. This is what I eventually used:

    fun yourFunction() {
    
        val uriToLoad = ...
    
        val validUris = contentResolver.persistedUriPermissions.map { uri }
    
        if (isLoadable(uriToLoad, validUris) != UriLoadable.NO) {
            // Attempt to load the uri
        }
    }
    
    enum class UriLoadable {
        YES, NO, MAYBE
    }
    
    fun isLoadable(uri: Uri, granted: List<Uri>): UriLoadable {
    
        return when(uri.scheme) {
            "content" -> {
                if (DocumentsContract.isDocumentUri(this, uri))
                    if (documentUriExists(uri) && granted.contains(uri))
                        UriLoadable.YES
                    else
                        UriLoadable.NO
                else // Content URI is not from a document provider
                    if (contentUriExists(uri))
                        UriLoadable.YES
                    else
                        UriLoadable.NO
            }
    
            "file" -> if (File(uri.path).exists()) UriLoadable.YES else UriLoadable.NO
    
            // http, https, etc. No inexpensive way to test existence.
            else -> UriLoadable.MAYBE
        }
    }
    
    // All DocumentProviders should support the COLUMN_DOCUMENT_ID column
    fun documentUriExists(uri: Uri): Boolean =
            resolveUri(uri, DocumentsContract.Document.COLUMN_DOCUMENT_ID)
    
    // All ContentProviders should support the BaseColumns._ID column
    fun contentUriExists(uri: Uri): Boolean =
            resolveUri(uri, BaseColumns._ID)
    
    fun resolveUri(uri: Uri, column: String): Boolean {
    
        val cursor = contentResolver.query(uri,
                arrayOf(column), // Empty projections are bad for performance
                null,
                null,
                null)
    
        val result = cursor?.moveToFirst() ?: false
    
        cursor?.close()
    
        return result
    }
    

    If someone has a more elegant -- or correct -- alternative, please do comment.

    0 讨论(0)
提交回复
热议问题