Skip to content
This repository has been archived by the owner on Nov 9, 2021. It is now read-only.

"Unknown URI" for downloaded document #34

Open
andelz opened this issue Oct 15, 2018 · 17 comments
Open

"Unknown URI" for downloaded document #34

andelz opened this issue Oct 15, 2018 · 17 comments

Comments

@andelz
Copy link

andelz commented Oct 15, 2018

Trying to get file path for a downloaded document I get an error:

Unknown URI: content://downloads/public_downloads/12627

Content URI provided was:
content://com.android.providers.downloads.documents/document/12627

It seems that files downloaded using Chrome or Gmail are stored this way. Am I missing something? Taking a look at your code it seems that since Android 8 (which I'm running on) you expect something that contains raw inside the content URI. But in my case it's lacking the raw part?

@gurwinder-macrew
Copy link

I am also getting same issue as @andelz content://com.android.providers.downloads.documents/document/521

vendor.js:1443 ERROR Error: Uncaught (in promise): Unknown URI: content://downloads/public_downloads/521
    at c (polyfills.js:3)
    at c (polyfills.js:3)
    at polyfills.js:3
    at t.invokeTask (polyfills.js:3)
    at Object.onInvokeTask (vendor.js:4499)
    at t.invokeTask (polyfills.js:3)
    at r.runTask (polyfills.js:3)
    at o (polyfills.js:3)

I am getting this issue in Android Oreo 8.1. Below Oreo, It is working fine. Please add support for oreo.

Thanks.

@gurwinder-macrew
Copy link

gurwinder-macrew commented Oct 16, 2018

I am able to fix the issue by adding in FilePath.java class. Please add below code in FilePath.java in getPath method.

Cursor cursor = null; try { cursor = context.getContentResolver().query(uri, new String[]{MediaStore.MediaColumns.DISPLAY_NAME}, null, null, null); if (cursor != null && cursor.moveToFirst()) { String fileName = cursor.getString(0); String path = Environment.getExternalStorageDirectory().toString() + "/Download/" + fileName; if (!TextUtils.isEmpty(path)) { return path; } } } finally { if (cursor != null) cursor.close(); }

Final code:

`package com.hiddentao.cordova.filepath;

import android.Manifest;
import android.content.ContentUris;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.provider.OpenableColumns;
import android.text.TextUtils;
import android.util.Log;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PermissionHelper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;

public class FilePath extends CordovaPlugin {

public static final int READ_REQ_CODE = 0;
public static final String READ = Manifest.permission.READ_EXTERNAL_STORAGE;
private static final String TAG = "[FilePath plugin]: ";
private static final int INVALID_ACTION_ERROR_CODE = -1;
private static final int GET_PATH_ERROR_CODE = 0;
private static final String GET_PATH_ERROR_ID = null;
private static final int GET_CLOUD_PATH_ERROR_CODE = 1;
private static final String GET_CLOUD_PATH_ERROR_ID = "cloud";
private static final int RC_READ_EXTERNAL_STORAGE = 5;
private static CallbackContext callback;
private static String uriStr;

/**
 * @param uri The Uri to check.
 * @return Whether the Uri authority is ExternalStorageProvider.
 */
private static boolean isExternalStorageDocument(Uri uri) {
    return "com.android.externalstorage.documents".equals(uri.getAuthority());
}

/**
 * @param uri The Uri to check.
 * @return Whether the Uri authority is DownloadsProvider.
 */
private static boolean isDownloadsDocument(Uri uri) {
    return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}

/**
 * @param uri The Uri to check.
 * @return Whether the Uri authority is MediaProvider.
 */
private static boolean isMediaDocument(Uri uri) {
    return "com.android.providers.media.documents".equals(uri.getAuthority());
}

/**
 * @param uri The Uri to check.
 * @return Whether the Uri authority is Google Photos.
 */
private static boolean isGooglePhotosUri(Uri uri) {
    return ("com.google.android.apps.photos.content".equals(uri.getAuthority())
            || "com.google.android.apps.photos.contentprovider".equals(uri.getAuthority()));
}

/**
 * @param uri The Uri to check.
 * @return Whether the Uri authority is Google Drive.
 */
private static boolean isGoogleDriveUri(Uri uri) {
    return "com.google.android.apps.docs.storage".equals(uri.getAuthority()) || "com.google.android.apps.docs.storage.legacy".equals(uri.getAuthority());
}

/**
 * Get the value of the data column for this Uri. This is useful for
 * MediaStore Uris, and other file-based ContentProviders.
 *
 * @param context       The context.
 * @param uri           The Uri to query.
 * @param selection     (Optional) Filter used in the query.
 * @param selectionArgs (Optional) Selection arguments used in the query.
 * @return The value of the _data column, which is typically a file path.
 */
private static String getDataColumn(Context context, Uri uri, String selection,
                                    String[] selectionArgs) {

    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = {
            column
    };

    try {
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                null);
        if (cursor != null && cursor.moveToFirst()) {
            final int column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

/**
 * Get content:// from segment list
 * In the new Uri Authority of Google Photos, the last segment is not the content:// anymore
 * So let's iterate through all segments and find the content uri!
 *
 * @param segments The list of segment
 */
private static String getContentFromSegments(List<String> segments) {
    String contentPath = "";

    for (String item : segments) {
        if (item.startsWith("content://")) {
            contentPath = item;
            break;
        }
    }

    return contentPath;
}

/**
 * Check if a file exists on device
 *
 * @param filePath The absolute file path
 */
private static boolean fileExists(String filePath) {
    File file = new File(filePath);

    return file.exists();
}

/**
 * Get full file path from external storage
 *
 * @param pathData The storage type and the relative path
 */
private static String getPathFromExtSD(String[] pathData) {
    final String type = pathData[0];
    final String relativePath = "/" + pathData[1];
    String fullPath = "";

    // on my Sony devices (4.4.4 & 5.1.1), `type` is a dynamic string
    // something like "71F8-2C0A", some kind of unique id per storage
    // don't know any API that can get the root path of that storage based on its id.
    //
    // so no "primary" type, but let the check here for other devices
    if ("primary".equalsIgnoreCase(type)) {
        fullPath = Environment.getExternalStorageDirectory() + relativePath;
        if (fileExists(fullPath)) {
            return fullPath;
        }
    }

    // Environment.isExternalStorageRemovable() is `true` for external and internal storage
    // so we cannot relay on it.
    //
    // instead, for each possible path, check if file exists
    // we'll start with secondary storage as this could be our (physically) removable sd card
    fullPath = System.getenv("SECONDARY_STORAGE") + relativePath;
    if (fileExists(fullPath)) {
        return fullPath;
    }

    fullPath = System.getenv("EXTERNAL_STORAGE") + relativePath;
    if (fileExists(fullPath)) {
        return fullPath;
    }

    return fullPath;
}

/**
 * Get a file path from a Uri. This will get the the path for Storage Access
 * Framework Documents, as well as the _data field for the MediaStore and
 * other file-based ContentProviders.<br>
 * <br>
 * Callers should check whether the path is local before assuming it
 * represents a local file.
 *
 * @param context The context.
 * @param uri     The Uri to query.
 */
private static String getPath(final Context context, final Uri uri) {

    Log.d(TAG, "File - " +
            "Authority: " + uri.getAuthority() +
            ", Fragment: " + uri.getFragment() +
            ", Port: " + uri.getPort() +
            ", Query: " + uri.getQuery() +
            ", Scheme: " + uri.getScheme() +
            ", Host: " + uri.getHost() +
            ", Segments: " + uri.getPathSegments().toString()
    );

    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

    // DocumentProvider
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
        // ExternalStorageProvider
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            String fullPath = getPathFromExtSD(split);
            if (fullPath != "") {
                return fullPath;
            } else {
                return null;
            }
        }
        // DownloadsProvider
        else if (isDownloadsDocument(uri)) {
            Cursor cursor = null;
            try {
                cursor = context.getContentResolver().query(uri, new String[]{MediaStore.MediaColumns.DISPLAY_NAME}, null, null, null);
                if (cursor != null && cursor.moveToFirst()) {
                    String fileName = cursor.getString(0);
                    String path = Environment.getExternalStorageDirectory().toString() + "/Download/" + fileName;
                    if (!TextUtils.isEmpty(path)) {
                        return path;
                    }
                }
            } finally {
                if (cursor != null)
                    cursor.close();
            }
            final String id = DocumentsContract.getDocumentId(uri);
            try {
                final Uri contentUri = ContentUris.withAppendedId(
                        Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

                return getDataColumn(context, contentUri, null, null);
            } catch (NumberFormatException e) {
                //In Android 8 and Android P the id is not a number
                return uri.getPath().replaceFirst("^/document/raw:", "").replaceFirst("^raw:", "");
            }
            /*String id = DocumentsContract.getDocumentId(uri);
            if (id.startsWith("raw:")) {
                return id.replaceFirst("raw:", "");
            }
            Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads"), Long.valueOf(id));

            return getDataColumn(context, contentUri, null, null);

*/
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];

            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            final String selection = "_id=?";
            final String[] selectionArgs = new String[]{
                    split[1]
            };

            return getDataColumn(context, contentUri, selection, selectionArgs);
        } else if (isGoogleDriveUri(uri)) {
            return getDriveFilePath(uri, context);
        }
    }
    // MediaStore (and general)
    else if ("content".equalsIgnoreCase(uri.getScheme())) {

        // Return the remote address
        if (isGooglePhotosUri(uri)) {
            String contentPath = getContentFromSegments(uri.getPathSegments());
            if (contentPath != "") {
                return getPath(context, Uri.parse(contentPath));
            } else {
                return null;
            }
        }

        if (isGoogleDriveUri(uri)) {
            return getDriveFilePath(uri, context);
        }

        return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

private static String getDriveFilePath(Uri uri, Context context) {
    Uri returnUri = uri;
    Cursor returnCursor = context.getContentResolver().query(returnUri, null, null, null, null);
    /*
     * Get the column indexes of the data in the Cursor,
     *     * move to the first row in the Cursor, get the data,
     *     * and display it.
     * */
    int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
    int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
    returnCursor.moveToFirst();
    String name = (returnCursor.getString(nameIndex));
    String size = (Long.toString(returnCursor.getLong(sizeIndex)));
    File file = new File(context.getCacheDir(), name);
    try {
        InputStream inputStream = context.getContentResolver().openInputStream(uri);
        FileOutputStream outputStream = new FileOutputStream(file);
        int read = 0;
        int maxBufferSize = 1 * 1024 * 1024;
        int bytesAvailable = inputStream.available();

        //int bufferSize = 1024;
        int bufferSize = Math.min(bytesAvailable, maxBufferSize);

        final byte[] buffers = new byte[bufferSize];
        while ((read = inputStream.read(buffers)) != -1) {
            outputStream.write(buffers, 0, read);
        }
        Log.e("File Size", "Size " + file.length());
        inputStream.close();
        outputStream.close();
        Log.e("File Path", "Path " + file.getPath());
        Log.e("File Size", "Size " + file.length());
    } catch (Exception e) {
        Log.e("Exception", e.getMessage());
    }
    return file.getPath();
}

protected void getReadPermission(int requestCode) {
    PermissionHelper.requestPermission(this, requestCode, READ);
}

public void initialize(CordovaInterface cordova, final CordovaWebView webView) {
    super.initialize(cordova, webView);
}

/**
 * Executes the request and returns PluginResult.
 *
 * @param action          The action to execute.
 * @param args            JSONArry of arguments for the plugin.
 * @param callbackContext The callback context through which to return stuff to caller.
 * @return A PluginResult object with a status and message.
 */
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    this.callback = callbackContext;
    this.uriStr = args.getString(0);

    if (action.equals("resolveNativePath")) {
        if (PermissionHelper.hasPermission(this, READ)) {
            resolveNativePath();
        } else {
            getReadPermission(READ_REQ_CODE);
        }

        return true;
    } else {
        JSONObject resultObj = new JSONObject();

        resultObj.put("code", INVALID_ACTION_ERROR_CODE);
        resultObj.put("message", "Invalid action.");

        callbackContext.error(resultObj);
    }

    return false;
}

public void resolveNativePath() throws JSONException {
    JSONObject resultObj = new JSONObject();
    /* content:///... */
    Uri pvUrl = Uri.parse(this.uriStr);

    Log.d(TAG, "URI: " + this.uriStr);

    Context appContext = this.cordova.getActivity().getApplicationContext();
    String filePath = getPath(appContext, pvUrl);

    //check result; send error/success callback
    if (filePath == GET_PATH_ERROR_ID) {
        resultObj.put("code", GET_PATH_ERROR_CODE);
        resultObj.put("message", "Unable to resolve filesystem path.");

        this.callback.error(resultObj);
    } else if (filePath.equals(GET_CLOUD_PATH_ERROR_ID)) {
        resultObj.put("code", GET_CLOUD_PATH_ERROR_CODE);
        resultObj.put("message", "Files from cloud cannot be resolved to filesystem, download is required.");

        this.callback.error(resultObj);
    } else {
        Log.d(TAG, "Filepath: " + filePath);

        this.callback.success("file://" + filePath);
    }
}

public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException {
    for (int r : grantResults) {
        if (r == PackageManager.PERMISSION_DENIED) {
            JSONObject resultObj = new JSONObject();
            resultObj.put("code", 3);
            resultObj.put("message", "Filesystem permission was denied.");

            this.callback.error(resultObj);
            return;
        }
    }

    if (requestCode == READ_REQ_CODE) {
        resolveNativePath();
    }
}

}
`

@olaferlandsen
Copy link
Contributor

Fixed in pull request #35

@distante
Copy link

distante commented Dec 1, 2018

I hope the pull request for this fix get merged soon! I am having the same problem.

@olaferlandsen
Copy link
Contributor

olaferlandsen commented Dec 1, 2018

I hope the pull request for this fix get merged soon! I am having the same problem.

Don't worry, reinstall this plugin using the git uri:
ionic cordova plugin add https://github.com/olaferlandsen/cordova-plugin-filepath

@distante
Copy link

distante commented Dec 3, 2018

I suppose is it safe to say that this repository is not longer maintained .

@olaferlandsen
Copy link
Contributor

I suppose is it safe to say that this repository is not longer maintained .

the last commit... 4 years ago

@PeterHdd
Copy link

PeterHdd commented Jan 2, 2019

Out of curiosity, since it is merged. Why doesn't this work ionic cordova plugin add cordova-plugin-filepath @olaferlandsen

@distante
Copy link

distante commented Jan 2, 2019

@PeterHdd
Copy link

PeterHdd commented Jan 3, 2019

Whenever I do:

ionic cordova plugin add https://github.com/olaferlandsen/cordova-plugin-filepath

I get the following error:

UnhandledPromiseRejectionWarning: CordovaError: Failed to fetch plugin https://github.com/olaferlandsen/cordova-plugin-filepath via registry.
Probably this is either a connection problem, or plugin spec is incorrect.
Check your connection and plugin name/version/URL.
Error: cmd: Command failed with exit code 1 Error output:
npm ERR! code 128
npm ERR! Command failed: C:\Program Files\Git\cmd\git.EXE submodule update -q --init --recursive
npm ERR! fatal: 'submodule' appears to be a git command, but we were not
npm ERR! able to execute it. Maybe git-submodule is broken?
npm ERR!

npm ERR! A complete log of this run can be found in:

How can this be solved?

@hiddentao
Copy link
Owner

Hey all, 1.5.0 has now been pushed, enjoy.

@hiddentao
Copy link
Owner

Sorry about the delay, someone else was supposed to be maintaining this repo but I'm not sure if they're still interested in doing so. I'm happy for anyone else who is keen to take over maintenance duties as I don't build with cordova anymore as it is. Alternatively, I can keep merging PRs that the community feel are worthy of addition.

@PeterHdd
Copy link

PeterHdd commented Jan 3, 2019

yes I can help you if you want

@gurwinder-macrew
Copy link

Yes, I can help you for this.

@hiddentao
Copy link
Owner

@PeterHdd @gurwinder-macrew Thanks a lot!

@denermiranda
Copy link

Having the same problem here. Any news on the fix?

@masterTail
Copy link

masterTail commented Nov 30, 2020

            Cursor cursor = null;
            try {
                cursor = context.getContentResolver().query(uri, new String[]{MediaStore.MediaColumns.DISPLAY_NAME}, null, null, null);
                if (cursor != null && cursor.moveToFirst()) {
                    String fileName = cursor.getString(0);
                    String path = Environment.getExternalStorageDirectory().toString() + "/Download/Browser/" + fileName;
                    if (!TextUtils.isEmpty(path)) {
                        return path;
                    }
                }
            } finally {
                if (cursor != null)
                cursor.close();
            }

I think " String path = Environment.getExternalStorageDirectory().toString() + "/Download/" + fileName;" isn't reasonable.
If the file is placed in the download subdirectory.This will cause the program to run abnormally

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants