Android: save a file from an existing URI

后端 未结 8 915
半阙折子戏
半阙折子戏 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:58

    Use this method, it works

    void savefile(URI sourceuri)
    {
        String sourceFilename= sourceuri.getPath();
        String destinationFilename = android.os.Environment.getExternalStorageDirectory().getPath()+File.separatorChar+"abc.mp3";
    
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
    
        try {
          bis = new BufferedInputStream(new FileInputStream(sourceFilename));
          bos = new BufferedOutputStream(new FileOutputStream(destinationFilename, false));
          byte[] buf = new byte[1024];
          bis.read(buf);
          do {
            bos.write(buf);
          } while(bis.read(buf) != -1);
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
          try {
            if (bis != null) bis.close();
            if (bos != null) bos.close();
          } catch (IOException e) {
                e.printStackTrace();
          }
        }
    }
    
    0 讨论(0)
  • 2020-12-03 04:06

    You can do it using

    new File(uri.getPath());
    
    0 讨论(0)
提交回复
热议问题