资源中获取图片:可以从工程assets文件夹、res/drawble文件夹、sd卡、服务端下载图片。

页面:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/btnDecodeFile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="从文件解析图片" /> <Button
android:id="@+id/btnDecodeStream"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="丛输入流解析" /> <Button
android:id="@+id/btnDecodeResource"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="从项目res/drawble中解析" /> <Button
android:id="@+id/btnDecodeByteArray"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="从字节数组解析" />
<ImageView 显示解析的图片
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
</LinearLayout>

java

package com.sxt.day06_08;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils; import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView; public class MainActivity extends Activity {
ImageView mImageView;
static final String FILE_NAME="sxt_logo.png";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setListener();
} private void setListener() {
setResetClickListener();
setDecodeFileClickListener();
setDecodeResourceClickListener();
setDecodeStreamClickListener();
setDecodeByteArrayClickListener();
} private void setDecodeByteArrayClickListener() {
findViewById(R.id.btnDecodeByteArray).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Thread(){
public void run() {
HttpGet get=new HttpGet("http://10.0.2.2/"+FILE_NAME);//服务端资源文件路径
HttpClient client=new DefaultHttpClient();
try {
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();//
byte[] data = EntityUtils.toByteArray(entity);
final Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
//工作线程不能修改UI,可以用Handler做,runOnUiThread方法会把里面的代码发送给主线程,修改UI。
//Runnable可以被多个线程共享,工作线程可以把该Runnable对象交给主线程由主线程执行。
runOnUiThread(new Runnable() {
@Override
public void run() {
mImageView.setImageBitmap(bitmap);
}
});
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(client!=null){
client.getConnectionManager().shutdown();//关闭
}
}
};
}.start();
}
});
} private void setDecodeStreamClickListener() {
findViewById(R.id.btnDecodeStream).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File file=new File(dir, FILE_NAME);//sd卡图片路径
FileInputStream in=null;
try {
in=new FileInputStream(file);
Bitmap bitmap = BitmapFactory.decodeStream(in);
mImageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
if(in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
} private void setDecodeResourceClickListener() {
findViewById(R.id.btnDecodeResource).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mImageView.setImageResource(R.drawable.sxt_logo);
}
});
} private void setResetClickListener() {//点击图片还原
findViewById(R.id.iv).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mImageView.setImageResource(R.drawable.ic_launcher);
}
});
} //获取sd卡的图片文件
private void setDecodeFileClickListener() {
findViewById(R.id.btnDecodeFile).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);//sd卡的图片的路径
File file=new File(dir, FILE_NAME);//获取sd卡的图片文件
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
mImageView.setImageBitmap(bitmap);
}
});
} private void initView() {
mImageView=(ImageView) findViewById(R.id.iv);
}
}

工程描述文件添加:

    <uses-permission android:name="android.permission.INTERNET"/>     申请网络权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 申请读sd卡权限

android 42 获取图片的更多相关文章

  1. Android中获取图片的宽和高

    在Android中,我们想获取图片的宽和高应该怎么办?一.正常加载图片的方法下获取宽和高 举一个简单的例子:创建一个图片的副本 //加载原图 Bitmap bmSrc = BitmapFactory. ...

  2. Java乔晓松-android中获取图片的缩略图(解决OutOfMemoryError)内存溢出的Bug

    由于android获取图片过大是会出现内存溢出的Bug 07-02 05:10:13.792: E/AndroidRuntime(6016): java.lang.OutOfMemoryError 解 ...

  3. Android笔记-获取图片

     1. 图片放在sdcard中,根据路径获得: Bitmap imageBitmap = BitmapFactory.decodeFile(path) (path 是图片的路径,跟目录是/sdcard ...

  4. android调用系统相机并获取图片

    如果不是特别的要求,通过拍照的方式取得图片的话,我们一般调用系统的拍照来完成这项工作,而没必要再自己去实现一个拍照功能.调用系统相机很简单,只需要一个intent就可以跳转到相几界面,然后再通过onA ...

  5. Android 从Gallery获取图片

    本文主要介绍Android中从Gallery获取图片 设计项目布局 <LinearLayout xmlns:android="http://schemas.android.com/ap ...

  6. Android 拍照或者从相册获取图片的实现

    我们常常会用到上传头像,或者发帖子的时候选择本地图片上传的功能.这个很常见 今天因为app的需求我研究了下.现在分享下. 其实不论是通过拍照还是从相册选取都会用到Intent 这是系统提供给我们用来调 ...

  7. android通过BitmapFactory.decodeFile获取图片bitmap报内存溢出的解决办法

    android通过BitmapFactory.decodeFile获取图片bitmap报内存溢出的解决办法 原方法: public static Bitmap getSmallBitmap(Strin ...

  8. xamarin.android之 Android 4.4+ 获取图片真实路径

    Android 4.4以下 选择图片是可以获取到图片路径的.高于Android 4.4获取图片路径只是获取到一个图片编号. 所以需要针对Android版本进行路径解析: #region 高于 v4.4 ...

  9. Android相机、相册获取图片显示并保存到SD卡

    Android相机.相册获取图片显示并保存到SD卡 [复制链接]   电梯直达 楼主    发表于 2013-3-13 19:51:43 | 只看该作者 |只看大图  本帖最后由 happy小妖同学 ...

随机推荐

  1. ZendFramework使用中常见问题

    MVC 代码书写:控制器代码书写:<?phpclass IndexController extends Zend_Controller_Action{ function init() { $th ...

  2. mysql备份,还原命令

    mysql导出数据1.导出整个数据库mysqldump -u用户名 -p 数据库名 >备份文件2.导出一个表mysqldump -u用户名 -p databaseName tablename & ...

  3. python读取excel文件

    一.xlrd的说明 xlrd是专门用来在python中读取excel文档的模块,使用前需要安装. 可以到这https://pypi.python.org/pypi/xlrd进行下载tar.gz文件,然 ...

  4. MAC Python环境配置以及安装Pycharm 5.4.0

    安装XCODE 去APP STORE下载,然后安装.免费 终端执行 xcode-select --install 安装或更新命令行开发工具 安装Pycharm 下载软件 官网:https://www. ...

  5. BootStrap Progressbar 实现大文件上传的进度条

    1.首先实现大文件上传,如果是几兆或者几十兆的文件就用基本的上传方式就可以了,但是如果是大文件上传的话最好是用分片上传的方式.我这里主要是使用在客户端进行分片读取到服务器段,然后保存,到了服务器段读取 ...

  6. apache pdfbox

    转 http://www.blogjava.net/sxyx2008/archive/2010/07/23/326890.html 轻松使用apache pdfbox将pdf文件生成图 近期在项目中使 ...

  7. js代码判断浏览器种类IE、FF、Opera、Safari、chrome及版本

    这篇文章主要分享了判断IE.FF.Opera.Safari.Chrome等浏览器和版本的两种方法,需要的朋友可以参考下 因为ie10-ie11的版本问题,不再支持document.all判断,所以ie ...

  8. clang: error: invalid deployment target for -stdlib=libc++ (requires iOS 5.0 or later)

    低于5.0版本的不支持设置成 或 在Xcode中做如下配置 静态库工程也要做同样设置

  9. Android假退出不是流氓行为

    转自Android假退出不是流氓行为 关于Android程序的退出,目前我们没有再用System.exit(0)或killProcess的机制而是直接用Activity.finish假退出了.因此在内 ...

  10. 如何监控 Nginx?

    什么是 Nginx? Nginx("engine-x")是一个 HTTP 和反向代理服务器,同时也是一个邮件代理服务器和通用的 TCP 代理服务器.作为一个免费开源的服务器,Ngi ...