溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android開發中怎么調用系統圖庫

發布時間:2020-11-21 15:26:09 來源:億速云 閱讀:271 作者:Leah 欄目:移動開發

這篇文章將為大家詳細講解有關Android開發中怎么調用系統圖庫,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

首先上效果圖:

Android開發中怎么調用系統圖庫  

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

1.跳轉至系統圖庫頁面:

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

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

@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;
}

二、跳轉至系統圖庫,選擇照片,并裁剪。

1.跳轉至系統圖庫:

//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);
}

跳轉之前需要傳遞的數據:

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

裁剪完成之后,同樣會回調onActivityResult方法(resultCode為RESULT_OK),并且圖片會被保存在cacheFile這個位置,因此可以直接使用這個文件,例如將其設置為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));
    }
}

關于Android開發中怎么調用系統圖庫就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女