how to get file name from URI

后端 未结 8 626
花落未央
花落未央 2020-12-16 15:46

Hello I am develop video player with android gallery. I receive URI from gallry. and I need to display video title, when play video. so if content has not title meta data. I

8条回答
  •  温柔的废话
    2020-12-16 16:12

    Since none of the answers really solve the questions, i put my solution here, hope it will be helpful.

         String fileName;
         if (uri.getScheme().equals("file")) {
                fileName = uri.getLastPathSegment();
            } else {
                Cursor cursor = null;
                try {
                    cursor = getContentResolver().query(uri, new String[]{
                            MediaStore.Images.ImageColumns.DISPLAY_NAME
                    }, null, null, null);
    
                    if (cursor != null && cursor.moveToFirst()) {
                        fileName = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME));
                        Log.d(TAG, "name is " + fileName);
                    }
                } finally {
    
                    if (cursor != null) {
                        cursor.close();
                    }
                }
            }
    

提交回复
热议问题