显示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 {
	@Override
	public 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(){
			@Override
			public 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);
		//创建HttpClient
		HttpClient 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按钮后,显示如下面


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值