发布于 2017-03-20 10:07:30 | 122 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


下面小编就为大家带来一篇Android 读取sdcard上的图片实例(必看)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务