Android: save a file from an existing URI

后端 未结 8 986
半阙折子戏
半阙折子戏 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();
        }
    }
    

提交回复
热议问题