码迷,mamicode.com
首页 > 移动开发 > 详细

【Android】读取sdcard上的图片

时间:2017-08-09 23:57:10      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:data   组件   port   extern   iss   add   blog   environ   信息   

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上的图片

标签:data   组件   port   extern   iss   add   blog   environ   信息   

原文地址:http://www.cnblogs.com/brucemengbm/p/7327898.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!