簡體   English   中英

如何從 zip 文件的內容 uri 中獲取文件路徑?

[英]How to get file path from the content uri for zip file?

我正在觸發選擇 zip 文件的意圖,並且在 onActivityResult 中,它為我提供了一個內容 URI。 例如:: (content://com.android.providers.downloads.documents/document/197)。 選擇的文件來自下載文件夾。 誰能解釋一下,如何查詢內容解析器以獲取文件路徑? 謝謝。

Already tried code :- 

   Cursor cursor = getActivity().getContentResolver()
                .query(contentUri, null, null, null, null);

        try {
            if (cursor != null && cursor.moveToFirst()) {
            String path=cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                Log.i(TAG, "path  Name: " + path);
            }
           // NOTE :: Here I am getting the file name, But i want the file path.
        } finally {
            cursor.close();
        }

我嘗試了很多方法來從下載文件夾中的選定文件中檢索文件路徑。 由於從 android Q 開始,谷歌建議 SAF(存儲訪問框架)從用戶那里獲取數據。 我們不能使用 Environment.getExternalStoragePublicDirectory() 從 android Q 及以上獲取文件路徑。

對於 android Q 及以上您可以按照此步驟操作。

1)從您要觸發的活動或片段開始意圖。

例如:

public void performFileSearch() { //from developer docs

    // ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file
    // browser.
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);

    // Filter to only show results that can be "opened", such as a
    // file (as opposed to a list of contacts or timezones)
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    // Filter to show only images, using the image MIME data type.
    // If one wanted to search for ogg vorbis files, the type would be "audio/ogg".
    // To search for all documents available via installed storage providers,
    // it would be "*/*".
    intent.setType("application/pdf"); // MIME type for what file you required

    startActivityForResult(intent, READ_REQUEST_CODE);
}

2)然后結果將出現在您的活動或片段的onActivityResult中

@Override
public void onActivityResult(int requestCode, int resultCode,
        Intent resultData) { // from developer docs

    // The ACTION_OPEN_DOCUMENT intent was sent with the request code
    // READ_REQUEST_CODE. If the request code seen here doesn't match, it's the
    // response to some other intent, and the code below shouldn't run at all.

    if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        // The document selected by the user won't be returned in the intent.
        // Instead, a URI to that document will be contained in the return intent
        // provided to this method as a parameter.
        // Pull that URI using resultData.getData().
        Uri uri = null;
        if (resultData != null) {
            uri = resultData.getData();
            Log.i(TAG, "Uri: " + uri.toString());
            processWithUriToGetPath(uri);
        }
    }
}

從 uri 中提取數據(注意:對於圖像、視頻和音頻,您可以參考開發者文檔。由於沒有找到任何合適的文件文檔,因此給出了此示例)。

    private void processWithUriToGetPath(Uri contentUri) {
        //Use content Resolver to get the input stream that it holds the data and copy that in a temp file of your app file directory for your references
         File selectedFile = new File(getActivity().getFilesDir(), "your file name"); //your app file dir or cache dir you can use
       InputStream in = getActivity().getContentResolver().openInputStream(contentUri);
                 OutputStream out = new FileOutputStream(selectedFile);
                        try {
                            byte[] buf = new byte[1024];
                            int len;
                            if (in != null) {
                                while ((len = in.read(buf)) > 0) {
                                    out.write(buf, 0, len);
                                }
                                out.close();
                                in.close();
                            } catch (IOException ie) {
                            ie.printStackTrace();
                        }


 //after this you will get file from the selected file and you can do 
           whatever with your wish.
         // Hope it helps... 
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM