国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 讀取sdcard上的圖片實例(必看)

2019-12-12 03:25:46
字體:
來源:轉載
供稿:網友

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上的圖片實例(必看)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 高阳县| 大厂| 射阳县| 青冈县| 武川县| 东阿县| 蓬安县| 永宁县| 浦城县| 祁东县| 贵阳市| 西宁市| 宣武区| 肃南| 轮台县| 德昌县| 泌阳县| 松江区| 廊坊市| 元朗区| 柳江县| 揭西县| 武平县| 通江县| 牡丹江市| 留坝县| 奉贤区| 鲁山县| 柯坪县| 泰宁县| 会理县| 繁峙县| 瑞金市| 中江县| 楚雄市| 富川| 开远市| 贵港市| 左云县| 天水市| 龙游县|