PDA

View Full Version : برنامه دانلودر اندرویدی



zzz123
شنبه 29 آبان 1395, 13:09 عصر
با سلام می خواستم بدونم چطوری آرایه ای از OutputStream و InputStream درست کنیم و ذخیره در آرایه ای از file داشته باشیم کد من اینه
package com.example.quranlovehamrah;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;











import java.util.Arrays;


import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

public class ProgressDialogDemoActivity extends Activity {

private Button btnPlayMusic;



private ProgressDialog prgDialog;

private String[] myArray_2;

private String[] fdown;

private String[] fpath;

public static final int progress_bar_type = 0;

File file;

ProgressBar progressBar1;
File sdCard;

String fdownString;
String fpathString;
File externalFile;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);


Bundle ex = getIntent().getExtras();
if (ex != null) {
myArray_2 = ex.getStringArray("array1");

}


externalFile = new File(Environment.getExternalStorageDirectory().get Path() , "/quransund/gamedi/");
if (externalFile.exists()) {

}else {
externalFile.mkdirs();
}









/*
sdCard =( Environment.getExternalStorageDirectory());
// Check if the Music file already exists
if (file.exists()) {
Toast.makeText(getApplicationContext(), "File already exist under SD card, playing Music", Toast.LENGTH_LONG).show();

// If the Music File doesn't exist in SD card (Not yet downloaded)
} else {
Toast.makeText(getApplicationContext(), "File doesn't exist under SD Card, downloading Mp3 from Internet", Toast.LENGTH_LONG).show();
// Trigger Async Task (onPreExecute method)

file.mkdirs();
}

*/

progressBar1=(ProgressBar) findViewById(R.id.progressBar1);

btnPlayMusic = (Button) findViewById(R.id.btnProgressBar);

btnPlayMusic.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

btnPlayMusic.setEnabled(false);


for(String k: myArray_2){

fdown = new String[]{"http://www.everyayah.com/data/Ghamadi_40kbps/"+k+".mp3"};


fpath= new String[]{ ""+k+".mp3"};


System.out.println(k);

fdownString = Arrays.toString(fdown);
System.out.println(fdownString);


fpathString = Arrays.toString(fpath);
System.out.println(fpathString);

// Downloaded Music File path in SD Card

// file = new File (sdCard.getAbsolutePath() + fpath);

new DownloadMusicfromInternet().execute(fdown);

}




}

});

}
// Show Dialog Box with Progress bar
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type:
prgDialog = new ProgressDialog (this);
prgDialog.setMessage("Downloading Mp3 file. Please wait...");
prgDialog.setIndeterminate(false);
prgDialog.setMax(100);


prgDialog.show();
return prgDialog;
default:
return null;
}
}

// Async Task Class
class DownloadMusicfromInternet extends AsyncTask<String, String, String> {




// Show Progress bar before downloading Music
@Override
protected void onPreExecute() {
super.onPreExecute();
// Shows Progress Bar Dialog and then call doInBackground method
showDialog(progress_bar_type);
}

// Download Music File from Internet
@Override
protected String doInBackground(String... f_url) {



for(int x=0; x< myArray_2.length ; x++){




int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// Get Music file length

int lenghtOfFile = conection.getContentLength();

// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(),10*1024);


// Output stream to write file in SD card
File file = new File(externalFile , fpathString);
OutputStream output = new FileOutputStream(file );




byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// Publish the progress which triggers onProgressUpdate method
publishProgress("" + (int) ((total * 100) / lenghtOfFile));

// Write data to file
output.write(data, 0, count);
}
// Flush output
output.flush();
// Close streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
}
return null;
}


// While Downloading Music File
protected void onProgressUpdate(String... progress) {
// Set progress percentage
prgDialog.setProgress(Integer.parseInt(progress[0]));
}

// Once Music File is downloaded
@Override
protected void onPostExecute(String file_url) {
// Dismiss the dialog after the Music file was downloaded
dismissDialog(progress_bar_type);
Toast.makeText(getApplicationContext(), "Download complete, playing Music", Toast.LENGTH_LONG).show();

}
}


}