Anju
Anju

Reputation: 9479

Save a video file to sd card folder

I want to store the video file to my app's folder in sd card. Currently I am taking a video and I have its URI. How can I save that file? Please reply. Thanks in advance.

Upvotes: 1

Views: 2173

Answers (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");
    }
}

may this serves, where fileurl= your Url and path= local path to store

Upvotes: 1

Related Questions