۱۳۹۳/۱۱/۲۳, ۱۲:۰۰ ب.ظ
سلام.خسته نباشید
من یک دانلود منیجر ساده ساختم که آدرس فایل رو میگیره و بعد دانلود می کنه.ولی من تو این برنامم دو تا مشکل بزرگ دارم.
مشکل اول اینه که نمیتونم یک فولدر جدید تو sdcard بسازم و فایل هایی که دانلود میشن رو توش ذخیره کنم(البته کد ایجاد پوشه ی جدید و ذخیره ی فایل ها درون اون رو نوشتم ولی کار نمی کنه).
-------------------
مشکل دوم اینه که من نمیتونم نام اصلی اون فایلی که داره دانلود میشه رو بدست بیارم و فایل با اون اسم(اسمی که توی هاست داره) ذخیره بشه.
این هم کد دانلود منیجر بنده.
لطفا راهنمایی کنید.
من یک دانلود منیجر ساده ساختم که آدرس فایل رو میگیره و بعد دانلود می کنه.ولی من تو این برنامم دو تا مشکل بزرگ دارم.
مشکل اول اینه که نمیتونم یک فولدر جدید تو sdcard بسازم و فایل هایی که دانلود میشن رو توش ذخیره کنم(البته کد ایجاد پوشه ی جدید و ذخیره ی فایل ها درون اون رو نوشتم ولی کار نمی کنه).
-------------------
مشکل دوم اینه که من نمیتونم نام اصلی اون فایلی که داره دانلود میشه رو بدست بیارم و فایل با اون اسم(اسمی که توی هاست داره) ذخیره بشه.
این هم کد دانلود منیجر بنده.
لطفا راهنمایی کنید.
کد پیاچپی:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
public ProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/Arazapp");
dir.mkdirs(); // build directory
final EditText et = (EditText)findViewById(R.id.editText1);
Button btn=(Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String editText_input = et.getText().toString();
// instantiate it within the onCreate method
mProgressDialog = new ProgressDialog(MainActivity.this); // MainActivity = activity name
mProgressDialog.setMessage("در حال دانلود فایل ...");
mProgressDialog.setIndeterminate(true);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(true);
// execute this when the downloader must be fired
final DownloadTask downloadTask = new DownloadTask(MainActivity.this); // MainActivity = activity name
downloadTask.execute(editText_input); // the url to the file you want to download
mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
downloadTask.cancel(true);
}
});
}
// usually, subclasses of AsyncTask are declared inside the activity class.
// that way, you can easily modify the UI thread from here
class DownloadTask extends AsyncTask<String, Integer, String> {
private Context context;
public DownloadTask(Context context) {
this.context = context;
}
@Override
protected String doInBackground(String... sUrl) {
// take CPU lock to prevent CPU from going off if the user
// presses the power button during download
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
getClass().getName());
wl.acquire();
try {
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
try {
URL url = new URL(sUrl[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
// expect HTTP 200 OK, so we don't mistakenly save error report
// instead of the file
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
return "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage();
// this will be useful to display download percentage
// might be -1: server did not report the length
int fileLength = connection.getContentLength();
// download the file
input = connection.getInputStream();
output = new FileOutputStream("/sdcard/(filename)"); // /sdcard/file_name.extension
byte data[] = new byte[4096];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
// allow canceling with back button
if (isCancelled())
return null;
total += count;
// publishing the progress....
if (fileLength > 0) // only if total length is known
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
} catch (Exception e) {
return e.toString();
} finally {
try {
if (output != null)
output.close();
if (input != null)
input.close();
}
catch (IOException ignored) { }
if (connection != null)
connection.disconnect();
}
} finally {
wl.release();
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.show();
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
// if we get here, length is known, now set indeterminate to false
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgress(progress[0]);
}
@Override
protected void onPostExecute(String result) {
mProgressDialog.dismiss();
if (result != null)
Toast.makeText(context,"دانلود ناموفق", Toast.LENGTH_LONG).show();
else
Toast.makeText(context,"دانلود شد", Toast.LENGTH_SHORT).show();
} }
});
}
}
گفتند که تک سوارمان در راه است
از اول صبح چشممان بر راه است
از یازدهم دوازده قرن گذشت
تا ساعت تو چقدر دیگر راه است؟
اللهم عجل لولیک الفرج ...