当前位置: 移动技术网 > IT编程>移动开发>Android > Android 读取sdcard上的图片实例(必看)

Android 读取sdcard上的图片实例(必看)

2019年07月24日  | 移动技术网IT编程  | 我要评论

全运会男篮广东,哪吒闹海动画片下载,愤题和尚诘问

android读取sdcard上的图片是非常简单的事情,下面用一个例子来说明这个问题。

首先,在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上的图片实例(必看)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网