0

I am using okhttp for downloading video from server. there is no error no exception but the file is not downloading every where but it seems as it is.

Here is the code:

OkHttpClient httpClient = new OkHttpClient();
Call call = httpClient.newCall(new Request.Builder().url("http://res.cloudinary.com/demo/video/upload/v1427018743/ygzxwxmflekucvqcrb8c.mp4").get().build());
 try {
        File file = new File(getCacheDir(), user_Videos.get(i).video_title+ ".mp4");
        OutputStream out = new FileOutputStream(file);
        Response response = call.execute();
        if (response.code() == 200) {
           InputStream inputStream = null;
           try {
                inputStream = response.body().byteStream();
                byte[] buff = new byte[1024 * 8];
                long downloaded = 0;
                long target = response.body().contentLength();

                 publishProgress(0L, target);
                 while (true) {
                      int readed = inputStream.read(buff);
                       if (readed == -1) {
                           break;
                                        }
                          //write buff
                     downloaded += readed;

              try {
                   out.write(buff,0,readed);

                  } catch (Exception e) {
                       e.printStackTrace();
                                        }
                   publishProgress(downloaded, target);
                     if (isCancelled()) {
                                            return false;
                                        }
                                    }
                                    return downloaded == target;
                                } catch (IOException ignore) {
                                    return false;
                                } finally {
                                    if (inputStream != null) {
                                        out.flush();
                                        out.close();
                                        inputStream.close();
                                    }
                                }
                            } else {
                                return false;
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                            return false;
                        }

The progress is showing correctly but video is not showing in directory.

Thanks.

Sreehari
  • 5,621
  • 2
  • 25
  • 59
Syeda Zunaira
  • 5,191
  • 3
  • 38
  • 70

1 Answers1

0

So there is no problem in my code but the path. Thanks @greenapps who made me think about path/ directory.

Basically I just change the path to a real one instead of cache and yup here is the video.

Changed this line

 File file = new File(getCacheDir(), user_Videos.get(i).video_title  + ".mp4");

to this

 File file = new File(Environment.getExternalStorageDirectory(), user_Videos.get(i).video_title+ ".mp4");

Thanks @greenapps for the clue.

Syeda Zunaira
  • 5,191
  • 3
  • 38
  • 70