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

首頁 > 系統 > Android > 正文

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

2019-10-23 18:35:18
字體:
來源:轉載
供稿:網友

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

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

android,讀取圖片

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

android,讀取圖片

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


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 肥东县| 霍林郭勒市| 石楼县| 观塘区| 大化| 康乐县| 宝山区| 合江县| 新巴尔虎右旗| 江陵县| 东阳市| 蒙山县| 横山县| 绥滨县| 武宁县| 武夷山市| 随州市| 长子县| 突泉县| 汤原县| 阿鲁科尔沁旗| 南和县| 屏边| 怀安县| 赤峰市| 筠连县| 万年县| 曲靖市| 桐城市| 永福县| 蒙自县| 托里县| 青岛市| 体育| 石城县| 乌苏市| 融水| 苏尼特左旗| 山西省| 万全县| 宝清县|