Android開發(fā)中怎么調(diào)用系統(tǒng)圖庫

這篇文章將為大家詳細(xì)講解有關(guān)Android開發(fā)中怎么調(diào)用系統(tǒng)圖庫,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識(shí)有一定的了解。

10多年的衡山網(wǎng)站建設(shè)經(jīng)驗(yàn),針對設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。全網(wǎng)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整衡山建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“衡山網(wǎng)站設(shè)計(jì)”,“衡山網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

首先上效果圖:

Android開發(fā)中怎么調(diào)用系統(tǒng)圖庫  

一、只調(diào)用系統(tǒng)圖庫(不裁剪),返回用戶選擇的圖片。(只支持單選,如需多選則需要自己實(shí)現(xiàn),可參考Android編程實(shí)現(xiàn)仿QQ照片選擇器(按相冊分類顯示,多選添加)源碼。)

1.跳轉(zhuǎn)至系統(tǒng)圖庫頁面:

Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECT_IMAGE);

2.在onActivityResult中接收系統(tǒng)圖庫返回的信息(也就是用戶選擇的照片)。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode != RESULT_OK || data == null) {
      return;
    }
    //select an image
    if (requestCode == SELECT_IMAGE) {
      //get image path from uri
      String imagePath = getImagePath(data.getData());
      return;
    }
}
private String getImagePath(Uri selectedImage) {
    String[] filePathColumn = {MediaStore.Images.Media.DATA};
    Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
    cursor.moveToFirst();
    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    String imagePath = cursor.getString(columnIndex);
    cursor.close();
    System.out.println("image path:" + imagePath);
    return imagePath;
}

二、跳轉(zhuǎn)至系統(tǒng)圖庫,選擇照片,并裁剪。

1.跳轉(zhuǎn)至系統(tǒng)圖庫:

//select image via system gallery, crop and save the new image file.
public void selectImageAndCrop(View view) {
    //After cropping, the image file will be stored here!
    cacheFile = imageCacheFolder + File.separator + "cache_" + System.currentTimeMillis() + ".jpg";
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/*");
    intent.putExtra("crop", "true");
    //width:height
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    intent.putExtra("output", Uri.fromFile(new File(cacheFile)));
    intent.putExtra("outputFormat", "JPEG");
    startActivityForResult(Intent.createChooser(intent, "Choose Image"), SELECT_IMAGE_CROP);
}

跳轉(zhuǎn)之前需要傳遞的數(shù)據(jù):

(1)crop:傳遞一個(gè)true,告訴系統(tǒng)需要裁剪。
(2)aspectX和aspectY:裁剪框的寬高比。
(3)output:需要傳遞一個(gè)由文件路徑cacheFile構(gòu)建的uri,用戶在圖庫頁面選擇照片之后會(huì)自動(dòng)進(jìn)入裁剪頁面,裁剪之后圖片會(huì)被保存在cacheFile這個(gè)位置。

裁剪完成之后,同樣會(huì)回調(diào)onActivityResult方法(resultCode為RESULT_OK),并且圖片會(huì)被保存在cacheFile這個(gè)位置,因此可以直接使用這個(gè)文件,例如將其設(shè)置為ImageView的資源。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode != RESULT_OK) {
      return;
    }
    //select an image and crop
    if (requestCode == SELECT_IMAGE_CROP) {
       //compress the original image to save memory.
      BitmapFactory.Options opt = new BitmapFactory.Options();
      opt.inSampleSize = 4;
      imageView.setImageBitmap(BitmapFactory.decodeFile(cacheFile, opt));
    }
}

關(guān)于Android開發(fā)中怎么調(diào)用系統(tǒng)圖庫就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

網(wǎng)頁名稱:Android開發(fā)中怎么調(diào)用系統(tǒng)圖庫
URL網(wǎng)址:http://muchs.cn/article4/gpjdoe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航虛擬主機(jī)、商城網(wǎng)站營銷型網(wǎng)站建設(shè)、品牌網(wǎng)站制作面包屑導(dǎo)航

廣告

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

成都網(wǎng)站建設(shè)公司