我正在測試一個應用程序下載按鈕單擊文件。但是,當按鈕被點擊時,沒有任何反應。對話框出現並在幾分之一秒內消失。不知道什麼是錯 這裏是我的代碼下載沒有得到執行後按下按鈕
mainactivity
public class MainActivity extends Activity
{
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private Button startBtn;
private ProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startBtn = (Button) findViewById(R.id.mainButton);
startBtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View p1)
{
// TODO: Implement this method
startDownload();
}
});
}
private void startDownload()
{
String url = "http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg";
new DownloadFileAsync().execute(url);
}
@Override protected Dialog onCreateDialog(int id)
{
switch (id)
{
case DIALOG_DOWNLOAD_PROGRESS: mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Downloading file..");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
return mProgressDialog;
default: return null;
}
}
class DownloadFileAsync extends AsyncTask<String, String, String>
{
@Override protected void onPreExecute()
{
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override protected String doInBackground(String... aurl)
{
int count;
try
{
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection(); conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/some_photo_from_gdansk_poland.jpg");
byte data[] = new byte[1024];
long total = 0; while ((count = input.read(data)) != -1)
{
total += count;
publishProgress("" + (int)((total * 100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
}
catch (Exception e)
{
}
return null;
} protected void onProgressUpdate(String... progress)
{
Log.d("ANDRO_ASYNC", progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
} @Override protected void onPostExecute(String unused)
{
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
}
}
Android清單
<?xml version="1.0" encoding="utf-8"?>
uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
添加以下權限它顯示有些東西..'Log.d(「ANDRO_ASYNC」,「文件的長度:」+ lenghtOfFile);' –
嘗試使用'InputStream input = conexion.getInputStream()'而不是'InputStream input =新的BufferedInputStream(url.openStream());'。 –
@sourabhbans嘗試過,但同樣的問題,感謝您抽出寶貴的興趣 – user5524159