۱۳۹۴/۰۸/۰۴, ۰۱:۲۳ ق.ظ
۱۳۹۴/۰۸/۰۴, ۰۱:۴۹ ق.ظ
سلام دوست عزیز
اگر منظور شما این که پی دی اف که آپلود کردی در برنامه قرار بدی و کاربر با کلیک کردن به دکمه بتونه اون فایل پی دی اف رو دانلود کنه از این کد استفاده کن
به جای این لینک
لینکی که خودت آپلود کردی رو بزار
و به جای
باید مسیر پوشه خودت رو بسازی چون فایل شما پی دی اف هستش از این کد استفاده کن
/storage/sdcard0/NAME FOLDER/NAME FILE PDF.pdf
اگر منظور شما این که پی دی اف که آپلود کردی در برنامه قرار بدی و کاربر با کلیک کردن به دکمه بتونه اون فایل پی دی اف رو دانلود کنه از این کد استفاده کن
کد پیاچپی:
final Button btnd = (Button)findViewById(R.id.button1);
btnd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mProgressDialog = new ProgressDialog(Mabhas1.this);
mProgressDialog.setMessage("ÏÑ ÍÇá ÏÇäáæÏ...");
mProgressDialog.setIndeterminate(true);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(true);
final DownloadTask downloadTask = new DownloadTask(Mabhas1.this);
downloadTask.execute("http://bayanbox.ir/id/6955356415739458249?download");
mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
downloadTask.cancel(true);
}
});
}
class DownloadTask extends AsyncTask<String, Integer, String> {
private Context context;
public DownloadTask(Context context) {
this.context = context;
}
@Override
protected String doInBackground(String... sUrl) {
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();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
return "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage();
int fileLength = connection.getContentLength();
input = connection.getInputStream();
output = new FileOutputStream("/storage/sdcard0/TanhaMasir/tanha1.mp3"); // /sdcard/file_name.extension
byte data[] = new byte[4096];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
if (isCancelled())
return null;
total += count;
if (fileLength > 0)
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);
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgress(progress[0]);
}
@Override
protected void onPostExecute(String result) {
mProgressDialog.dismiss();
if (result != null)
Toast.makeText(context,"ÎØÇ ÏÑ ÏÇäáæÏ: "+result, Toast.LENGTH_LONG).show();
else
Toast.makeText(context,"ÏÇäáæÏ ÔÏ", Toast.LENGTH_SHORT).show();
}
}
});
به جای این لینک
کد پیاچپی:
http://bayanbox.ir/id/6955356415739458249?download
لینکی که خودت آپلود کردی رو بزار
و به جای
کد پیاچپی:
/storage/sdcard0/TanhaMasir/tanha1.mp3
باید مسیر پوشه خودت رو بسازی چون فایل شما پی دی اف هستش از این کد استفاده کن
/storage/sdcard0/NAME FOLDER/NAME FILE PDF.pdf