android 42 获取图片
资源中获取图片:可以从工程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 获取图片的更多相关文章
- Android中获取图片的宽和高
在Android中,我们想获取图片的宽和高应该怎么办?一.正常加载图片的方法下获取宽和高 举一个简单的例子:创建一个图片的副本 //加载原图 Bitmap bmSrc = BitmapFactory. ...
- Java乔晓松-android中获取图片的缩略图(解决OutOfMemoryError)内存溢出的Bug
由于android获取图片过大是会出现内存溢出的Bug 07-02 05:10:13.792: E/AndroidRuntime(6016): java.lang.OutOfMemoryError 解 ...
- Android笔记-获取图片
1. 图片放在sdcard中,根据路径获得: Bitmap imageBitmap = BitmapFactory.decodeFile(path) (path 是图片的路径,跟目录是/sdcard ...
- android调用系统相机并获取图片
如果不是特别的要求,通过拍照的方式取得图片的话,我们一般调用系统的拍照来完成这项工作,而没必要再自己去实现一个拍照功能.调用系统相机很简单,只需要一个intent就可以跳转到相几界面,然后再通过onA ...
- Android 从Gallery获取图片
本文主要介绍Android中从Gallery获取图片 设计项目布局 <LinearLayout xmlns:android="http://schemas.android.com/ap ...
- Android 拍照或者从相册获取图片的实现
我们常常会用到上传头像,或者发帖子的时候选择本地图片上传的功能.这个很常见 今天因为app的需求我研究了下.现在分享下. 其实不论是通过拍照还是从相册选取都会用到Intent 这是系统提供给我们用来调 ...
- android通过BitmapFactory.decodeFile获取图片bitmap报内存溢出的解决办法
android通过BitmapFactory.decodeFile获取图片bitmap报内存溢出的解决办法 原方法: public static Bitmap getSmallBitmap(Strin ...
- xamarin.android之 Android 4.4+ 获取图片真实路径
Android 4.4以下 选择图片是可以获取到图片路径的.高于Android 4.4获取图片路径只是获取到一个图片编号. 所以需要针对Android版本进行路径解析: #region 高于 v4.4 ...
- Android相机、相册获取图片显示并保存到SD卡
Android相机.相册获取图片显示并保存到SD卡 [复制链接] 电梯直达 楼主 发表于 2013-3-13 19:51:43 | 只看该作者 |只看大图 本帖最后由 happy小妖同学 ...
随机推荐
- quick-x 计时器的写法
local scheduler = require("framework.scheduler") --计时器 function MainScene:recoderTime() pr ...
- PHP创建桌面快捷方式实例
要利用php创建桌面快捷方式我们需要借助于header,InternetShortcut及一些我看不懂的代码. 方法:新建一个php文件,然后把下面的代码扔进去,保存为比如shortcut.php,放 ...
- CANoe 入门 Step by step系列(三)简单例子的剖析【转】
最好的学习方式是什么?模仿.有人会问,那不是山寨么?但是我认为,那是模仿的初级阶段,当把别人最好的设计已经融化到自己的血液里,变成自己的东西,而灵活运用的时候,才是真正高级阶段.正所谓画虎画皮难画骨. ...
- 练习2 F题 - 平方和与立方和
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 给定一 ...
- WPF DataGrid
前台代码 <DataGrid Name="gv_GetWork" AutoGenerateColumns="False" CanUserSortColum ...
- Runtime-b
感谢大神分享 依旧是网上很多runtime的资料,依旧是看不懂,,,这里给大家转化一下runtime,使它由隐晦难懂变得通俗易懂. (虽然截图和语言组织的有些凌乱,但是大家还是一点一点的阅读下去吧,可 ...
- iOS 8创建交互式通知-备
iOS 8提供了一个令人兴奋的新API来创建交互式通知(interactive notifications),它能让你在你的应用之外为用户提供额外的功能.我发现网上还没有关于如何实现它的比较好的示例教 ...
- Struts2技术内幕----深入解析Struts2架构与设计(一)
Struts2的核心入口程序,从功能上来说必须能够处理Http请求,这是表示层框架的基本要求.为了达到这一目的,Struts2毫无例外地遵循了Servlet标准,通过实现标准的Filter接口来进行H ...
- jq实现瀑布流效果
<!doctype html><html><head><meta http-equiv="Content-Type" content=&q ...
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...