利用Android如何獲取sdcard中的圖片

利用Android如何獲取sdcard中的圖片?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

漢壽ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!

1、首先你要在AndroidManifest.xml申請(qǐng)讀取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修改這個(gè)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中布置一個(gè)帶id的Textview,一會(huì)兒的提示信息將寫入這個(gè)Textview中,同時(shí)布置一個(gè)帶id的線性布局。一會(huì)兒圖片將會(huì)添加到這個(gè)線性布局里面去。

<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類讀取。最后創(chuàng)建一個(gè)ImageView對(duì)象,將其加載到線性布局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卡不存在!");
		}

	}

}

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。

網(wǎng)頁標(biāo)題:利用Android如何獲取sdcard中的圖片
文章位置:http://muchs.cn/article14/pppsde.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、小程序開發(fā)、自適應(yīng)網(wǎng)站、定制開發(fā)、企業(yè)建站、App設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化