Giter Club home page Giter Club logo

Comments (8)

SanskarDahiya avatar SanskarDahiya commented on August 22, 2024 4

Hello guys,
I'm getting issue for same in Android 10,
as my file URI consist msf: format.
Most solutions said to copy files into cache path & then use that path.

Thus, here is the solution to get a path, without copying it.

 if (isAndroid10 && id.startsWith("msf:")) {
     final String[] split = id.split(":");
     final String selection = "_id=?";
     final String[] selectionArgs = new String[] { split[1] };
     return getDataColumn(context, MediaStore.Downloads.EXTERNAL_CONTENT_URI, selection, selectionArgs);
}

Try to implement this.

MediaStore.Downloads.EXTERNAL_CONTENT_URI -- ONLY PRESENT FROM ANDROID 10

from whatthecodec.

tamo avatar tamo commented on August 22, 2024

And I forgot to say a big thanks to your excellent article.

from whatthecodec.

Javernaut avatar Javernaut commented on August 22, 2024

Hello @tamo ,
Thanks for kind words :)

As for the issue, I managed to reproduce it by downloading a video file via the Chrome app on Android 10 emulator. Unfortunately, just skipping the 'msf:' part doesn't help to make a proper Uri that could lead us to a file path. That is why I decided to just fallback to the File Descriptor in this case. The code change is already merged.

Thanks for pointing this thing out.
Soon I'll make a new release with this change.

from whatthecodec.

tamo avatar tamo commented on August 22, 2024

Thanks for the fix.
Yes, I downloaded movie files via Chrome. I should have mentioned it. Sorry.

For now your fix seems the only way to go. Googling MSF tells me only about medical information.

And I'm looking forward to see the new release.

from whatthecodec.

AliElDerawi avatar AliElDerawi commented on August 22, 2024

I tried this solution and its worked.
It simply copies the file for the application cache directory , then gets the path of the generated file.

 if (id != null && id.startsWith("msf:")) {
                    final File file = new File(context.getCacheDir(), uri.getLastPathSegment());
                    try (final InputStream inputStream = context.getContentResolver().openInputStream(uri);
                         OutputStream output = new FileOutputStream(file)) {
                         // You may need to change buffer size. I use large buffer size to help loading large file , but be ware of 
                        //  OutOfMemory Exception
                        final byte[] buffer = new byte[8 * 1024]; 
                        int read;

                        while ((read = inputStream.read(buffer)) != -1) {
                            output.write(buffer, 0, read);
                        }

                        output.flush();
                        return file.getPath();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    return null;
                }

                

P.S. You may need to clear the cache directory after finished

from whatthecodec.

Javernaut avatar Javernaut commented on August 22, 2024

@AliElDerawi , you are trying to solve just a slightly different problem here. The original issue was about having the prefix in the URI.

After that I acutually found a way of accessing files by a FileDescriptor (even if it isn't perfect).

Regarding copying the file to the cache, I wouldn't do such a thing. Original files can be very big (like movies) and actually I don't want to copy user's data, because it would lead to my Privacy Policy changing.

from whatthecodec.

HBiSoft avatar HBiSoft commented on August 22, 2024

@SanskarDahiya
I tried what you said:

String id = DocumentsContract.getDocumentId(uri);
if (id.startsWith("msf")){
    final String[] split = id.split(":");
    final String selection = "_id=?";
    final String[] selectionArgs = new String[] { split[1] };
    Log.e("Returned ", getDataColumn(context, MediaStore.Downloads.EXTERNAL_CONTENT_URI, selection, selectionArgs));
}

The Uri above is content://com.android.providers.downloads.documents/document/msf%3A16803.

Here is my getDataColumn:

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 index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(index);
        }
    }catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

The cursor is null so getDataColumn returns null.

Is there something I'm doing wrong?

from whatthecodec.

SanskarDahiya avatar SanskarDahiya commented on August 22, 2024

@SanskarDahiya I tried what you said:

String id = DocumentsContract.getDocumentId(uri);
if (id.startsWith("msf")){
    final String[] split = id.split(":");
    final String selection = "_id=?";
    final String[] selectionArgs = new String[] { split[1] };
    Log.e("Returned ", getDataColumn(context, MediaStore.Downloads.EXTERNAL_CONTENT_URI, selection, selectionArgs));
}

The Uri above is content://com.android.providers.downloads.documents/document/msf%3A16803.

Here is my getDataColumn:

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 index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(index);
        }
    }catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

The cursor is null so getDataColumn returns null.

Is there something I'm doing wrong?

Hi @HBiSoft
You are doing everything fine. I'm also implementing the same code as mentioned by you.

Sill I'm facing an issue to get paths from some URI's in specific devices. I don't know what to do now.
Since I'm not an Android developer, So I kinda drop this issue.

from whatthecodec.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.