Java 使用加密文件并向其提供FileProvider

Java 使用加密文件并向其提供FileProvider,java,android,cryptography,android-fileprovider,fileprovider,Java,Android,Cryptography,Android Fileprovider,Fileprovider,在我的Android应用程序中,我需要公开来自文件提供商的一些文件。没有加密,这很容易:我只需将FileProvider添加到manifest.xml 我需要接收一些文件,对它们进行加密(将其机密存储在数据库中),然后向文件提供者公开它们。我该怎么做 谢谢您可以像在中一样滚动您自己的文件提供程序。根据您想要提供的文件类型和应该查看该文件的应用程序,您可以选择一种策略,这在中有一点讨论 例如,Word app可以很好地使用管道ParcelFileDescriptor,对于图像,您可能需要创建文

在我的Android应用程序中,我需要公开来自文件提供商的一些文件。没有加密,这很容易:我只需将
FileProvider
添加到
manifest.xml


我需要接收一些文件,对它们进行加密(将其机密存储在数据库中),然后向文件提供者公开它们。我该怎么做


谢谢

您可以像在中一样滚动您自己的
文件提供程序。根据您想要提供的文件类型和应该查看该文件的应用程序,您可以选择一种策略,这在中有一点讨论

例如,Word app可以很好地使用管道
ParcelFileDescriptor
,对于图像,您可能需要创建文件的临时副本并提供该副本

下面是一个示例,说明如何查找Word文件和类似文件:

@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
将内容从流获取到读/写端:

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>
<?xml version="1.0" encoding="utf-8"?>
<paths>
   <files-path name="files" path="./" />
</paths>

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

您可以像中一样滚动自己的
文件提供者。根据您想要提供的文件类型和应该查看该文件的应用程序,您可以选择一种策略,这在中有一点讨论

例如,Word app可以很好地使用管道
ParcelFileDescriptor
,对于图像,您可能需要创建文件的临时副本并提供该副本

下面是一个示例,说明如何查找Word文件和类似文件:

@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
将内容从流获取到读/写端:

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>
<?xml version="1.0" encoding="utf-8"?>
<paths>
   <files-path name="files" path="./" />
</paths>

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

加密文件仍然是文件,对吗?或者您希望实现某种特定的自动化,以便接收解密的文件?我需要在本地文件系统中存储加密的文件,但我希望导出解密的文件。我想这部分文档中隐含了技巧:“如果要覆盖
FileProvider
方法的任何默认行为,请扩展
FileProvider
类,并在
元素的android:name属性中使用完全限定的类名。“但是我没有尝试过,所以我不能确定。否则,
ContentProvider
可能更有意义……这就是问题(和目标答案)如何编写一个FileProvider/ContentProvider,从一个加密文件开始提供一个未加密的文件?我也很感兴趣。一个加密文件仍然是一个文件,对吗?或者你希望有一些特定的自动化,以便你收到一个解密文件?我需要在本地文件系统中存储一个加密文件,但我希望导出解密的文件。我想这部分文档中隐含了一个技巧:“如果您想覆盖
FileProvider
方法的任何默认行为,请扩展
FileProvider
类,并在
元素的android:name属性中使用完全限定的类名。“但我还没有尝试过,所以我不能确定。否则,
ContentProvider
可能更有意义……问题(以及目标答案)是如何编写一个文件提供程序/ContentProvider,从加密文件开始提供未加密的文件?我也会感兴趣。