天天看點

android 讀取sdcard圖檔 不顯示,Android 讀取sdcard上的圖檔執行個體(必看)

Android讀取sdcard上的圖檔是非常簡單的事情,下面用一個例子來說明這個問題。

首先,在sdcard上有一張已經準備好的img25.jpg

android 讀取sdcard圖檔 不顯示,Android 讀取sdcard上的圖檔執行個體(必看)

下面,需要做的是把這張圖檔讀取到app中顯示。做到如下的效果:

android 讀取sdcard圖檔 不顯示,Android 讀取sdcard上的圖檔執行個體(必看)

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上的圖檔執行個體(必看)就是小編分享給大家的全部内容了,希望能給大家一個參考,也希望大家多多支援找一找教程網。