从SD卡读图并显示(一)
来源:互联网 发布:淘宝html5 编辑:程序博客网 时间:2024/05/28 18:45
本节,我们做一个很简单的Demo,实现从SD卡读取一张图片,并把它显示在APP中。
分三步:
1.设置权限。
在Manifest文件中添加三行权限信息。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
2.配置MXL视图。
可以参考:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:id="@+id/myView" android:layout_width="fill_parent" android:layout_height="fill_parent" /></LinearLayout>
3.Java代码编写部分。
大体上就两个步骤:获取图片文件,显示图片
(详细过程见代码,注释)
package com.example.showimage;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.os.Environment;import android.widget.ImageView;public class MainActivity extends Activity { private ImageView myView = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myView = (ImageView)findViewById(R.id.myView); String path = Environment.getExternalStorageDirectory() + "/";//Get the SD card default path String name = path + "image.jpg";//Get the image File's path+name BitmapFactory.Options option = new BitmapFactory.Options(); option.inSampleSize = 10;//Set the different pixel.The bigger,the lower Bitmap bm = BitmapFactory.decodeFile(name,option);//Decode the image file according to the pathName and the pixel myView.setImageBitmap(bm);//Let the ImageView myView equal to the bitmap }}
注意:myView.setImageBitmap(bm); 是在代码中设置xml中的id属性。(因为此刻图片资源并不来自app本身(或者说R资源),是来自SD卡。)
点击这里下载源代码
0 0
- 从SD卡读图并显示(一)
- 从数据库读图片并在页面中显示
- Android从SD卡读取图片并显示
- Android实现从网络获取图片显示并保存到SD卡的方法
- 单片机读取SD卡中BMP图片文件并送往TFT彩屏上显示(测试成功)(一)
- Android 读取sd卡图片并显示
- android---读取SD卡内容并显示
- android学习笔记(一) 遍历SD卡中所有文件并在listview中显示
- Android从网络获取图片,并缓存到SD卡
- 从网络获取图片,并缓存到SD卡
- android-加载sd卡的图片并显示
- 遍历SD卡并以list形式显示
- android通过ContentPrivider扫描sd卡所有图片,并显示
- 获取SD卡中的图片资源并显示
- android示例之遍历SD卡的文件并显示
- Android读取SD卡文件并显示到textview中
- opencv读图不显示
- 如何从网络获取图片显示并保存到SD卡里
- 第一个 安卓项目 hello world!
- HDU 5308 规律+模拟
- 黑马程序员——Java多线程之线程安全问题
- Codeforces Round #313 (Div. 1) C. Gerald and Giant Chess(DP+组合数取模)(好题)
- Scala入门到精通——第九节 继承与组合
- 从SD卡读图并显示(一)
- iOS 消息推送及本地通知,原理解析
- HDU 3657 Game(取数 最小割)经典
- Qt中多事件使用实例
- 用D3.js进行医疗数据可视化 (四) 堆积区图 (Stacked Area Chart)
- qsort 函数中的cmp
- 在linux上安装oracle(红帽) 前的检查工作
- Rotate List
- hadoop权威指南气象数据下载