我制作了一个可以从网上下载图像的android应用程序,但是当我运行它时它会停止。这里没有错误是我的主类 MainActivity.class `package com.downloadfiles;
public class MainActivity extends Activity {
Button btnShowProgress;
private ProgressDialog pDialog;
ImageView my_image;
EditText edt = (EditText) findViewById(R.id.editText1);
public static final int progress_bar_type = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String i = edt.getText().toString();
final String file_url = i ;
btnShowProgress = (Button) findViewById(R.id.btnProgressBar);
my_image = (ImageView) findViewById(R.id.my_image);
btnShowProgress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DownloadFileFromURL().execute(file_url);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type:
pDialog = new ProgressDialog(this);
pDialog.setMessage("Downloading file Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
return pDialog;
default:
return null;
}
}
class DownloadFileFromURL extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
int lenghtOfFile = conection.getContentLength();
String mak;
InputStream input = new BufferedInputStream(url.openStream(), 8192);
String foe;
EditText form = (EditText) findViewById(R.id.formet);
mak = form.getText().toString();
foe = "Download file.";
OutputStream output = new FileOutputStream("/sdcard/download/" +foe +mak);
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) {
Log.e("Error: ", e.getMessage());
}
return null;
}
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String file_url) {
dismissDialog(progress_bar_type);
String mak;
EditText form = (EditText) findViewById(R.id.formet);
mak = form.getText().toString();
if (mak =="jpg")
{
String imagePath = Environment.getExternalStorageDirectory().toString() + "/download/Download file." +"jpg";
my_image.setImageDrawable(Drawable.createFromPath(imagePath));
}
else if (mak == "png")
{
String imagePath = Environment.getExternalStorageDirectory().toString() + "/download/Download file." +"png";
my_image.setImageDrawable(Drawable.createFromPath(imagePath));
}
else if (mak == "gif"){
String imagePath = Environment.getExternalStorageDirectory().toString() + "/download/Download file."+"gif";
my_image.setImageDrawable(Drawable.createFromPath(imagePath));
}
else
{Intent toNextPage = new Intent(MainActivity.this,
backgo.class);
startActivity(toNextPage);
}
}}}`
这是我的另一堂课 backgo.class
`public class backgo extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sorryerror);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent toNextPage = new Intent(backgo.this,
MainActivity.class);
startActivity(toNextPage);
}
});}}`
这是我的布局 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13dp"
android:text="Enter the formet in which you want to download the file i.e mp3,jpg or mp4:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/formet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPostalAddress" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter the url from where you want to download file:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text = "http://images6.fanpop.com/image/photos/33400000/Naruto-evoliution-naruto-33413374-1600-1152.png"
android:inputType="textPostalAddress" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnProgressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Download File " />
<ImageView android:id="@+id/my_image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
这是我的另一个布局sorryerror.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sorry the formet of your image you requested is not supported by the application we are very sorry..."
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30dp"
android:textStyle="bold"
android:typeface="serif" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="233dp"
android:layout_weight="0.37"
android:src="@drawable/crying" />
<Button
android:id="@+id/button1"
android:layout_width="166dp"
android:layout_height="wrap_content"
android:layout_weight="0.01"
android:text="Click here to go back" />
</LinearLayout>
请帮助提前谢谢。