欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2017年03月10日 08:45:30   投稿:jingxian  
下面小編就為大家?guī)硪黄狝ndroid 讀取sdcard上的圖片實例(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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防止按鈕重復點擊示例代碼

    Android防止按鈕重復點擊示例代碼

    本文介紹封裝的一個小的工具類庫,按鈕點擊事件類庫,該類庫可以防止按鈕重復點擊,可以判斷網(wǎng)絡狀態(tài),可以判斷用戶登錄態(tài),以及自定義驗證條件等等,有興趣的可以了解一下
    2018-05-05
  • Android ListView滑動改變標題欄背景漸變效果

    Android ListView滑動改變標題欄背景漸變效果

    這篇文章主要為大家詳細介紹了Android ListView滑動改變標題欄背景漸變效果,透明轉變成不透明,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Android自定義Dialog框樣式

    Android自定義Dialog框樣式

    這篇文章主要為大家詳細介紹了Android自定義Dialog框樣式,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • Android AndFix熱修復原理詳情

    Android AndFix熱修復原理詳情

    這篇文章主要介紹了Android AndFix熱修復原理詳情,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值嗎,需要的小伙伴可以參考一下
    2022-08-08
  • Android實現(xiàn)獲取簽名及公鑰的方法

    Android實現(xiàn)獲取簽名及公鑰的方法

    這篇文章主要介紹了Android實現(xiàn)獲取簽名及公鑰的方法,可實現(xiàn)Android通過包名獲取相關簽名及公鑰的功能,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-10-10
  • Android手機衛(wèi)士之設置密碼對話框

    Android手機衛(wèi)士之設置密碼對話框

    這篇文章主要為大家詳細介紹了Android手機衛(wèi)士之設置密碼對話框,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • Flutter以兩種方式實現(xiàn)App主題切換的代碼

    Flutter以兩種方式實現(xiàn)App主題切換的代碼

    這篇文章主要介紹了Flutter以兩種方式實現(xiàn)App主題切換的代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • Android中Hilt的使用詳解

    Android中Hilt的使用詳解

    Hilt?是?Android?的依賴項注入庫,可減少在項目中執(zhí)行手動依賴項注入的樣板代碼,本文就來為大家介紹一下Hilt的具體使用吧,希望對大家有所幫助
    2023-06-06
  • Android開發(fā)Input系統(tǒng)觸摸事件分發(fā)

    Android開發(fā)Input系統(tǒng)觸摸事件分發(fā)

    這篇文章主要為大家介紹了Android開發(fā)Input系統(tǒng)觸摸事件分發(fā)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • Android之使用Android-query框架開發(fā)實戰(zhàn)(二)

    Android之使用Android-query框架開發(fā)實戰(zhàn)(二)

    這篇文章主要介紹了Android之使用Android-query框架開發(fā)實戰(zhàn)(二)的相關資料,需要的朋友可以參考下
    2015-10-10

最新評論