显示Web图片和SD卡图片
下面是res/layout/show_image.xml
?
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/show_image_button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Show Image" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="下列图片来自互联网:" /> <ImageView android:id="@+id/web_image" android:layout_width="fill_parent" android:layout_height="100dp" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="下列图片来自SD卡:" /> <ImageView android:id="@+id/sd_image" android:layout_width="fill_parent" android:layout_height="100dp" /></LinearLayout>
?
下面是ShowImageActivity.java代码
?
import java.io.IOException;import java.io.InputStream;import org.apache.http.HttpEntity;import org.apache.http.HttpHost;import org.apache.http.HttpResponse;import org.apache.http.HttpStatus;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.conn.params.ConnRoutePNames;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.params.BasicHttpParams;import org.apache.http.params.HttpConnectionParams;import org.apache.http.params.HttpParams;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;public class ShowImageActivity extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.show_image); Button showButton = (Button)findViewById(R.id.show_image_button); showButton.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View view) {getWebPicture();getSDCardPicture();}});}private void getWebPicture() {HttpGet httpGet = new HttpGet("http://www.2014412.com/wp-content/uploads/2011/04/17-150x150.jpg");HttpClient client = getHttpClient();Bitmap bitmap = null;try {HttpResponse res = client.execute(httpGet);if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {HttpEntity entity = res.getEntity();InputStream is = entity.getContent();bitmap = BitmapFactory.decodeStream(is);} else {Toast.makeText(this, "网络图片没有取到", Toast.LENGTH_LONG).show();return;}} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}ImageView iv = (ImageView)findViewById(R.id.web_image);iv.setImageBitmap(bitmap);}private void getSDCardPicture() {String picPath = "/sdcard/pictures/sd_image.jpg";Bitmap bitmap = BitmapFactory.decodeFile(picPath);ImageView iv = (ImageView)findViewById(R.id.sd_image);iv.setImageBitmap(bitmap);}private HttpClient getHttpClient() {//设置HTTP参数(httpParams参数不是必需)HttpParams httpParams = new BasicHttpParams();HttpConnectionParams.setConnectionTimeout(httpParams, 5000);HttpConnectionParams.setSoTimeout(httpParams, 5000);HttpConnectionParams.setSocketBufferSize(httpParams, 10240);//创建HttpClientHttpClient client = new DefaultHttpClient(httpParams);//设置代理(如果不需要代理可以不用设置)HttpHost proxy = new HttpHost("192.168.190.251",3128);client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);return client;}}?
上面代码有个从SD卡取图片的功能。因此,需要放一张图片到模拟器的SD卡中。操作如果
?
#在SD卡中创建目录(如果放在sdcard目录下就不需要创建目录)
打开cmd,依次输入:
adb shell
adb sdcard
mkdir pictures
exit
?
#把文件放入SD卡中
adb push H:\sd_image.jpg /sdcard/pictures
?
注意:不要用下面格式
adb push H:\sd_image.jpg \sdcard\pictures
?
出现以下异常
failed to copy 'H:\sd_image.jpg' to '\sdcard\pictures': Read-only file system
?
原因是第二个参数是Linux目录,应该使用“/”而不是“\”来表示路径
?
?
?
运行结果
当点击Show Image按钮后,显示如下面
?
?