Android: save a file from an existing URI

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

提交回复
热议问题