99偷拍视频精品区一区二,口述久久久久久久久久久久,国产精品夫妇激情啪发布,成人永久免费网站在线观看,国产精品高清免费在线,青青草在线观看视频观看,久久久久久国产一区,天天婷婷久久18禁,日韩动漫av在线播放直播

利用Android如何獲取sdcard中的圖片-創新互聯

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

我們提供的服務有:成都做網站、成都網站設計、微信公眾號開發、網站優化、網站認證、明山ssl等。為上1000+企事業單位解決了網站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的明山網站制作公司

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寫入數據權限 -->

  <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類讀取。最后創建一個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);//創建一個imageView對象
			if (file.exists()) {
				Bitmap bm = BitmapFactory.decodeFile(filepath);
				// 將圖片顯示到ImageView中
				imageView.setImageBitmap(bm);
				linearLayout1.addView(imageView);
			}
		} else {
			textView1.setText("sd卡不存在!");
		}

	}

}

看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注創新互聯行業資訊頻道,感謝您對創新互聯的支持。

網站名稱:利用Android如何獲取sdcard中的圖片-創新互聯
文章網址:http://www.yijiale78.com/article38/disepp.html

成都網站建設公司_創新互聯,為您提供企業網站制作網站策劃標簽優化品牌網站設計網站排名外貿建站

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

手機網站建設