import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) findViewById(R.id.editText1);
Button huoqu = (Button) findViewById(R.id.button1);
Button clear = (Button) findViewById(R.id.button2);
huoqu.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String path = edit.getText().toString();
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setConnectTimeout(5 * 1000);
connection.setRequestMethod("GET");
if (connection.getResponseCode() != 200) {
throw new RuntimeException("请求url失败");
}
InputStream input = connection.getInputStream();
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] bt = new byte[1024];
int length = -1;
while ((length = input.read(bt)) != -1) {
output.write(bt, 0, length);
}
byte data[] = output.toByteArray();
if (path.endsWith(".jpg")||path.endsWith(".JPG")) {
ImageView iv = (ImageView) findViewById(R.id.imageView1);
Bitmap bit = BitmapFactory.decodeByteArray(data, 0,
data.length);
iv.setImageBitmap(bit);
} else if (path.endsWith(".com")||path.endsWith(".COM")) {
TextView tv = (TextView) findViewById(R.id.textView2);
tv.setText(new String(data, "UTF-8"));
} else {
Toast.makeText(getBaseContext(), "只能获取图片或图片!",
Toast.LENGTH_LONG).show();
}
;
} catch (Exception e) {
e.printStackTrace();
Log.i("TAG", e.toString());
Toast.makeText(getBaseContext(), "出错了,找不到数据", 1).show();
}
}
});
clear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
edit.setText("");
}
});
}
}
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) findViewById(R.id.editText1);
Button huoqu = (Button) findViewById(R.id.button1);
Button clear = (Button) findViewById(R.id.button2);
huoqu.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String path = edit.getText().toString();
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setConnectTimeout(5 * 1000);
connection.setRequestMethod("GET");
if (connection.getResponseCode() != 200) {
throw new RuntimeException("请求url失败");
}
InputStream input = connection.getInputStream();
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] bt = new byte[1024];
int length = -1;
while ((length = input.read(bt)) != -1) {
output.write(bt, 0, length);
}
byte data[] = output.toByteArray();
if (path.endsWith(".jpg")||path.endsWith(".JPG")) {
ImageView iv = (ImageView) findViewById(R.id.imageView1);
Bitmap bit = BitmapFactory.decodeByteArray(data, 0,
data.length);
iv.setImageBitmap(bit);
} else if (path.endsWith(".com")||path.endsWith(".COM")) {
TextView tv = (TextView) findViewById(R.id.textView2);
tv.setText(new String(data, "UTF-8"));
} else {
Toast.makeText(getBaseContext(), "只能获取图片或图片!",
Toast.LENGTH_LONG).show();
}
;
} catch (Exception e) {
e.printStackTrace();
Log.i("TAG", e.toString());
Toast.makeText(getBaseContext(), "出错了,找不到数据", 1).show();
}
}
});
clear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
edit.setText("");
}
});
}
}
Android应用中加载网络图片
本文介绍了一个简单的Android应用程序,该程序能够从网络上获取图片并显示在ImageView中。此外,还介绍了如何处理不同类型的文件(如图片和文本),以及错误处理机制。
2301

被折叠的 条评论
为什么被折叠?



