加载手机磁盘里的图片文件(BitmapFactory.decodeFile()
2016-03-08 22:53
465 查看
加载手机磁盘里的图片文件(BitmapFactory.decodeFile()
新建一个继承Activity类的BitmapFactoryDeocdeFileActivity,并设置布局文件为:bitmapdecodefile.xml。在布局文件中添加一个Button和一个ImageView组件
<Button android:id="@+id/bitmapfactorydecodefile_btn" style="@android:style/Widget.Button.Inset" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/change" /> <ImageView android:id="@+id/bitmapfactorydecodefile_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> |
package lyx.feng.second; ...... public class BitmapFactoryDeocdeFileActivity extends Activity implements OnClickListener { private Button btn = null; private ImageView image = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.bitmapdecodefile); // 取得组件,注册按钮事件 this.btn = (Button) super .findViewById(R.id.bitmapfactorydecodefile_btn); this.image = (ImageView) super .findViewById(R.id.bitmapfactorydecodefile_image); this.btn.setOnClickListener(this); } @Override public void onClick(View v) { // 得到一个图片用于保存到内存中 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_002); File file = new File("data/data/lyx.feng.simpletextdemo/image.png"); if (!file.exists()) { // 如果文件不存在就保存一张图片到File中 try { FileOutputStream stream = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); } catch (FileNotFoundException e) { e.printStackTrace(); } } else { bitmap = null; // 读取图片到ImageView中 bitmap = BitmapFactory .decodeFile("data/data/lyx.feng.simpletextdemo/image.png"); this.image.setImageBitmap(bitmap); } } } |
|
|
|
|
|
|
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories