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

Android從系統(tǒng)Gallery獲取圖片具體實(shí)現(xiàn)

 更新時(shí)間:2013年12月05日 16:33:01   作者:  
這篇文章主要介紹了Android從系統(tǒng)Gallery獲取圖片具體實(shí)現(xiàn),有需要的朋友可以參考一下

前言

  在Android應(yīng)用中,經(jīng)常有場(chǎng)景會(huì)需要使用到設(shè)備上存儲(chǔ)的圖片,而直接從路徑中獲取無(wú)疑是非常不便利的。所以一般推薦調(diào)用系統(tǒng)的Gallery應(yīng)用,選擇圖片,然后使用它。本篇博客將講解如何在Android中通過(guò)系統(tǒng)Gallery獲取圖片。

Gallery應(yīng)用

  Android原生內(nèi)置了很多App,而Gallery為圖庫(kù),用于操作設(shè)備上的圖片,它會(huì)在開(kāi)機(jī)的時(shí)候主動(dòng)掃描設(shè)備上存儲(chǔ)的圖片,并可以使用Gallery操作它們。既然要使用Gallery,那么先看看它的AndroidManifest.xml清單文件。

復(fù)制代碼 代碼如下:

<activity android:name="com.android.camera.ImageGallery"
                android:label="@string/gallery_label"
                android:configChanges="orientation|keyboardHidden"
                android:icon="@drawable/ic_launcher_gallery">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="vnd.android.cursor.dir/image" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="vnd.android.cursor.dir/video" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.GET_CONTENT" />
                <category android:name="android.intent.category.OPENABLE" />
                <data android:mimeType="vnd.android.cursor.dir/image" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.GET_CONTENT" />
                <category android:name="android.intent.category.OPENABLE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="image/*" />
                <data android:mimeType="video/*" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.PICK" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="image/*" />
                <data android:mimeType="video/*" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.PICK" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="vnd.android.cursor.dir/image" />
            </intent-filter>
        </activity>

  上面是Gallery的AndroidManifest.xml文件中的部分代碼,展示了ImageGallery,從眾多Intent-filter中可以看出,選取圖片應(yīng)該使用"android.intent.action.PICK",它有兩個(gè)miniType,"image/*"是用來(lái)獲取圖片的、"video/*"是用來(lái)獲取視頻的。Android中眾多Action的字符串其實(shí)被封裝在Intent類(lèi)中,android.intent.action.PICK也不例外,它是Intent.ACTION_PICK。

  既然知道了啟動(dòng)Gallery的Action,那么再看看ImageGallery.java的源碼,找找其中選中圖片后的返回值。

復(fù)制代碼 代碼如下:

private void launchCropperOrFinish(IImage img) {
        Bundle myExtras = getIntent().getExtras();

        long size = MenuHelper.getImageFileSize(img);
        if (size < 0) {
            // Return if the image file is not available.
            return;
        }

        if (size > mVideoSizeLimit) {
            DialogInterface.OnClickListener buttonListener =
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            };
            new AlertDialog.Builder(this)
                    .setIcon(android.R.drawable.ic_dialog_info)
                    .setTitle(R.string.file_info_title)
                    .setMessage(R.string.video_exceed_mms_limit)
                    .setNeutralButton(R.string.details_ok, buttonListener)
                    .show();
            return;
        }

        String cropValue = myExtras != null ? myExtras.getString("crop") : null;
        if (cropValue != null) {
            Bundle newExtras = new Bundle();
            if (cropValue.equals("circle")) {
                newExtras.putString("circleCrop", "true");
            }

            Intent cropIntent = new Intent();
            cropIntent.setData(img.fullSizeImageUri());
            cropIntent.setClass(this, CropImage.class);
            cropIntent.putExtras(newExtras);

            /* pass through any extras that were passed in */
            cropIntent.putExtras(myExtras);
            startActivityForResult(cropIntent, CROP_MSG);
        } else {
            Intent result = new Intent(null, img.fullSizeImageUri());
            if (myExtras != null && myExtras.getBoolean("return-data")) {
                // The size of a transaction should be below 100K.
                Bitmap bitmap = img.fullSizeBitmap(
                        IImage.UNCONSTRAINED, 100 * 1024);
                if (bitmap != null) {
                    result.putExtra("data", bitmap);
                }
            }
            setResult(RESULT_OK, result);
            finish();
        }
    }

以上的ImageGallery.java的部分源碼,從setResult()方法可以看出,它返回的Intent包含了選中圖片的Uri,它是一個(gè)content://開(kāi)頭的內(nèi)容提供者,并且如果傳遞過(guò)去的Intent的Extra中,包含一個(gè)name為"return-data"并且值為true的時(shí)候,還會(huì)往Extra中寫(xiě)入name為"data"的圖片縮略圖。

Gallery獲取圖片Demo

  既然已經(jīng)知道了啟動(dòng)Gallery的Action,和它如何返回選中的數(shù)據(jù),那么接下來(lái)通過(guò)一個(gè)簡(jiǎn)單的Demo來(lái)演示一下如何從系統(tǒng)Gallery中獲取圖片,并把獲取到的圖片展示到界面的一個(gè)ImageView中。

復(fù)制代碼 代碼如下:

package cn.bgxt.sysgallerydemo;

import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity {
    private Button btn_getImage;
    private ImageView iv_image;
    private final static String TAG = "main";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_getImage = (Button) findViewById(R.id.btn_getImage);
        iv_image = (ImageView) findViewById(R.id.iv_image);

        btn_getImage.setOnClickListener(getImage);
    }

    private View.OnClickListener getImage = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // 設(shè)定action和miniType
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_PICK);
            intent.setType("image/*");
            // 以需要返回值的模式開(kāi)啟一個(gè)Activity
            startActivityForResult(intent, 0);
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // 如果獲取成功,resultCode為-1
        Log.i(TAG, "resultCode:" + resultCode);
        if (requestCode == 0 && resultCode == -1) {
            // 獲取原圖的Uri,它是一個(gè)內(nèi)容提供者
            Uri uri = data.getData();
            iv_image.setImageURI(uri);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}

  效果展示:

總結(jié)

  本篇博客到這里就基本上講解了如何在Android下調(diào)用系統(tǒng)Gallery獲取圖片,其實(shí)功能實(shí)現(xiàn)很簡(jiǎn)單,主要是要注意Action和miniType不要寫(xiě)錯(cuò)了,并且返回值是一個(gè)Uri。雖然現(xiàn)在越來(lái)越多需要用到圖片的商業(yè)應(yīng)用,都在自己開(kāi)發(fā)獲取設(shè)備圖片的功能,但是使用系統(tǒng)自帶的Gallery來(lái)獲取不失為一種快速實(shí)現(xiàn)功能的解決辦法。為了方便起見(jiàn),系統(tǒng)的Gallery源碼,也會(huì)一并打包放到源碼中,有需要的可以下載來(lái)看看。

相關(guān)文章

  • Android打開(kāi)淘寶客戶(hù)端(手淘)效果及實(shí)現(xiàn)代碼

    Android打開(kāi)淘寶客戶(hù)端(手淘)效果及實(shí)現(xiàn)代碼

    這篇文章主要介紹了Android打開(kāi)淘寶客戶(hù)端(手淘)效果及實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2018-04-04
  • Android仿微信界面的導(dǎo)航以及右上角菜單欄效果

    Android仿微信界面的導(dǎo)航以及右上角菜單欄效果

    這篇文章主要介紹了Android仿微信界面的導(dǎo)航以及右上角菜單欄效果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • Android自定義ViewGroup實(shí)現(xiàn)標(biāo)簽流效果

    Android自定義ViewGroup實(shí)現(xiàn)標(biāo)簽流效果

    這篇文章主要為大家詳細(xì)介紹了Android自定義ViewGroup實(shí)現(xiàn)標(biāo)簽流效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • android ndk程序獲取外置SD沙盒目錄的方法講解

    android ndk程序獲取外置SD沙盒目錄的方法講解

    今天小編就為大家分享一篇android ndk程序獲取外置SD沙盒目錄的方法講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • Android項(xiàng)目中引用本地aar文件的方法

    Android項(xiàng)目中引用本地aar文件的方法

    這篇文章主要介紹了Android項(xiàng)目中引用本地aar文件的方法,本文講解了什么是aar文件、導(dǎo)出aar文件方法、引用本地的aar文件方法等內(nèi)容,需要的朋友可以參考下
    2015-05-05
  • Android實(shí)現(xiàn)頂部弧形背景效果

    Android實(shí)現(xiàn)頂部弧形背景效果

    在當(dāng)今移動(dòng)互聯(lián)網(wǎng)應(yīng)用中,界面設(shè)計(jì)的美觀與交互體驗(yàn)往往成為用戶(hù)評(píng)價(jià)一款產(chǎn)品的重要因素之一,本文將詳細(xì)介紹如何在 Android 應(yīng)用中實(shí)現(xiàn)頂部弧形背景效果,通過(guò)自定義 View、繪制原理和動(dòng)畫(huà)特效等多種技術(shù)手段,打造出既美觀又具有較好擴(kuò)展性的界面背景效果
    2025-04-04
  • 使用PackageManager獲得應(yīng)用信息實(shí)例方法

    使用PackageManager獲得應(yīng)用信息實(shí)例方法

    PackageManager是Android中一個(gè)很有用的類(lèi),能夠獲取已安裝的應(yīng)用(包)的信息,如應(yīng)用名稱(chēng)、圖標(biāo)、權(quán)限,安裝、刪除應(yīng)用(包)等
    2013-11-11
  • 分析Android中應(yīng)用的啟動(dòng)流程

    分析Android中應(yīng)用的啟動(dòng)流程

    不知道大家有沒(méi)有好奇過(guò)點(diǎn)擊Launcher圖標(biāo)時(shí),到喚起一個(gè)應(yīng)用頁(yè)面,這個(gè)流程會(huì)是怎么樣的?那這篇文章的目的就是盡可能梳理清楚流程,能夠讓大家對(duì)整個(gè)流程有一個(gè)相對(duì)清晰的認(rèn)知。下面跟著小編一起學(xué)習(xí)學(xué)習(xí)。
    2016-08-08
  • Android照片墻應(yīng)用實(shí)現(xiàn) 再多的圖片也不怕崩潰

    Android照片墻應(yīng)用實(shí)現(xiàn) 再多的圖片也不怕崩潰

    這篇文章主要為大家詳細(xì)介紹了Android照片墻應(yīng)用實(shí)現(xiàn),再多的圖片也不怕崩潰,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • Android編程實(shí)現(xiàn)圖片放大縮小功能ZoomControls控件用法實(shí)例

    Android編程實(shí)現(xiàn)圖片放大縮小功能ZoomControls控件用法實(shí)例

    這篇文章主要介紹了Android編程實(shí)現(xiàn)圖片放大縮小功能ZoomControls控件用法,結(jié)合具體實(shí)例形式分析了Android ZoomControls控件實(shí)現(xiàn)圖片縮放的具體操作方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-09-09

最新評(píng)論

老河口市| 左权县| 福海县| 广德县| 广汉市| 涿鹿县| 白山市| 彭泽县| 北安市| 屏南县| 平江县| 徐州市| 双峰县| 阿拉尔市| 古蔺县| 安仁县| 苏尼特左旗| 漳浦县| 菏泽市| 盱眙县| 延边| 开原市| 伊金霍洛旗| 乐清市| 娱乐| 乐至县| 湘乡市| 保康县| 霍邱县| 开化县| 神木县| 阳春市| 漠河县| 乐业县| 禄劝| 三门峡市| 咸丰县| 左权县| 肇东市| 大余县| 澄江县|