我正在尝试构建一个应用程序,该应用程序获取图像链接并从Web下载。无论什么时候我的应用程序,它给我一个MalformedUrlException:协议没有形成:url ICan有人告诉我我做错了什么。我是android新手,所以我无法弄清楚我做错了什么。任何帮助是极大的赞赏。提前谢谢你:)
package com.img.imagedwnloader;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
static Button dwnload;
static EditText link;
static TextView textv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dwnload = (Button) findViewById (R.id.bstrtD);
link = (EditText) findViewById (R.id.etlink);
textv = (TextView) findViewById (R.id.tvUpdate);
dwnload.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textv.setText("click");
// TODO Auto-generated method stub
textv.setText("thread");
File storedwn = Environment.getExternalStorageDirectory();
File file = new File (storedwn, "downloadFile.jpg");
long startTime = System.currentTimeMillis();
try{
textv.setText("try");
String linkt = link.getText().toString();
URL url = new URL (linkt);
textv.setText(link.getText().toString());
URLConnection urlconnect = url.openConnection();
InputStream is = urlconnect.getInputStream();
BufferedInputStream bis = new BufferedInputStream (is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
textv.setText("Download Completed in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");
} catch (IOException e) {
textv.setText("Error: " + e);
}
}
/* private void downloadVid() {
// TODO Auto-generated method stub
try{
URL url = new URL (link.getText().toString());
textv.setText(link.getText());
HttpURLConnection urlconnect = (HttpURLConnection) url.openConnection();
urlconnect.setRequestMethod("GET");
urlconnect.setDoOutput(true);
urlconnect.connect();
File storedwn = Environment.getExternalStorageDirectory();
File file = new File (storedwn, "downloadFile.avi");
} catch (Exception e){
textv.setText("Failed!");
}
}
}).start();;
} */
});
}
}