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清單文件。
<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的源碼,找找其中選中圖片后的返回值。
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中。
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)看看。
- Android入門(mén)之Gallery用法實(shí)例解析
- Android 使用Gallery實(shí)現(xiàn)3D相冊(cè)(附效果圖+Demo源碼)
- Android開(kāi)發(fā)學(xué)習(xí)筆記 Gallery和GridView淺析
- Android入門(mén)之Gallery+ImageSwitcher用法實(shí)例解析
- 怎樣刪除android的gallery中的圖片實(shí)例說(shuō)明
- Android控件Gallery3D效果實(shí)例代碼
- Android控件系列之相冊(cè)Gallery&Adapter適配器入門(mén)&控件縮放動(dòng)畫(huà)入門(mén)
- Android App開(kāi)發(fā)中使用RecyclerView實(shí)現(xiàn)Gallery畫(huà)廊的實(shí)例
- Android控件之Gallery用法實(shí)例分析
- Android高級(jí)組件Gallery畫(huà)廊視圖使用方法詳解
相關(guān)文章
Android ListView自定義Adapter實(shí)現(xiàn)仿QQ界面
這篇文章主要為大家詳細(xì)介紹了ListView自定義Adapter實(shí)現(xiàn)仿QQ界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android開(kāi)關(guān)控件Switch的使用案例
今天小編就為大家分享一篇關(guān)于Android開(kāi)關(guān)控件Switch的使用案例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03Android RenderScript實(shí)現(xiàn)高斯模糊
這篇文章主要為大家詳細(xì)介紹了Android RenderScript實(shí)現(xiàn)高斯模糊的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12ListView異步加載圖片實(shí)現(xiàn)思路(優(yōu)化篇)
關(guān)于listview的異步加載,網(wǎng)上其實(shí)很多示例了,中心思想都差不多,不過(guò)很多版本或是有bug,或是有性能問(wèn)題有待優(yōu)化,下面就讓在下闡述其原理以探索個(gè)中奧秘2013-04-04Android 下載文件通知欄顯示進(jìn)度條功能的實(shí)例代碼
這篇文章主要介紹了Android 下載文件通知欄顯示進(jìn)度條功能的實(shí)例代碼,通過(guò)使用AsyncTask異步任務(wù)實(shí)現(xiàn),調(diào)用publishProgress()方法刷新進(jìn)度來(lái)實(shí)現(xiàn),具體代碼大家參考下本文2018-04-04TextView長(zhǎng)按復(fù)制的實(shí)現(xiàn)方法(總結(jié))
下面小編就為大家?guī)?lái)一篇TextView長(zhǎng)按復(fù)制的實(shí)現(xiàn)方法(總結(jié))。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04Android利用Intent啟動(dòng)和關(guān)閉Activity
這篇文章主要為大家詳細(xì)介紹了Android利用Intent啟動(dòng)和關(guān)閉Activity的相關(guān)操作,感興趣的小伙伴們可以參考一下2016-06-06Android模擬器安裝APP出現(xiàn)INSTALL_FAILED_NO_MATCHING_ABIS錯(cuò)誤解決方案
這篇文章主要介紹了 Android模擬器安裝APP出現(xiàn)INSTALL_FAILED_NO_MATCHING_ABIS錯(cuò)誤解決方案的相關(guān)資料,需要的朋友可以參考下2016-12-12詳解Android壁紙服務(wù)的啟動(dòng)過(guò)程
你有設(shè)置過(guò)手機(jī)的壁紙嗎,你知道壁紙是什么樣的程序它是怎么在后臺(tái)運(yùn)行的嗎?這篇文章主要介紹了詳解Android系統(tǒng)壁紙服務(wù)的啟動(dòng)過(guò)程2021-08-08Android-Zxing實(shí)現(xiàn)二維碼的掃描與生成
本文主要介紹了Android中Zxing實(shí)現(xiàn)二維碼的掃描與生成的方法,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02