Android ContentProvider 提供 Assets 目录下的图片或文件

Android About 1,294 words

提供 Assets 目录下文件

@Nullable
@Override
public AssetFileDescriptor openAssetFile(@NonNull Uri uri, @NonNull String mode) throws FileNotFoundException {
        AssetFileDescriptor afd = null;
        try {
            afd = mContext.getAssets().openFd("pic/" + imageName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return afd;
}

提供指定目录下文件

@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
    try {
        File logoFile = LoadLogo.getLogoFile(getContext(), number);

        if (logoFile != null && logoFile.exists()) {
            return ParcelFileDescriptor.open(logoFile, ParcelFileDescriptor.MODE_READ_ONLY);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    throw new FileNotFoundException(uri.getPath());
}


public static File getLogoFile(Context ctx, String number) {
    StringBuilder sbNumLogo = new StringBuilder(ctx.getFilesDir().getAbsolutePath());
    sbNumLogo.append(File.separator);
    sbNumLogo.append(LOGO_PATH);
    sbNumLogo.append(File.separator);
    sbNumLogo.append(PhoneNumberFormat.getFormattedNumber(number));

    File numLogo = new File(sbNumLogo.toString());
    if (numLogo.exists()) {
        return numLogo;
    }
    return null;
}
Views: 2,420 · Posted: 2019-04-12

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓
Today On History
Browsing Refresh