android sp存储图片,Android 读取sdcard上的图片实例(必看)

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

首先,在sdcard上有一张已经准备好的img25.jpg

38109eaeabd50fe47200284927e98999.png

下面,需要做的是把这张图片读取到app中显示。做到如下的效果:

921bcfcb0e9f3f483c5b7ec7f5a72720.png

1、首先你要在AndroidManifest.xml申请读取sdcard的权限,加入一条语句之后,AndroidManifest.xml如下:

package="com.sdcardread"

android:versionCode="1"

android:versionName="1.0" >

android:minSdkVersion="8"

android:targetSdkVersion="18" />

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

android:name="com.sdcardread.MainActivity"

android:label="@string/app_name" >

2、之后在res\values\strings.xml修改这个app名称为“图片读取”,这步可以不做,只是为了程序更加美观。

图片读取

Settings

3、其次在res\layout\activity_main.xml中布置一个带id的Textview,一会儿的提示信息将写入这个Textview中,同时布置一个带id的线性布局。一会儿图片将会添加到这个线性布局里面去。

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="24sp" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="24sp" />

android:id="@+id/linearLayout1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值