美文网首页
Android-拷贝其他APP私有文件到自己APP私有目录下

Android-拷贝其他APP私有文件到自己APP私有目录下

作者: Android14k纯金大佬 | 来源:发表于2023-01-06 13:24 被阅读0次
 /**
     * 用流拷贝文件一份到自己APP私有目录下
     *
     * @param context
     * @param uri
     * @param fileName
     */
    public static String getPathFromInputStreamUri(Context context, Uri uri, String fileName) {
        InputStream inputStream = null;
        String filePath = null;

        if (uri.getAuthority() != null) {
            try {
                inputStream = context.getContentResolver().openInputStream(uri);
                File file = createTemporalFileFrom(inputStream, fileName);
                filePath = file.getPath();
            } catch (Exception e) {
            } finally {
                try {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                } catch (Exception e) {
                }
            }
        }

        return filePath;
    }

    private static File createTemporalFileFrom(InputStream inputStream, String fileName)
            throws IOException {
        File targetFile = null;

        if (inputStream != null) {
            int read;
            byte[] buffer = new byte[8 * 1024];
            //自己定义拷贝文件路径
            targetFile = new File(PathUtils.getInternalAppFilesPath(), fileName);
            if (targetFile.exists()) {
                targetFile.delete();
            }
            OutputStream outputStream = new FileOutputStream(targetFile);
            while ((read = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, read);
            }
            outputStream.flush();

            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return targetFile;
    }

PathUtils.getInternalAppFilesPath()私有目录文件路径

相关文章

网友评论

      本文标题:Android-拷贝其他APP私有文件到自己APP私有目录下

      本文链接:https://www.haomeiwen.com/subject/blcjcdtx.html