Android 讀取sdcard上的圖片實(shí)例(必看)
Android讀取sdcard上的圖片是非常簡(jiǎn)單的事情,下面用一個(gè)例子來(lái)說(shuō)明這個(gè)問(wèn)題。
首先,在sdcard上有一張已經(jīng)準(zhǔn)備好的img25.jpg
下面,需要做的是把這張圖片讀取到app中顯示。做到如下的效果:
1、首先你要在AndroidManifest.xml申請(qǐng)讀取sdcard的權(quán)限,加入一條語(yǔ)句之后,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寫(xiě)入數(shù)據(jù)權(quán)限 --> <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修改這個(gè)app名稱(chēng)為“圖片讀取”,這步可以不做,只是為了程序更加美觀。
<?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中布置一個(gè)帶id的Textview,一會(huì)兒的提示信息將寫(xiě)入這個(gè)Textview中,同時(shí)布置一個(gè)帶id的線(xiàn)性布局。一會(huì)兒圖片將會(huì)添加到這個(gè)線(xiàn)性布局里面去。
<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、整個(gè)程序的核心在MainActivity.java,代碼如下,獲取組件之后,先用Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);判斷sdcard是否存在,之后使用Environment.getExternalStorageDirectory().getAbsolutePath();獲取sdcard的絕對(duì)路徑供Java的File類(lèi)讀取。最后創(chuàng)建一個(gè)ImageView對(duì)象,將其加載到線(xiàn)性布局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);//創(chuàng)建一個(gè)imageView對(duì)象 if (file.exists()) { Bitmap bm = BitmapFactory.decodeFile(filepath); // 將圖片顯示到ImageView中 imageView.setImageBitmap(bm); linearLayout1.addView(imageView); } } else { textView1.setText("sd卡不存在!"); } } }
以上這篇Android 讀取sdcard上的圖片實(shí)例(必看)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Android 網(wǎng)絡(luò)圖片查看顯示的實(shí)現(xiàn)方法
- Android讀取本地或網(wǎng)絡(luò)圖片并轉(zhuǎn)換為Bitmap
- Android 異步獲取網(wǎng)絡(luò)圖片并處理導(dǎo)致內(nèi)存溢出問(wèn)題解決方法
- Android顯示網(wǎng)絡(luò)圖片實(shí)例
- Android 下載網(wǎng)絡(luò)圖片并顯示到本地
- 簡(jiǎn)單實(shí)現(xiàn)Android讀取網(wǎng)絡(luò)圖片到本地
- Android使用線(xiàn)程獲取網(wǎng)絡(luò)圖片的方法
- 在Android的應(yīng)用中實(shí)現(xiàn)網(wǎng)絡(luò)圖片異步加載的方法
- Android實(shí)現(xiàn)網(wǎng)絡(luò)圖片瀏覽功能
- Android sdcard實(shí)現(xiàn)圖片存儲(chǔ) 、聯(lián)網(wǎng)下載
- Android開(kāi)發(fā)實(shí)現(xiàn)加載網(wǎng)絡(luò)圖片并下載至本地SdCard的方法
相關(guān)文章
Android ListView滑動(dòng)改變標(biāo)題欄背景漸變效果
這篇文章主要為大家詳細(xì)介紹了Android ListView滑動(dòng)改變標(biāo)題欄背景漸變效果,透明轉(zhuǎn)變成不透明,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Android實(shí)現(xiàn)獲取簽名及公鑰的方法
這篇文章主要介紹了Android實(shí)現(xiàn)獲取簽名及公鑰的方法,可實(shí)現(xiàn)Android通過(guò)包名獲取相關(guān)簽名及公鑰的功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Android手機(jī)衛(wèi)士之設(shè)置密碼對(duì)話(huà)框
這篇文章主要為大家詳細(xì)介紹了Android手機(jī)衛(wèi)士之設(shè)置密碼對(duì)話(huà)框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10Flutter以?xún)煞N方式實(shí)現(xiàn)App主題切換的代碼
這篇文章主要介紹了Flutter以?xún)煞N方式實(shí)現(xiàn)App主題切換的代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04Android開(kāi)發(fā)Input系統(tǒng)觸摸事件分發(fā)
這篇文章主要為大家介紹了Android開(kāi)發(fā)Input系統(tǒng)觸摸事件分發(fā)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Android之使用Android-query框架開(kāi)發(fā)實(shí)戰(zhàn)(二)
這篇文章主要介紹了Android之使用Android-query框架開(kāi)發(fā)實(shí)戰(zhàn)(二)的相關(guān)資料,需要的朋友可以參考下2015-10-10