Android: save a file from an existing URI

后端 未结 8 914
半阙折子戏
半阙折子戏 2020-12-03 03:39

How to save a media file (say .mp3) from an existing URI, which I am getting from an Implicit Intent?

相关标签:
8条回答
  • 2020-12-03 03:45

    Here's the easiest and the cleanest:

    private void saveFile(Uri sourceUri, File destination)
        try {
            File source = new File(sourceUri.getPath());
            FileChannel src = new FileInputStream(source).getChannel();
            FileChannel dst = new FileOutputStream(destination).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    0 讨论(0)
  • 2020-12-03 03:46
    private static String FILE_NAM  = "video";
    String outputfile = getFilesDir() + File.separator+FILE_NAM+"_tmp.mp4";
    
    InputStream in = getContentResolver().openInputStream(videoFileUri);
    private static File createFileFromInputStream(InputStream inputStream, String fileName) {
    
        try {
            File f = new File(fileName);
            f.setWritable(true, false);
            OutputStream outputStream = new FileOutputStream(f);
            byte buffer[] = new byte[1024];
            int length = 0;
    
            while((length=inputStream.read(buffer)) > 0) {
                outputStream.write(buffer,0,length);
            }
    
            outputStream.close();
            inputStream.close();
    
            return f;
        } catch (IOException e) {
            System.out.println("error in creating a file");
            e.printStackTrace();
        }
    
        return null;
    }
    
    0 讨论(0)
  • 2020-12-03 03:47

    I have used following code to save a file from an existing Uri given back from an Intent to an Uri that my App hosts:

     private void copyFile(Uri pathFrom, Uri pathTo) throws IOException {
            try (InputStream in = getContentResolver().openInputStream(pathFrom)) {
                if(in == null) return;
                try (OutputStream out = getContentResolver().openOutputStream(pathTo)) {
                    if(out == null) return;
                    // Transfer bytes from in to out
                    byte[] buf = new byte[1024];
                    int len;
                    while ((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-03 03:49

    If Uri is received from Google Drive, it can be a Virtual File Uri too. Check this article from CommonsWare for more information. So you have to consider that condition too while saving file from Uri.

    To find if file Uri is virtual or not you can use

    private static boolean isVirtualFile(Context context, Uri uri) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (!DocumentsContract.isDocumentUri(context, uri)) {
                return false;
            }
            Cursor cursor = context.getContentResolver().query(
                    uri,
                    new String[]{DocumentsContract.Document.COLUMN_FLAGS},
                    null, null, null);
            int flags = 0;
            if (cursor.moveToFirst()) {
                flags = cursor.getInt(0);
            }
            cursor.close();
            return (flags & DocumentsContract.Document.FLAG_VIRTUAL_DOCUMENT) != 0;
        } else {
            return false;
        }
    }
    

    You can get the stream data from this virtual file like this:

    private static InputStream getInputStreamForVirtualFile(Context context, Uri uri, String mimeTypeFilter)
            throws IOException {
    
        ContentResolver resolver = context.getContentResolver();
        String[] openableMimeTypes = resolver.getStreamTypes(uri, mimeTypeFilter);
        if (openableMimeTypes == null || openableMimeTypes.length < 1) {
            throw new FileNotFoundException();
        }
        return resolver
                .openTypedAssetFileDescriptor(uri, openableMimeTypes[0], null)
                .createInputStream();
    }
    

    For finding MIME type try

    private static String getMimeType(String url) {
        String type = null;
        String extension = MimeTypeMap.getFileExtensionFromUrl(url);
        if (extension != null) {
            type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        }
        return type;
    }
    

    Overall, you can use

    public static boolean saveFile(Context context, String name, Uri sourceuri, String destinationDir, String destFileName) {
    
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        InputStream input = null;
        boolean hasError = false;
    
        try {
            if (isVirtualFile(context, sourceuri)) {
                input = getInputStreamForVirtualFile(context, sourceuri, getMimeType(name));
            } else {
                input = context.getContentResolver().openInputStream(sourceuri);
            }
    
            boolean directorySetupResult;
            File destDir = new File(destinationDir);
            if (!destDir.exists()) {
                directorySetupResult = destDir.mkdirs();
            } else if (!destDir.isDirectory()) {
                directorySetupResult = replaceFileWithDir(destinationDir);
            } else {
                directorySetupResult = true;
            }
    
            if (!directorySetupResult) {
                hasError = true;
            } else {
                String destination = destinationDir + File.separator + destFileName;
                int originalsize = input.available();
    
                bis = new BufferedInputStream(input);
                bos = new BufferedOutputStream(new FileOutputStream(destination));
                byte[] buf = new byte[originalsize];
                bis.read(buf);
                do {
                    bos.write(buf);
                } while (bis.read(buf) != -1);
            }
        } catch (Exception e) {
            e.printStackTrace();
            hasError = true;
        } finally {
            try {
                if (bos != null) {
                    bos.flush();
                    bos.close();
                }
            } catch (Exception ignored) {
            }
        }
    
        return !hasError;
    }
    
    private static boolean replaceFileWithDir(String path) {
        File file = new File(path);
        if (!file.exists()) {
            if (file.mkdirs()) {
                return true;
            }
        } else if (file.delete()) {
            File folder = new File(path);
            if (folder.mkdirs()) {
                return true;
            }
        }
        return false;
    }
    

    Call this method from an AsycTask. Let me know if this helps.

    0 讨论(0)
  • 2020-12-03 03:53

    1.Create a file from a URI path as:

    File from = new File(uri.toString());
    

    2.Create another File where you want the file to save as:

    File to = new File("target file path");
    

    3.Rename the file as:

    from.renameTo(to);
    

    With this the file from default path is automatically deleted and created at the new path.

    0 讨论(0)
  • 2020-12-03 03:53

    When receiving a android.net.Uri from an external source, the best way to save the file is from the stream:

    try (InputStream ins = activity.getContentResolver().openInputStream(source_uri)) {
        File dest = new File(destination_path);
        createFileFromStream(ins, dest);
    } catch (Exception ex) {
        Log.e("Save File", ex.getMessage());
        ex.printStackTrace();
    }
    

    createFileFromStream method:

    public static void createFileFromStream(InputStream ins, File destination) {
        try (OutputStream os = new FileOutputStream(destination)) {
            byte[] buffer = new byte[4096];
            int length;
            while ((length = ins.read(buffer)) > 0) {
                os.write(buffer, 0, length);
            }
            os.flush();
        } catch (Exception ex) {
            Log.e("Save File", ex.getMessage());
            ex.printStackTrace();
        }
    }
    
    0 讨论(0)
提交回复
热议问题