将视频文件保存到SD卡文件夹

时间:2011-07-18 04:32:39

标签: android video

我想将视频文件存储到sd卡中我应用的文件夹中。目前我正在拍摄视频并且我有自己的URI。我该如何保存该文件?请回复。提前谢谢。

1 个答案:

答案 0 :(得分: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 =您的Url和path =存储的本地路径