2011-07-18 112 views
1

我想將視頻文件存儲到我的應用程序在SD卡中的文件夾。目前我正在拍攝一個視頻,我有它的URI。我怎樣才能保存該文件?請回復。提前致謝。將視頻文件保存到SD卡文件夾

回答

1
static void writeData(String fileurl, boolean append, String path, 
     String filename, Activity mContext) throws CustomException 
{ 
    URL myfileurl = null; 
    ByteArrayBuffer baf = null; 
    HttpURLConnection conn = null; 
    final int length; 
    try { 
     myfileurl = new URL(fileurl); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } 
    try { 
     conn = (HttpURLConnection) myfileurl 
       .openConnection(); 
     conn.setDoInput(true); 
     conn.connect(); 
     conn.setConnectTimeout(100000); 

     length = conn.getContentLength(); 
     System.out.println("total length.." + length); 

     int interval = (int)length/100; 
     if (length > 0) { 
      InputStream is = conn.getInputStream(); 
      BufferedInputStream bis = new BufferedInputStream(is); 
      baf = new ByteArrayBuffer(1000); 
      int current = 0; 

      while ((current = bis.read()) != -1) { 
       try { 
        baf.append((byte) current); 
        mBufferError=false; 
       } catch (Exception e){ 
        // TODO: handle exception 
        mBufferError=true; 
        System.out.println("buffer Problem"); 
        e.printStackTrace(); 
        throw new CustomException("@@@ memory problem ", "Buffer Error"); 
       } 
      } 
     } 

    } catch (IOException e) { 
     mBufferError=true; 
     System.out.println("HttpURLConnection"); 
     e.printStackTrace(); 

    } 
    try{ 
    if(conn.getResponseCode()==200 && mBufferError==false) 
    { 
     path = path + "/" + filename; 
     boolean appendData = append; 
     FileOutputStream foutstream; 

      File file = new File(path); 
     boolean exist = false; 

     try { 
      if (appendData) 
       exist = file.exists(); 
      else 
       exist = file.createNewFile(); 
     } catch (IOException e) { 
      try { 
       System.out.println("@@@ existed file :" + path); 
       return; 
      } catch (Exception err) { 
       Log.e("SAX", err.toString()); 
      } 
     } 
     if (!appendData && !exist) { 
     } else if (appendData && !exist) { 

     } else { 
      try { 
       foutstream = new FileOutputStream(file, appendData); 
       foutstream.write(baf.toByteArray()); 
       foutstream.close(); 
      } catch (Exception e) { 
       System.out.println("error in closing! " + e); 
       e.printStackTrace(); 
      } 
     } 
    } 
    }catch (Exception e) { 
     // TODO: handle exception 
     throw new CustomException("@@@ I/O problem ", "I/O Error"); 
    } 
} 

可能這個服務,其中fileurl =您的網址和路徑=本地路徑來存儲