0

The library I'm using requires me to pass it a file path.

Currently, I use the Uri to create a new file (in an AsyncTask) as shown below:

@Override
protected String doInBackground(Uri... params) {
    File file = null;
    int size = -1;
    try {
        try {
            if (returnCursor != null && returnCursor.moveToFirst()){
                int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
                size = (int) returnCursor.getLong(sizeIndex);
            }
        }
        finally {
            if (returnCursor != null)
            returnCursor.close();
        }

        if (extension == null){
            pathPlusName = folder + "/" + filename;
            file = new File(folder + "/" + filename);
        }else {
            pathPlusName = folder + "/" + filename + "." + extension;
            file = new File(folder + "/" + filename + "." + extension);
        }

        BufferedInputStream bis = new BufferedInputStream(is);
        FileOutputStream fos = new FileOutputStream(file);

        byte[] data = new byte[1024];
        long total = 0;
        int count;
        while ((count = bis.read(data)) != -1) {
            if (!isCancelled()) {
                total += count;
                if (size != -1) {
                    publishProgress((int) ((total * 100) / size));
                }
                fos.write(data, 0, count);
            }
        }
        fos.flush();
        fos.close();

    } catch (IOException e) {
        errorReason = e.getMessage();
    }
    return file.getAbsolutePath();
}

As you can imagine, this might take some time depending on the file size.

Question

Is there any way to access the file without having to create/copy it?

*I have read that I can use ContentResolver and methods like openInputStream() and openOutputStream(). But this will only provide me with a stream and does not solve the issue I have of having to write a new file?


Extra info:

I get the Uri by passing the following to an Intent:

intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);

Then in onActivityResult I get the Uri by calling data.getData().


Please do not provide me with solutions like this one. I do not want to get a file Uri from a content Uri because it's unreliable and incorrect.

KRK
  • 99
  • 11

1 Answers1

0

It looks pretty clear that a library that cannot handle a content scheme is of no use if you only have a content scheme.

You have to make a copy of the content in a file. As you do now.

blackapps
  • 8,011
  • 2
  • 11
  • 25