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

Android編程實現(xiàn)調(diào)用系統(tǒng)圖庫與裁剪圖片功能

 更新時間:2017年01月30日 09:29:11   作者:books1958  
這篇文章主要介紹了Android編程實現(xiàn)調(diào)用系統(tǒng)圖庫與裁剪圖片功能,結(jié)合實例形式分析了Android針對圖形的旋轉(zhuǎn)與剪切等具體操作技巧,需要的朋友可以參考下

本文實例講述了Android編程實現(xiàn)調(diào)用系統(tǒng)圖庫與裁剪圖片功能。分享給大家供大家參考,具體如下:

在Android開發(fā)中,調(diào)用系統(tǒng)圖庫和裁剪照片是很常見的需求。相對于自己實現(xiàn)這種功能,直接調(diào)用系統(tǒng)具有諸多優(yōu)點,如不用考慮屏幕適配,不用擔(dān)心性能問題,等等。因此,對于一般的需求,建議直接調(diào)用系統(tǒng)的功能,簡便高效!

首先上效果圖:

  

一、只調(diào)用系統(tǒng)圖庫(不裁剪),返回用戶選擇的圖片。(只支持單選,如需多選則需要自己實現(xiàn),可參考Android編程實現(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:傳遞一個true,告訴系統(tǒng)需要裁剪。
(2)aspectX和aspectY:裁剪框的寬高比。
(3)output:需要傳遞一個由文件路徑cacheFile構(gòu)建的uri,用戶在圖庫頁面選擇照片之后會自動進入裁剪頁面,裁剪之后圖片會被保存在cacheFile這個位置。

裁剪完成之后,同樣會回調(diào)onActivityResult方法(resultCode為RESULT_OK),并且圖片會被保存在cacheFile這個位置,因此可以直接使用這個文件,例如將其設(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相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對大家Android程序設(shè)計有所幫助。

相關(guān)文章

最新評論