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

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)文章

最新評(píng)論