【Android】读取sdcard上的图片
Android读取sdcard上的图片是很easy的事情,以下用一个样例来说明这个问题。
首先,在sdcard上有一张已经准备好的img25.jpg
以下,须要做的是把这张图片读取到app中显示。
做到例如以下的效果:
1、首先你要在AndroidManifest.xml申请读取sdcard的权限,增加一条语句之后,AndroidManifest.xml例如以下:
<?xml version="1.0" encoding="utf-8"? >
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sdcardread"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 向SDCard写入数据权限 --> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.sdcardread.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>
2、之后在res\values\strings.xml改动这个app名称为“图片读取”。这步能够不做,仅仅是为了程序更加美观。
<?xml version="1.0" encoding="utf-8"? >
<resources> <string name="app_name">图片读取</string>
<string name="action_settings">Settings</string> </resources>
3、其次在res\layout\activity_main.xml中布置一个带id的Textview,一会儿的提示信息将写入这个Textview中,同一时候布置一个带id的线性布局。一会儿图片将会加入到这个线性布局里面去。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp" /> <LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout> </LinearLayout>
4、整个程序的核心在MainActivity.java,代码例如以下,获取组件之后,先用Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);推断sdcard是否存在。之后使用Environment.getExternalStorageDirectory().getAbsolutePath();获取sdcard的绝对路径供Java的File类读取。
最后创建一个ImageView对象,将其载入到线性布局linearLayout1之中。
package com.sdcardread; import java.io.File; import android.os.Bundle;
import android.os.Environment;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory; public class MainActivity extends Activity {
private TextView textView1;
private LinearLayout linearLayout1; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) findViewById(R.id.textView1);
linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
boolean isSdCardExist = Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);// 推断sdcard是否存在
if (isSdCardExist) {
String sdpath = Environment.getExternalStorageDirectory()
.getAbsolutePath();// 获取sdcard的根路径
textView1.setText("sd卡是存在的。下面是sdcard下的img25.jpg!");
String filepath = sdpath + File.separator + "img25.jpg";
File file = new File(filepath);
ImageView imageView = new ImageView(this);//创建一个imageView对象
if (file.exists()) {
Bitmap bm = BitmapFactory.decodeFile(filepath);
// 将图片显示到ImageView中
imageView.setImageBitmap(bm);
linearLayout1.addView(imageView);
}
} else {
textView1.setText("sd卡不存在!");
} } }
【Android】读取sdcard上的图片的更多相关文章
- Android 获取SDCard上图片和视频的缩略图
获取图片缩略图和视频缩略图的方法: Java代码: import java.io.File; import android.app.Activity; import android.graphics. ...
- Android四大组件之ContentProvider(二)读取设备上的图片、音频和视频
Android系统提供了MediaScanner,MediaProvider,MediaStore等接口,通过Content Provider的方式提供给用户.当设备开机或者有SD卡插拔等事件发生时, ...
- Android控件上添加图片
项目中有一个点赞功能,点赞的小图标添加在点赞列表旁边,在xml里可以进行设置,也可以在代码中进行绘图. 下面是两种方法的设置: 1.xml里:一些控件:button.textView等等里面有个属性是 ...
- Android获取ImageView上的图片,和一个有可能遇到的问题!
1.在获取图片前先调用setDrawingCacheEnabled(true)这个方法: 举例:mImageView.setDrawingCacheEnabled(true); 2.之后可以通过get ...
- Android获取网页上的图片的代码
public Bitmap getWebBitmap(String imgUrl) { Bitmap bitmap =null; try { InputStream inputStream = nul ...
- 【Android】读取sdcard卡上的全部图片而且显示,读取的过程有进度条显示
尽管以下的app还没有做到快图浏览.ES文件浏览器的水平,遇到大sdcard还是会存在读取过久.内存溢出等问题,可是基本思想是这种. 例如以下图.在sdcard卡上有4张图片, 打开app,则会吧sd ...
- android 读取sd卡中的图片
一.获取读取SD卡的权限 <!--在SDCard中创建与删除文件权限 --> <uses-permission android:name="android.perm ...
- android读取远程图片案例
关键代码:Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);imageview.setImageBitmap(bitm ...
- Android 上千张图片的列表滑动加载
一般项目中图片加载用的比较多的是ImageLoader 但是需求自己配置一些参数 上手有些复杂 对于手机图库中有上千张图片需要加载时 一个使用性能很好的库Glide可以解决 效果图如下 滑动非常流畅 ...
随机推荐
- BZOJ4552 HEOI2016排序
太棒了!思路很不错. 没想到HEOID1三道线段树. 这题我们可以二分答案,将小于他的在线段树中设成0,大于他的设成1然后模拟操作复杂度O(mlog^2n) By:大奕哥 #include<bi ...
- HDU 3404 Switch lights 博弈论 nim积
http://acm.hdu.edu.cn/showproblem.php?pid=3404 题目 http://www.doc88.com/p-5098170314707.html 论文 nim积在 ...
- Python字典树实现
class Trie: # word_end = -1 def __init__(self): """ Initialize your data structure he ...
- ted飞行器
http://v.youku.com/v_show/id_XNTc0MTk0MzI4.html
- MONO,原来你是水中月
什么是MONO? MONO项目是由Ximian发起的,由Miguel de lcaza领导的,一个致力于开创.NET在Linux上使用的开源工程.它包含了一个C#语言的编译器,一个CLR的运行时,和一 ...
- Windows Server 2008 Standard Enterprise Datacenter各个版本区别
-- Windows Server 2008 Standard 包含1个虚拟实例许可,5个客户端访问授权,售价999美元. -- Windows Server 2008 Enterprise 包含4个 ...
- 让linux history命令显示命令的运行时间、在哪个机器运行的这个命令
1.在/etc/profile的最后加入例如以下部分: USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g ...
- [翻译] UIView-draggable 可拖拽的UIView
UIView-draggable 可拖拽的UIView https://github.com/andreamazz/UIView-draggable UIView category that adds ...
- 替换Android系统镜像system.img的方法
之前改动了Android的系统源代码的framework层代码,定制ROM.通过make之后会生成三个镜像文件userdata.img.system.img.ramdisk.img三个文件.这个时候我 ...
- 关于JDBC PreparedStatement
PreparedStatement的执行步骤: 1. 向数据库服务器发送SQL语句,数据库对SQL进行解析和优化(conn.preparedStatement(sql)) 2. 向数据库发送绑定的参数 ...