Stickers: [Android] How do I load stickers from internal/external storage?

Created on 12 Nov 2018  路  12Comments  路  Source: WhatsApp/stickers

Android

Most helpful comment

Per my understanding of the source code, the content provider only allows loading stickers from asset folder. Is it possible to load from internal/external storage? One of the solutions I am looking at is to implement FileProvider instead. Any guidance on this would be really helpful.

All 12 comments

Per my understanding of the source code, the content provider only allows loading stickers from asset folder. Is it possible to load from internal/external storage? One of the solutions I am looking at is to implement FileProvider instead. Any guidance on this would be really helpful.

Yeah, i'm stuck at this, too :(

Even i want to know this thing :(

There is,
@Nullable
@Override
public ParcelFileDescriptor openFile(@NonNull final Uri uri, @NonNull final String mode) throws FileNotFoundException {
return super.openFile(uri, mode);
}

in ContentProvider, are you not able to leverage that?

@simonzhexu Thanks for the suggestion. I will try that

this works for me

    @Nullable
    @Override
    public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode) {
        final List<String> pathSegments = uri.getPathSegments();

        if (pathSegments.size() != 3) {
            throw new IllegalArgumentException("path segments should be 3, uri is: " + uri);
        }
        String fileName = pathSegments.get(pathSegments.size() - 1);
        final String identifier = pathSegments.get(pathSegments.size() - 2);

        if (TextUtils.isEmpty(identifier)) {
            throw new IllegalArgumentException("identifier is empty, uri: " + uri);
        }
        if (TextUtils.isEmpty(fileName)) {
            throw new IllegalArgumentException("file name is empty, uri: " + uri);
        }
        try{
            File file = new File(getContext().getFilesDir()+ "/" + identifier, fileName);
            return ParcelFileDescriptor.open(file,ParcelFileDescriptor.MODE_READ_ONLY);
        } catch (FileNotFoundException e){
            e.printStackTrace();
        }
        return null;
    }

Still can not able to use Assets + custom local storage stickers
every time receiving the stickers_asset and calls openAssetFile not openFile,
Anyone resolved or having the same issue?

this works for me

    @Nullable
    @Override
    public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode) {
        final List<String> pathSegments = uri.getPathSegments();

        if (pathSegments.size() != 3) {
            throw new IllegalArgumentException("path segments should be 3, uri is: " + uri);
        }
        String fileName = pathSegments.get(pathSegments.size() - 1);
        final String identifier = pathSegments.get(pathSegments.size() - 2);

        if (TextUtils.isEmpty(identifier)) {
            throw new IllegalArgumentException("identifier is empty, uri: " + uri);
        }
        if (TextUtils.isEmpty(fileName)) {
            throw new IllegalArgumentException("file name is empty, uri: " + uri);
        }
        try{
            File file = new File(getContext().getFilesDir()+ "/" + identifier, fileName);
            return ParcelFileDescriptor.open(file,ParcelFileDescriptor.MODE_READ_ONLY);
        } catch (FileNotFoundException e){
            e.printStackTrace();
        }
        return null;
    }

Hi. Where should I put this code?

It's in Content provider but the issue is that some stickers are in asset some in the local directory I don't know why but my file name with asset URI generated every time and where the actual URI generated in the code I don't know.

@parthdave93 @miladesign i guess we can't use both Assets and local storage in the same Content provider. you have to make one Content provider for Assets and other one for Local Storage.

if you want to load from internet then you can use this https://github.com/viztushar/stickers-internet
is not complete yet but you can check the code.

i have loaded the stickers for whatsapp from both assets folder and from gallery. For that, i have created two different provider in the app.

https://github.com/WhatsApp/stickers/blob/master/Android/README.md#expose-files-that-are-stored-internally-as-stickers-through-contentprovider

Here are some instructions

Was this page helpful?
0 / 5 - 0 ratings