Android:从内容URI获取文件URI?

JMR*_*ies 109 android uri file

在我的应用程序中,用户将选择应用程序随后处理的音频文件.问题是,为了让应用程序按照我想要的方式处理音频文件,我需要URI为文件格式.当我使用Android的原生音乐播放器在应用程序中浏览音频文件时,URI是一个内容URI,如下所示:

content://media/external/audio/media/710
Run Code Online (Sandbox Code Playgroud)

但是,使用流行的文件管理器应用程序Astro,我得到以下内容:

file:///sdcard/media/audio/ringtones/GetupGetOut.mp3
Run Code Online (Sandbox Code Playgroud)

后者对我来说更容易使用,但当然我希望应用程序具有用户选择的音频文件的功能,而不管他们用来浏览他们的集合的程序.所以我的问题是,有没有办法将content://样式URI转换为file://URI?否则,你会建议我解决这个问题?以下是调用选择器的代码,供参考:

Intent ringIntent = new Intent();
ringIntent.setType("audio/mp3");
ringIntent.setAction(Intent.ACTION_GET_CONTENT);
ringIntent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(ringIntent, "Select Ringtone"), SELECT_RINGTONE);
Run Code Online (Sandbox Code Playgroud)

我使用内容URI执行以下操作:

m_ringerPath = m_ringtoneUri.getPath();
File file = new File(m_ringerPath);
Run Code Online (Sandbox Code Playgroud)

然后用所述文件做一些FileInputStream.

Jas*_*run 137

只需使用getContentResolver().openInputStream(uri)得到一个InputStream从URI.

http://developer.android.com/reference/android/content/ContentResolver.html#openInputStream(android.net.Uri)

  • 有没有办法获取文件而不是InputStream(来自内容:...)? (54认同)
  • 实际上,我只是重新阅读了getContentResolver().openInputStream()的文档,并且它自动适用于"内容"或"文件"的方案,所以你*不需要检查方案...如果你可以安全地假设它始终是content://或file://然后openInputStream()将始终有效. (15认同)
  • 检查从选择器活动返回给您的URI的方案.如果是uri.getScheme.equals("content"),请使用内容解析器打开它.如果是uri.Scheme.equals("file"),请使用普通文件方法打开它.无论哪种方式,您最终都会得到一个可以使用公共代码处理的InputStream. (12认同)
  • 对于使用依赖于Files而不是FileStreams的闭源API,但又想使用操作系统允许用户选择文件的人来说,这个答案是不够的.参考答案@DanyalAytekin正是我需要什么(事实上,我能够大量削减的脂肪,因为我清楚地知道什么样的我正在使用的文件). (6认同)
  • @kilaka你可以获得文件路径,但这很痛苦.请参见http://stackoverflow.com/a/20559418/294855 (4认同)
  • 这给了我一个`FileNotFoundException` (2认同)

Raf*_*bre 41

您可以使用内容解析程序file://content://URI 获取路径:

String filePath = null;
Uri _uri = data.getData();
Log.d("","URI = "+ _uri);                                       
if (_uri != null && "content".equals(_uri.getScheme())) {
    Cursor cursor = this.getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
    cursor.moveToFirst();   
    filePath = cursor.getString(0);
    cursor.close();
} else {
    filePath = _uri.getPath();
}
Log.d("","Chosen path = "+ filePath);
Run Code Online (Sandbox Code Playgroud)

  • 这仅适用于本地文件,例如,它不适用于Google云端硬盘 (4认同)
  • 这是一个主要的反模式。某些 ContentProvider 确实提供了该列,但是当您尝试绕过 ContentResolver 时,不能保证您对“文件”具有读/写访问权限。使用 ContentResolver 方法对 `content://` uri 进行操作,这是官方的做法,受到 Google 工程师的鼓励。 (3认同)
  • 谢谢,这很完美。我无法像接受的答案所建议的那样使用 InputStream 。 (2认同)

Muh*_*uru 9

受启发的答案是Jason LaBrunDarth Raven。尝试已经回答的方法使我找到了以下解决方案,该解决方案可能主要涵盖光标空情况和从content://file:// 的转换

要转换文件,请从获得的 uri 读取和写入文件

public static Uri getFilePathFromUri(Uri uri) throws IOException {
    String fileName = getFileName(uri);
    File file = new File(myContext.getExternalCacheDir(), fileName);
    file.createNewFile();
    try (OutputStream outputStream = new FileOutputStream(file);
         InputStream inputStream = myContext.getContentResolver().openInputStream(uri)) {
        FileUtil.copyStream(inputStream, outputStream); //Simply reads input to output stream
        outputStream.flush();
    }
    return Uri.fromFile(file);
}
Run Code Online (Sandbox Code Playgroud)

要获取文件名使用,它将涵盖光标空情况

public static String getFileName(Uri uri) {
    String fileName = getFileNameFromCursor(uri);
    if (fileName == null) {
        String fileExtension = getFileExtension(uri);
        fileName = "temp_file" + (fileExtension != null ? "." + fileExtension : "");
    } else if (!fileName.contains(".")) {
        String fileExtension = getFileExtension(uri);
        fileName = fileName + "." + fileExtension;
    }
    return fileName;
}
Run Code Online (Sandbox Code Playgroud)

从 mime 类型转换为文件扩展名的好选择

 public static String getFileExtension(Uri uri) {
    String fileType = myContext.getContentResolver().getType(uri);
    return MimeTypeMap.getSingleton().getExtensionFromMimeType(fileType);
}
Run Code Online (Sandbox Code Playgroud)

光标获取文件名

public static String getFileNameFromCursor(Uri uri) {
    Cursor fileCursor = myContext.getContentResolver().query(uri, new String[]{OpenableColumns.DISPLAY_NAME}, null, null, null);
    String fileName = null;
    if (fileCursor != null && fileCursor.moveToFirst()) {
        int cIndex = fileCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
        if (cIndex != -1) {
            fileName = fileCursor.getString(cIndex);
        }
    }
    return fileName;
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,这个已经看了一周了。不喜欢为此复制文件,但它确实有效。 (2认同)

Thr*_*ian 7

如果您有内容Uri,则content://com.externalstorage...可以使用此方法获取Android 19或更高版本的文件夹或文件的绝对路径.

public static String getPath(final Context context, final Uri uri) {
    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

    // DocumentProvider
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
        System.out.println("getPath() uri: " + uri.toString());
        System.out.println("getPath() uri authority: " + uri.getAuthority());
        System.out.println("getPath() uri path: " + uri.getPath());

        // ExternalStorageProvider
        if ("com.android.externalstorage.documents".equals(uri.getAuthority())) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];
            System.out.println("getPath() docId: " + docId + ", split: " + split.length + ", type: " + type);

            // This is for checking Main Memory
            if ("primary".equalsIgnoreCase(type)) {
                if (split.length > 1) {
                    return Environment.getExternalStorageDirectory() + "/" + split[1] + "/";
                } else {
                    return Environment.getExternalStorageDirectory() + "/";
                }
                // This is for checking SD Card
            } else {
                return "storage" + "/" + docId.replace(":", "/");
            }

        }
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)

您可以检查Uri的每个部分是否使用println.下面列出了SD卡和设备主存储器的返回值.您可以访问和删除文件是否在内存中但我无法使用此方法从SD卡中删除文件,只能使用此绝对路径读取或打开图像.如果您找到了使用此方法删除的解决方案,请分享.SD卡

getPath() uri: content://com.android.externalstorage.documents/tree/612E-B7BF%3A/document/612E-B7BF%3A
getPath() uri authority: com.android.externalstorage.documents
getPath() uri path: /tree/612E-B7BF:/document/612E-B7BF:
getPath() docId: 612E-B7BF:, split: 1, type: 612E-B7BF
Run Code Online (Sandbox Code Playgroud)

主记忆

getPath() uri: content://com.android.externalstorage.documents/tree/primary%3A/document/primary%3A
getPath() uri authority: com.android.externalstorage.documents
getPath() uri path: /tree/primary:/document/primary:
getPath() docId: primary:, split: 1, type: primary
Run Code Online (Sandbox Code Playgroud)

如果你希望file:///在获得路径后使用Uri

DocumentFile documentFile = DocumentFile.fromFile(new File(path));
documentFile.getUri() // will return a Uri with file Uri
Run Code Online (Sandbox Code Playgroud)


小智 6

尝试通过调用处理带有content://方案的URI ContentResolver.query()不是一个好的解决方案.在运行4.2.2的HTC Desire上,您可以获得NULL作为查询结果.

为什么不使用ContentResolver呢? /sf/answers/2039926031/

  • 如果您没有访问权限,则“路径”是无用的。例如,如果应用程序为您提供了一个“ content://” uri,对应于其私有内部目录中的文件,那么您将无法在新的Android版本中将该uri与“ File” API一起使用。ContentResolver旨在克服这种安全限制。如果您从ContentResolver获得了uri,则可以期望它能够正常工作。 (2认同)

Sha*_*aon 6

尝试这个....

从内容 uri 中获取文件

fun fileFromContentUri(context: Context, contentUri: Uri): File {
    // Preparing Temp file name
    val fileExtension = getFileExtension(context, contentUri)
    val fileName = "temp_file" + if (fileExtension != null) ".$fileExtension" else ""

    // Creating Temp file
    val tempFile = File(context.cacheDir, fileName)
    tempFile.createNewFile()

    try {
        val oStream = FileOutputStream(tempFile)
        val inputStream = context.contentResolver.openInputStream(contentUri)

        inputStream?.let {
            copy(inputStream, oStream)
        }

        oStream.flush()
    } catch (e: Exception) {
        e.printStackTrace()
    }

    return tempFile
}

private fun getFileExtension(context: Context, uri: Uri): String? {
    val fileType: String? = context.contentResolver.getType(uri)
    return MimeTypeMap.getSingleton().getExtensionFromMimeType(fileType)
}

@Throws(IOException::class)
private fun copy(source: InputStream, target: OutputStream) {
    val buf = ByteArray(8192)
    var length: Int
    while (source.read(buf).also { length = it } > 0) {
        target.write(buf, 0, length)
    }
}
Run Code Online (Sandbox Code Playgroud)