InputStream Byte

Get path of file

      File a= result;
                  String path = a.getAbsolutePath();

File

Create subfoler

Try to use mkdirs() instead of mkdir() only, this worked for me.
Example:
File folder = new File(Environment.getExternalStorageDirectory() + "/myapp/folderone/foldertwo");
    boolean success = false;
    if (!folder.exists()) {
        success = folder.mkdirs();
    }
    if (!success) {
    } else {
    }
+Create path inside File folder = new File(getFilesDir() + "/MyDownloaded/");
Reading 10 bytes from InputStream into a array byte input.
byte[] input = new byte[10];
for (int i = 0; i < input.length; i++) {
int b = in.read( );
if (b == -1) break;
 input[i] = (byte) b;
}
+                       // http://srv68.vidtomp3.com/download/tNTEp6emr5SwZG5qlpaacXBg5KWmqW9w4pSXam5knGllamm0vMzHrKid2GU=/Tinhte.vn%20-%20Tre%3Fn%20tay%20Samsung%20Galaxy%20S6%20Edge.mp3
                              URL url = new URL(array.get(i));
                              HttpURLConnection urlConnection = (HttpURLConnection) url
                                          .openConnection();

                              // set up some things on the connection
                              urlConnection.setRequestMethod("GET");
                              urlConnection.setDoOutput(true);
                              urlConnection.connect();

                       
                              InputStream inputStream = urlConnection.getInputStream();

                              // this is the total size of the file
                              int totalSize = urlConnection.getContentLength();
                              // variable to store total downloaded bytes
                              int downloadedSize = 0;

// create a buffer...đây là bộ đệm chứa dữ liệu,giá trị cần đọc hoặc ghi mỗi lần là 1kb
                              byte[] buffer = new byte[1024];
                              int bufferLength = 0;
// Chiều dài hay số bytes đã đọc hoặc ghi,tùy thuộc vào mạnh yếu của mạng maximum cũng chỉ = kích thước bộ đệm ở đây là 1kb
                                                           
//Buffer bộ đệm số bytes cần đọc,ở đây 1kb. read(buffer) là cần đọc 1kb nhưng đọc chưa chắc đã đc 1kb
while ((bufferLength = inputStream.read(buffer)) > 0) {

//buffer bộ đệm dữ liệu chứa dòng (10101010001 nul nul nul nul),nói chung nó là 1 mảng 1024 phần tử kiểu bytes,có bao nhiêu phần tử được dùng thì bufferLength chiều dài hay số bytes được ghi.Phương thức này có tác dụng lấy ghi dữ liệu từ bộ đệm từ (vị trí 0 là bắt đầu đến int=bufferLenght) còn đoạn cuối là null null null thì không lấy
                                    fileOutput.write(buffer,0, bufferLength);
// Nếu mà kiểu này fileOutput.write(buffer); thì nó sẽ là dòng dữ liệu (010010101001110110 nul nul nul 01010101010101nul nul nul….)như vậy kích thước file sẽ tăng lên và nếu là file mp3 nghe sẽ ngắt quãng rồi chạy lại ngẵng quãng)

                                    System.out.println(buffer);
                                    System.out.println(bufferLength);
                        // add up the size so we know how much is downloaded
                                    downloadedSize += bufferLength;
   // this is where you would do something to report the
                 // prgress,like this maybe updateProgress(downloadedSize, totalSize);
                                    Log.w("DOWNLOAD", "progress " + downloadedSize + " / " + totalSize);


                              }
SHARE

Nguyễn Văn Duy

  • Image
  • Image
  • Image
  • Image
  • Image

0 comments:

Post a Comment