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的方法
相關文章
基于Google ML模型開發(fā)Android物體檢測應用
ML Kit是Google提供的機器學習SDK,包含了一系列預訓練模型,可以在Android和iOS應用中快速添加機器學習功能,本項目基于Google ML模型開發(fā)Android物體檢測應用,首先對圖像中的物體進行分類檢測,獲取分類物體的位置區(qū)域,然后結合圖像標記,逐個獲取單個物體的標簽2024-07-07
詳解Android的內(nèi)存優(yōu)化--LruCache
LruCache是基于Lru算法實現(xiàn)的一種緩存機制。本文對LruCache的概念和實現(xiàn)原理進行介紹,通過實例分析和使用介紹,讓大家更好的了解LruCache,下面跟著小編一起來看下吧2016-12-12
Android使用SoundPool實現(xiàn)播放音效
這篇文章主要為大家詳細介紹了Android使用SoundPool實現(xiàn)播放音效,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11
基于android startActivityForResult的學習心得總結
本篇文章是對android中的startActivityForResult進行了詳細的分析介紹,需要的朋友參考下2013-05-05
AndroidStudio 3.6 中 R.layout 找不到對應的xml文件問題及解決方法
這篇文章主要介紹了AndroidStudio 3.6 中 R.layout 找不到對應的xml文件問題,本文給出了解決方法對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
使用Android Studio Gradle實現(xiàn)友盟多渠道打包
這篇文章主要介紹了使用Android Studio Gradle實現(xiàn)友盟多渠道打包,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
利用Android中BitmapShader制作自帶邊框的圓形頭像
這篇文章給大家介紹了一下如何利用BitmapShader制作圓形頭像,可以自定義要顯示的圖片,邊框顏色和邊框?qū)挾鹊龋行枰呐笥褌兛梢詤⒖冀梃b。2016-09-09
Android修改源碼解決Alertdialog觸摸對話框邊緣消失的問題
在開發(fā)的時候遇到一個問題,就是一觸摸對話框邊緣外部,對話框會自動消失。這個問題很糾結啊,查找了一下發(fā)現(xiàn)從Android 4.0開始,AlertDialog有了變化,就是在觸摸對話框邊緣外部,對話框會自動消失,查了源碼,找到解決辦法如下2013-11-11

