最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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的權(quán)限,加入一條語句之后,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ù)權(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修改這個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)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

牙克石市| 布尔津县| 合江县| 双辽市| 漯河市| 隆子县| 平顶山市| 长寿区| 佛冈县| 遵义市| 南乐县| 五莲县| 炉霍县| 叙永县| 神池县| 乌兰县| 华蓥市| 通辽市| 常德市| 读书| 余江县| 康马县| 浦江县| 什邡市| 都江堰市| 莲花县| 武隆县| 云安县| 栾川县| 南皮县| 德令哈市| 高邑县| 盐城市| 乌拉特中旗| 佛冈县| 阿巴嘎旗| 龙江县| 恩平市| 岳池县| 山西省| 霍邱县|