如何解决我如何在 whatsapp 贴纸包应用程序中使用相机图像或图库图像作为自定义贴纸
-
Here is the content provider class is there any way to pick image from gallery and after converting into webp how can i use it for whatsappp sticker??
公共类 StickerContentProvider 扩展了 ContentProvider { @覆盖 公共布尔 onCreate() { final String authority = BuildConfig.CONTENT_PROVIDER_AUTHORITY; if (!authority.startsWith(Objects.requireNonNull(getContext()).getPackageName())) { throw new IllegalStateException("您对内容提供者的权限 (" + authority + ") 应以您的包名开头:" + getContext().getPackageName()); }
//the call to get the Metadata for the sticker packs. MATCHER.addURI(authority,MetaDATA,MetaDATA_CODE); //the call to get the Metadata for single sticker pack. * represent the identifier MATCHER.addURI(authority,MetaDATA + "/*",MetaDATA_CODE_FOR_SINGLE_PACK); //gets the list of stickers for a sticker pack,* respresent the identifier. MATCHER.addURI(authority,STICKERS + "/*",STICKERS_CODE); for (StickerPack stickerPack : getStickerPackList()) { MATCHER.addURI(authority,STICKERS_ASSET + "/" + stickerPack.identifier + "/" + stickerPack.trayImageFile,STICKER_PACK_TRAY_ICON_CODE); for (Sticker sticker : stickerPack.getStickers()) { MATCHER.addURI(authority,STICKERS_ASSET + "/" + stickerPack.identifier + "/" + sticker.imageFileName,STICKERS_ASSET_CODE); } } return true; } @Override public Cursor query(@NonNull Uri uri,@Nullable String[] projection,String selection,String[] selectionArgs,String sortOrder) { final int code = MATCHER.match(uri); if (code == MetaDATA_CODE) { return getPackForAllStickerPacks(uri); } else if (code == MetaDATA_CODE_FOR_SINGLE_PACK) { return getCursorForSingleStickerPack(uri); } else if (code == STICKERS_CODE) { return getStickersForAStickerPack(uri); } else { throw new IllegalArgumentException("UnkNown URI: " + uri); } } @Nullable @Override public AssetFileDescriptor openAssetFile(@NonNull Uri uri,@NonNull String mode) { final int matchCode = MATCHER.match(uri); if (matchCode == STICKERS_ASSET_CODE || matchCode == STICKER_PACK_TRAY_ICON_CODE) { return getimageAsset(uri); } return null; } @Override public String getType(@NonNull Uri uri) { final int matchCode = MATCHER.match(uri); switch (matchCode) { case MetaDATA_CODE: return "vnd.android.cursor.dir/vnd." + BuildConfig.CONTENT_PROVIDER_AUTHORITY + "." + MetaDATA; case MetaDATA_CODE_FOR_SINGLE_PACK: return "vnd.android.cursor.item/vnd." + BuildConfig.CONTENT_PROVIDER_AUTHORITY + "." + MetaDATA; case STICKERS_CODE: return "vnd.android.cursor.dir/vnd." + BuildConfig.CONTENT_PROVIDER_AUTHORITY + "." + STICKERS; case STICKERS_ASSET_CODE: return "image/webp"; case STICKER_PACK_TRAY_ICON_CODE: return "image/png"; default: throw new IllegalArgumentException("UnkNown URI: " + uri); } } private synchronized void readContentFile(@NonNull Context context) { try (InputStream contentsInputStream = context.getAssets().open(CONTENT_FILE_NAME)) { stickerPackList = ContentFileParser.parseStickerPacks(contentsInputStream); } catch (IOException | IllegalStateException e) { throw new RuntimeException(CONTENT_FILE_NAME + " file has some issues: " + e.getMessage(),e); } } private List<StickerPack> getStickerPackList() { if (stickerPackList == null) { readContentFile(Objects.requireNonNull(getContext())); } return stickerPackList; } private Cursor getPackForAllStickerPacks(@NonNull Uri uri) { return getStickerPackInfo(uri,getStickerPackList()); } private Cursor getCursorForSingleStickerPack(@NonNull Uri uri) { final String identifier = uri.getLastPathSegment(); for (StickerPack stickerPack : getStickerPackList()) { if (identifier.equals(stickerPack.identifier)) { return getStickerPackInfo(uri,Collections.singletonList(stickerPack)); } } return getStickerPackInfo(uri,new ArrayList<>()); } @NonNull private Cursor getStickerPackInfo(@NonNull Uri uri,@NonNull List<StickerPack> stickerPackList) { MatrixCursor cursor = new MatrixCursor( new String[]{ STICKER_PACK_IDENTIFIER_IN_QUERY,STICKER_PACK_NAME_IN_QUERY,STICKER_PACK_PUBLISHER_IN_QUERY,STICKER_PACK_ICON_IN_QUERY,ANDROID_APP_DOWNLOAD_LINK_IN_QUERY,IOS_APP_DOWNLOAD_LINK_IN_QUERY,PUBLISHER_EMAIL,PUBLISHER_WEBSITE,PRIVACY_POLICY_WEBSITE,LICENSE_AGREENMENT_WEBSITE,IMAGE_DATA_VERSION,AVOID_CACHE,ANIMATED_STICKER_PACK,}); for (StickerPack stickerPack : stickerPackList) { MatrixCursor.RowBuilder builder = cursor.newRow(); builder.add(stickerPack.identifier); builder.add(stickerPack.name); builder.add(stickerPack.publisher); builder.add(stickerPack.trayImageFile); builder.add(stickerPack.androidplayStoreLink); builder.add(stickerPack.iosAppStoreLink); builder.add(stickerPack.publisherEmail); builder.add(stickerPack.publisherWebsite); builder.add(stickerPack.privacyPolicyWebsite); builder.add(stickerPack.licenseAgreementWebsite); builder.add(stickerPack.imageDataVersion); builder.add(stickerPack.avoidCache ? 1 : 0); builder.add(stickerPack.animatedStickerPack ? 1 : 0); } cursor.setNotificationUri(Objects.requireNonNull(getContext()).getContentResolver(),uri); return cursor; } @NonNull private Cursor getStickersForAStickerPack(@NonNull Uri uri) { final String identifier = uri.getLastPathSegment(); MatrixCursor cursor = new MatrixCursor(new String[]{STICKER_FILE_NAME_IN_QUERY,STICKER_FILE_EMOJI_IN_QUERY}); for (StickerPack stickerPack : getStickerPackList()) { if (identifier.equals(stickerPack.identifier)) { for (Sticker sticker : stickerPack.getStickers()) { cursor.addRow(new Object[]{sticker.imageFileName,TextUtils.join(",",sticker.emojis)}); } } } cursor.setNotificationUri(Objects.requireNonNull(getContext()).getContentResolver(),uri); return cursor; } private AssetFileDescriptor getimageAsset(Uri uri) throws IllegalArgumentException { AssetManager am = Objects.requireNonNull(getContext()).getAssets(); 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); } //making sure the file that is trying to be fetched is in the list of stickers. for (StickerPack stickerPack : getStickerPackList()) { if (identifier.equals(stickerPack.identifier)) { if (fileName.equals(stickerPack.trayImageFile)) { return fetchFile(uri,am,fileName,identifier); } else { for (Sticker sticker : stickerPack.getStickers()) { if (fileName.equals(sticker.imageFileName)) { return fetchFile(uri,identifier); } } } } } return null; } private AssetFileDescriptor fetchFile(@NonNull Uri uri,@NonNull AssetManager am,@NonNull String fileName,@NonNull String identifier) { try { return am.openFd(identifier + "/" + fileName); } catch (IOException e) { Log.e(Objects.requireNonNull(getContext()).getPackageName(),"IOException when getting asset file,uri:" + uri,e); return null; } } @Override public int delete(@NonNull Uri uri,@Nullable String selection,String[] selectionArgs) { throw new UnsupportedOperationException("Not supported"); } @Override public Uri insert(@NonNull Uri uri,ContentValues values) { throw new UnsupportedOperationException("Not supported"); } @Override public int update(@NonNull Uri uri,ContentValues values,String[] selectionArgs) { throw new UnsupportedOperationException("Not supported"); }
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。