PDA

View Full Version : مشکل در اجرای این برنامه در اندروید 6



davod56
جمعه 07 آبان 1395, 09:47 صبح
سلام
لطفا اساتید راهنمایی کنند...
من از کد زیر برای دانلود فایل mp3 استفاده می کنم و مشکلی که دارم این هست که برنامه در شبیه ساز با نسخه ی 4 درست کار می کنه ولی در گوشی (اندروید 6) درست کار نمی کنه!!! یعنی نه پوشه ای می سازه و نه فایل رو دانلود می کنه...

اینم کدی که استفاده می کنم....



import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.TextView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class test extends Activity {

public static final String LOG_TAG = "Android Downloader by The Code Of A Ninja";

//initialize our progress dialog/bar
private ProgressDialog mProgressDialog;
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;

//initialize root directory
File rootDir = Environment.getExternalStorageDirectory();

//defining file name and url
public String fileName = "097_ghadr.mp3";
public String fileURL = "http://gmi.zgig.in/download/097_ghadr.mp3";

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setting some display
setContentView(R.layout.test);
TextView tv = new TextView(this);
tv.setText("Android Download File With Progress Bar");

//making sure the download directory exists
checkAndCreateDirectory("/my_downloads");

//executing the asynctask
new DownloadFileAsync().execute(fileURL);
}

//this is our download file asynctask
class DownloadFileAsync extends AsyncTask<String, String, String> {

@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}


@Override
protected String doInBackground(String... aurl) {

try {
//connecting to url
URL u = new URL(fileURL);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();

//lenghtOfFile is used for calculating download progress
int lenghtOfFile = c.getContentLength();

//this is where the file will be seen after the download
FileOutputStream f = new FileOutputStream(new File(rootDir + "/my_downloads/", fileName));
//file input is from the url
InputStream in = c.getInputStream();

//here’s the download code
byte[] buffer = new byte[1024];
int len1 = 0;
long total = 0;

while ((len1 = in.read(buffer)) > 0) {
total += len1; //total = total + len1
publishProgress(""+ (int)((total*100)/lenghtOfFile));
f.write(buffer, 0, len1);
}
f.close();

} catch (Exception e) {
Log.d(LOG_TAG, e.getMessage());
}

return null;
}

protected void onProgressUpdate(String... progress) {
Log.d(LOG_TAG,progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progr ess[0]));
}

@Override
protected void onPostExecute(String unused) {
//dismiss the dialog after the file was downloaded
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
}

//function to verify if directory exists
public void checkAndCreateDirectory(String dirName){
File new_dir = new File( rootDir + dirName );
if( !new_dir.exists() ){
new_dir.mkdirs();
}
}

//our progress bar settings
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS: //we set this to 0
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Downloading file...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.ST YLE_HORIZONTAL);
mProgressDialog.setCancelable(true);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}
}

ممنون می شم دوستان راه حلی ارائه کنند...

rezaricky
جمعه 07 آبان 1395, 11:48 صبح
سلام . کدت رو که درست نمایش نمیده اما از اونجایی که میگی مشکل در اندروید 6 هست احتمال زیاد مشکل در بخش permission هست .
http://www.truiton.com/2016/04/obtaining-runtime-permissions-android-marshmallow-6-0/

msroid
جمعه 07 آبان 1395, 15:15 عصر
سلام

چون داره فایل رو روی حافظه دستگاه کاربر مینویسه نیاز به مجوز نوشتن هست. در اندروید 6 علاوه بر اینکه مجوزها باید در manifest قرار بگیرند باید با کدنویسی هنگام نیاز به اونها از کاربر درخواست بشن.

davod56
جمعه 07 آبان 1395, 15:39 عصر
ممنون
الان من چطور باید این درخواست رو از کار بر بگیرم...
ممنون می شم یک راهنمایی کوچیک ( کامل باشه بهتره :لبخند: ) بکنید...
اگرم سایتی یا آموزشی دراین رابطه سراغ دارید ممنون میشم معرفی کنید

msroid
جمعه 07 آبان 1395, 16:46 عصر
لینک زیر اولین پاسخ به سوال جواب رو درست داده:

http://stackoverflow.com/questions/33666071/android-marshmallow-request-permission

davod56
شنبه 08 آبان 1395, 06:27 صبح
ممنون دوست عزیز این مشکلم حل شد...
مشکل دیگه ای که دارم اینکه میخوام همین فایل صوتی که دانلود شده رو توی برنامه پخش کنم...
توی نت هم هر کدی بود امتحان کردم ولی جواب نداد...
برای حل این مشکل هم ممنون می شم اگر کمکم کنید....