处理加密的文件并为它们提供FileProvider [英] Work with encrypted file and provide them with FileProvider

查看:159
本文介绍了处理加密的文件并为它们提供FileProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android应用中,我需要从FileProvider中公开一些文件.没有加密,这很容易:我只需将FileProvider添加到manifest.xml.

In my Android app, I need to expose some file from a FileProvider. Without encryption, it's quite easy: I simply add the FileProvider to manifest.xml.

<provider
   android:name="androidx.core.content.FileProvider"
   android:authorities="${applicationId}.provider"
   android:exported="false"
   android:grantUriPermissions="true">
   <meta-data
     android:name="android.support.FILE_PROVIDER_PATHS"
     android:resource="@xml/provider_paths" />
</provider>

我需要接收一些文件,对其进行加密(将其秘密存储在数据库中),然后使用FileProvider公开它们.我该怎么办?

I need to receive some files, encrypt them (storing its secrets in a database) and then expose them with a FileProvider. How can I do this?

谢谢

推荐答案

您可以像这个问题.

例如,Word应用程序通过使用管道ParcelFileDescriptor可以很好地工作,对于图像,您可能需要创建文件的临时副本并将其提供.

The Word app for example works well with using a piped ParcelFileDescriptor, for images you will probably need to create a temporary copy of the file and serve that.

这里是一个示例,显示了如何查找Word文件之类的文件:

Here is an example of how that might look for files like Word files and similar:

@Nullable
@Override
public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode) throws FileNotFoundException {
    ParcelFileDescriptor[] pipe = null;
    try {
        pipe = ParcelFileDescriptor.createReliablePipe();
    } catch (IOException e) {
        Log.d(TAG, "Error creating pipe", e);
    }
    if (mode.contains("r")) {
        FileInputStream fis = FileEncryptionWrapper.getEncryptedFileInputStream(getContext(), uri);
        new PipeFeederThread(fis, new ParcelFileDescriptor.AutoCloseOutputStream(pipe[1])).start();
        return pipe[0];
    } else if (mode.contains("w")) {
        FileOutputStream fos = FileEncryptionWrapper.getEncryptedFileOutputStream(getContext(), uri);
        new PipeFeederThread(new ParcelFileDescriptor.AutoCloseInputStream(pipe[0]), fos).start();
        return pipe[1];
    }
    return null;
}

它使用PipeFeederThread将内容从您的Stream中获取到读/写端:

It uses a PipeFeederThread to get the content from your Stream to the reading/writing side:

static class PipeFeederThread extends Thread {
    InputStream in;
    OutputStream out;

    PipeFeederThread(InputStream in, OutputStream out) {
        this.in = in;
        this.out = out;
    }

    @Override
    public void run() {
        byte[] buf = new byte[8192];
        int len;

        try {
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.flush();
            out.close();
        } catch (IOException e) {
            Log.e(TAG, "PipeFeederThread: Data transfer failed:", e);
        }
    }
}

FileProvider也需要在AndroidManifest.xml中声明:

<provider
    android:name=".InternalFileProvider"
    android:authorities="com.android.prototypes.encryptedimagefileprovider.InternalFileProvider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

file_paths.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths>
   <files-path name="files" path="./" />
</paths>

不幸的是,到目前为止,我还没有找到一个好的一刀切"的解决方案,并且仍在寻找中.似乎还没有以一种干净,一致的方式解决将不同类型的加密文件导出到其他应用程序的问题.

I unfortunately haven't found a good "one size fits all" solution so far and am still searching. It seems that exporting different types of encrypted files to other apps is not yet solved in a clean and consistent way.

这篇关于处理加密的文件并为它们提供FileProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆