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

AndroidContentProvider大约 1291 字

提供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;
}
阅读 395 · 发布于 2019-04-12

————        END        ————

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

昵称:
随便看看换一批