【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卡不存在!");
		}

	}

}
时间: 2024-08-04 01:06:00

【Android】读取sdcard上的图片的相关文章

Android控件上添加图片

项目中有一个点赞功能,点赞的小图标添加在点赞列表旁边,在xml里可以进行设置,也可以在代码中进行绘图. 下面是两种方法的设置: 1.xml里:一些控件:button.textView等等里面有个属性是android:drawableLeft 就可以将pic设置到text的左边.good.... 2.代码中: TextView txtlikedList = new TextView(this.getContext()); Drawable drawable= getResources().getD

Android从网络上下载图片实现

1.背景介绍 网络上图片的请求,是我们最常见的网络请求之一,不亚于对json/xml数据的请求.一般要展示给用户看的,都不会是纯粹的文字,往往都是图文信息.而在移动互联网时代,图文又往往需要最新的资讯,数据都是从网络上获取. 像我们都在使用的微信,它的朋友圈中就好多图文信息:使用的新浪微博,用户的图标也是图片信息,等等诸如此类.由此可见,对于图片的请求处理,非常重要,我们做开发的应该掌握.今天介绍一下笔者在开发Android项目过程中使用过的一些代码. 2.思路分析 (1)取得与服务器的连接 (

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

获取图片缩略图和视频缩略图的方法: Java代码: import java.io.File; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.media.ThumbnailUtils; import android.os.Bundle; import android.os.Environment; import an

Android 读取本地(SD卡)图片

private Bitmap getDiskBitmap(String pathString) { Bitmap bitmap = null; try { File file = new File(pathString); if(file.exists()) { bitmap = BitmapFactory.decodeFile(pathString); } } catch (Exception e) { // TODO: handle exception } return bitmap; }

android 在SdCard上创建数据库

数据库管理类中使用到的是自定义的Context,而非app的上下文对象: /** * 用于支持对存储在SD卡上的数据库的访问 **/ public class DbContext extends ContextWrapper { /** * 构造函数 * * @param base * 上下文环境 */ public DbContext(Context base) { super(base); } /** * 获得数据库路径,如果不存在,则创建对象对象 * * @param name * @pa

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

public Bitmap getWebBitmap(String imgUrl) { Bitmap bitmap =null; try { InputStream inputStream = null; URL url; url = new URL(imgUrl); if (url != null) { // 打开连接 HttpURLConnection httpURLConnection = (HttpURLConnection) url .openConnection(); httpURL

Android 读取assets里的图片

1 Bitmap btimg = getImageFromAssetsFile("image/ic_launcher.png"); 2 3 private Bitmap getImageFromAssetsFile(String fileName) 4 { 5 Bitmap image = null; 6 AssetManager am = getResources().getAssets(); 7 try 8 { 9 InputStream is = am.open(fileName

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

尽管以下的app还没有做到快图浏览.ES文件浏览器的水平,遇到大sdcard还是会存在读取过久.内存溢出等问题,可是基本思想是这种. 例如以下图.在sdcard卡上有4张图片, 打开app,则会吧sd卡上的全部图片读取,并显示出来.读取的过程有进度条显示. 制作步骤例如以下: 1.首先,res\values\strings.xml对字符设置例如以下,没有什么特别的. <? xml version="1.0" encoding="utf-8"?> <

【Android】读取sdcard卡上的所有图片并且显示,读取的过程有进度条显示

虽然下面的app还没有做到快图浏览.ES文件浏览器的水平,遇到大sdcard还是会存在读取过久.内存溢出等问题,但是基本思想是这样的. 如下图,在sdcard卡上有4张图片, 打开app,则会吧sd卡上的所有图片读取,并显示出来,读取的过程有进度条显示. 制作过程如下: 1.首先,res\values\strings.xml对字符设置如下,没有什么特别的. <?xml version="1.0" encoding="utf-8"?> <resour