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上的图片的更多相关文章

  1. Android 获取SDCard上图片和视频的缩略图

    获取图片缩略图和视频缩略图的方法: Java代码: import java.io.File; import android.app.Activity; import android.graphics. ...

  2. Android四大组件之ContentProvider(二)读取设备上的图片、音频和视频

    Android系统提供了MediaScanner,MediaProvider,MediaStore等接口,通过Content Provider的方式提供给用户.当设备开机或者有SD卡插拔等事件发生时, ...

  3. Android控件上添加图片

    项目中有一个点赞功能,点赞的小图标添加在点赞列表旁边,在xml里可以进行设置,也可以在代码中进行绘图. 下面是两种方法的设置: 1.xml里:一些控件:button.textView等等里面有个属性是 ...

  4. Android获取ImageView上的图片,和一个有可能遇到的问题!

    1.在获取图片前先调用setDrawingCacheEnabled(true)这个方法: 举例:mImageView.setDrawingCacheEnabled(true); 2.之后可以通过get ...

  5. Android获取网页上的图片的代码

    public Bitmap getWebBitmap(String imgUrl) { Bitmap bitmap =null; try { InputStream inputStream = nul ...

  6. 【Android】读取sdcard卡上的全部图片而且显示,读取的过程有进度条显示

    尽管以下的app还没有做到快图浏览.ES文件浏览器的水平,遇到大sdcard还是会存在读取过久.内存溢出等问题,可是基本思想是这种. 例如以下图.在sdcard卡上有4张图片, 打开app,则会吧sd ...

  7. android 读取sd卡中的图片

    一.获取读取SD卡的权限 <!--在SDCard中创建与删除文件权限  -->    <uses-permission android:name="android.perm ...

  8. android读取远程图片案例

    关键代码:Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);imageview.setImageBitmap(bitm ...

  9. Android 上千张图片的列表滑动加载

    一般项目中图片加载用的比较多的是ImageLoader 但是需求自己配置一些参数 上手有些复杂 对于手机图库中有上千张图片需要加载时 一个使用性能很好的库Glide可以解决 效果图如下 滑动非常流畅 ...

随机推荐

  1. CROC 2016 - Elimination Round (Rated Unofficial Edition) C. Enduring Exodus 二分

    C. Enduring Exodus 题目连接: http://www.codeforces.com/contest/655/problem/C Description In an attempt t ...

  2. iOS开发系列--通讯录、蓝牙、

    iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用系统应用.使用系统服务: ...

  3. 如何解决…has been modified since the precompiled header… was built的问题

    如何解决…has been modified since the precompiled header… was built 的问题 xcode5.1在程序中报错: File '/Applicatio ...

  4. C# WebHelper-CookieHelper,CacheHelper,SessionHelper

    常用web操作工具类,记录一下,本文记录的工具类,都要求引用 System.Web 1.CookieHelper /// <summary> /// Cookie工具类 /// </ ...

  5. Windows Embedded Compact 7网络编程概述(下)

    11.1.1 Select I/O模型 在Windows CE中,Select模型是唯一被支持的I/O模型.Select I/O模型就是利用select函数对I/O进行管理. 函数select的功能在 ...

  6. android启动之SystemServer启动

    SystemServer是Android系统的核心,APK应用中可以直接交互的大部分系统服务都在该进程中执行,常见的比方WindowManagerServer(Wms).ActivityManager ...

  7. FastStone Capture 8.4 注册码

    原文:https://blog.csdn.net/mlin_123/article/details/51557079 name:bluman serial/序列号/注册码:VPISCJULXUFGDD ...

  8. Appium+python自动化13-native和webview切换

    前言 现在大部分app都是混合式的native+webview,对应native上的元素通过uiautomatorviewer很容易定位到,webview上的元素就无法识别了. 一.识别webview ...

  9. VC++ 6.0下OpengGL配置以及glut配置

    转自:http://blog.sina.com.cn/s/blog_5f0cf7bd0100c9oa.html OpenGL官方网站(英文) http://www.opengl.org 下面我将对Wi ...

  10. node最简单的升级

    1.安装n插件 npm install -g n //全局安装 2.升级 n stable //升级 3.packjson升级 npm i -g npm-upgrade 4.升级 npm-upgrad ...