Android 讀取sdcard上的圖片實例(必看)
Android讀取sdcard上的圖片是非常簡單的事情,下面用一個例子來說明這個問題。
首先,在sdcard上有一張已經(jīng)準備好的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寫入數(shù)據(jù)權限 --> <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類讀取。最后創(chuàng)建一個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);//創(chuàng)建一個imageView對象 if (file.exists()) { Bitmap bm = BitmapFactory.decodeFile(filepath); // 將圖片顯示到ImageView中 imageView.setImageBitmap(bm); linearLayout1.addView(imageView); } } else { textView1.setText("sd卡不存在!"); } } }
以上這篇Android 讀取sdcard上的圖片實例(必看)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Android 網(wǎng)絡圖片查看顯示的實現(xiàn)方法
- Android讀取本地或網(wǎng)絡圖片并轉換為Bitmap
- Android 異步獲取網(wǎng)絡圖片并處理導致內(nèi)存溢出問題解決方法
- Android顯示網(wǎng)絡圖片實例
- Android 下載網(wǎng)絡圖片并顯示到本地
- 簡單實現(xiàn)Android讀取網(wǎng)絡圖片到本地
- Android使用線程獲取網(wǎng)絡圖片的方法
- 在Android的應用中實現(xiàn)網(wǎng)絡圖片異步加載的方法
- Android實現(xiàn)網(wǎng)絡圖片瀏覽功能
- Android sdcard實現(xiàn)圖片存儲 、聯(lián)網(wǎng)下載
- Android開發(fā)實現(xiàn)加載網(wǎng)絡圖片并下載至本地SdCard的方法
相關文章
Flutter以兩種方式實現(xiàn)App主題切換的代碼
這篇文章主要介紹了Flutter以兩種方式實現(xiàn)App主題切換的代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04Android開發(fā)Input系統(tǒng)觸摸事件分發(fā)
這篇文章主要為大家介紹了Android開發(fā)Input系統(tǒng)觸摸事件分發(fā)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03Android之使用Android-query框架開發(fā)實戰(zhàn)(二)
這篇文章主要介紹了Android之使用Android-query框架開發(fā)實戰(zhàn)(二)的相關資料,需要的朋友可以參考下2015-10-10