網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了Android實現(xiàn)從相冊選擇照片功能的具體代碼,供大家參考,具體內(nèi)容如下
之前在 《Android開發(fā)之裁剪照片》一文中介紹過,如何從相冊選取照片和拍攝照片并進(jìn)行裁切,在本篇中主要向介紹從相冊選擇原生照片(不裁切)。
第一步:向系統(tǒng)發(fā)送選擇照片的意圖。
/** ?* 從相冊選擇原生的照片(不裁切) ?*/ private void selectFromGallery() { ? ? // TODO Auto-generatedmethod stub ? ? Intentintent=new Intent(); ? ? intent.setAction(Intent.ACTION_PICK);//Pick an item fromthe data ? ? intent.setType("image/*");//從所有圖片中進(jìn)行選擇 ? ? startActivityForResult(intent, SELECT_ORIGINAL_PIC); ?? }
第二步:處理系統(tǒng)返回的結(jié)果。?
switch (requestCode) { case SELECT_ORIGINAL_PIC: ? ? if (resultCode==RESULT_OK) {//從相冊選擇照片不裁切 ? ? ? ?try { ? ? ? ? ? ?Uri selectedImage = data.getData(); //獲取系統(tǒng)返回的照片的Uri ? ? ? ? ? ?String[] filePathColumn = { MediaStore.Images.Media.DATA };? ? ? ? ? ? ?Cursor cursor =getContentResolver().query(selectedImage,? ? ? ? ? ? ? ? ? ? filePathColumn, null, null, null);//從系統(tǒng)表中查詢指定Uri對應(yīng)的照片 ? ? ? ? ? ?cursor.moveToFirst();? ? ? ? ? ? ?int columnIndex = cursor.getColumnIndex(filePathColumn[0]); ? ? ? ? ? ?String picturePath = cursor.getString(columnIndex); ?//獲取照片路徑 ? ? ? ? ? ?cursor.close();? ? ? ? ? ? ?Bitmap bitmap= BitmapFactory.decodeFile(picturePath); ? ? ? ? ? ?imgShow.setImageBitmap(bitmap); ? ? ? ?} catch (Exception e) { ? ? ? ? ? ?// TODO Auto-generatedcatch block ? ? ? ? ? ?e.printStackTrace(); ? ? ? ?} ? ? } ? ? break; }
代碼說明:
當(dāng)向系統(tǒng)發(fā)送選擇照片的意圖后,系統(tǒng)或啟動相冊管理程序,讓用戶來選擇照片,選取好照片之后,系統(tǒng)會返回一個選擇照片的Uri,為了獲取Uri對應(yīng)的照片的絕對路徑,我們需要向系統(tǒng)的媒體數(shù)據(jù)框中查找指定Uri對應(yīng)的圖片路徑。獲取到圖片的絕對路徑之后,我們就可以做一些操作,比如,將它設(shè)置到ImageVew上,上傳到網(wǎng)絡(luò)上等。
最后附上項目完整代碼:
package com.jph.cp; ? import java.io.File; import java.io.FileNotFoundException; import android.support.v7.app.ActionBarActivity; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.View; import android.widget.ImageView; ? /** ?* 從相冊選擇照片進(jìn)行裁剪,從相機拍取照片進(jìn)行裁剪<br> ?* 從相冊選擇照片(不裁切),并獲取照片的路徑<br> ?* 拍取照片(不裁切),并獲取照片路徑 ?* @author JPH ?* Date:2014.10.09 ?* last modified:2014.11.04 ?*/ public class MainActivity extends ActionBarActivity { ?? ?/**request Code 從相冊選擇照片并裁切**/ ?? ?private final static int SELECT_PIC=123;? ?? ?/**request Code 從相冊選擇照片不裁切**/ ?? ?private final static int SELECT_ORIGINAL_PIC=126;? ?? ?/**request Code 拍取照片并裁切**/ ?? ?private final static int TAKE_PIC=124;? ?? ?/**request Code 拍取照片不裁切**/ ?? ?private final static int TAKE_ORIGINAL_PIC=127;? ?? ?/**request Code 裁切照片**/ ?? ?private final static int CROP_PIC=125;? ?? ?private Uri imageUri; ?? ?private ImageView imgShow; ?? ?@Override ?? ?protected void onCreate(Bundle savedInstanceState) { ?? ??? ?super.onCreate(savedInstanceState); ?? ??? ?setContentView(R.layout.activity_main); ?? ??? ?//初始化imageUri ?? ??? ?imageUri=Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "test.jpg")); ?? ??? ?imgShow=(ImageView)findViewById(R.id.imgShow); ?? ?} ?? ?@Override ?? ?protected void onActivityResult(int requestCode, int resultCode, Intent data) { ?? ??? ?// TODO Auto-generated method stub ?? ??? ?switch (requestCode) { ?? ??? ?case SELECT_PIC: ?? ??? ??? ?if (resultCode==RESULT_OK) {//從相冊選擇照片并裁切 ?? ??? ??? ??? ?try { ?? ??? ??? ??? ??? ?Bitmap bitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));//將imageUri對象的圖片加載到內(nèi)存 ?? ??? ??? ??? ??? ?imgShow.setImageBitmap(bitmap); ?? ??? ??? ??? ?} catch (Exception e) { ?? ??? ??? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ?break; ?? ??? ?case SELECT_ORIGINAL_PIC: ?? ??? ??? ?if (resultCode==RESULT_OK) {//從相冊選擇照片不裁切 ?? ??? ??? ??? ?try { ?? ??? ??? ??? ??? ?Uri selectedImage = data.getData(); //獲取系統(tǒng)返回的照片的Uri? ?? ??? ??? ??? ??? ?String[] filePathColumn = { MediaStore.Images.Media.DATA }; ? ?? ??? ??? ??? ??? ?Cursor cursor = getContentResolver().query(selectedImage, ? ?? ??? ??? ??? ??? ??? ??? ?filePathColumn, null, null, null);//從系統(tǒng)表中查詢指定Uri對應(yīng)的照片 ?? ??? ??? ??? ??? ?cursor.moveToFirst(); ? ?? ??? ??? ??? ??? ?int columnIndex = cursor.getColumnIndex(filePathColumn[0]);? ?? ??? ??? ??? ??? ?String picturePath = cursor.getString(columnIndex); ?//獲取照片路徑 ?? ??? ??? ??? ??? ?cursor.close(); ? ?? ??? ??? ??? ??? ?Bitmap bitmap= BitmapFactory.decodeFile(picturePath); ?? ??? ??? ??? ??? ?imgShow.setImageBitmap(bitmap); ?? ??? ??? ??? ?} catch (Exception e) { ?? ??? ??? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ?break; ?? ??? ?case TAKE_PIC://拍取照片,并裁切 ?? ??? ??? ?if (resultCode==RESULT_OK) { ?? ??? ??? ??? ?cropImageUri(imageUri, 600, 600, CROP_PIC); ?? ??? ??? ?} ?? ??? ?case TAKE_ORIGINAL_PIC://拍取照片 ?? ??? ??? ?if (resultCode==RESULT_OK) { ?? ??? ??? ??? ?String imgPath=imageUri.getPath();//獲取拍攝照片路徑 ?? ??? ??? ?} ?? ??? ??? ?break; ?? ??? ?case CROP_PIC://拍取照片 ?? ??? ??? ?if (resultCode==RESULT_OK) { ?? ??? ??? ??? ?try { ?? ??? ??? ??? ??? ?Bitmap bitmap=BitmapFactory.decodeStream(getContentResolver(). ?? ??? ??? ??? ??? ??? ??? ?openInputStream(imageUri));//將imageUri對象的圖片加載到內(nèi)存 ?? ??? ??? ??? ??? ?imgShow.setImageBitmap(bitmap); ?? ??? ??? ??? ?} catch (FileNotFoundException e) { ?? ??? ??? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ?break; ?? ??? ?default: ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?super.onActivityResult(requestCode, resultCode, data); ?? ?} ?? ?/** ?? ? * 裁剪指定uri對應(yīng)的照片 ?? ? * @param imageUri:uri對應(yīng)的照片 ?? ? * @param outputX:裁剪寬 ?? ? * @param outputY:裁剪高 ?? ? * @param requestCode:請求碼 ?? ? */ ?? ?private void cropImageUri(Uri imageUri, int outputX, int outputY, int requestCode){ ?? ? ? ?Intent intent = new Intent("com.android.camera.action.CROP"); ?? ? ? ?intent.setDataAndType(imageUri, "image/*"); ?? ? ? ?intent.putExtra("crop", "true"); ?? ? ? ?intent.putExtra("aspectX", 1); ?? ? ? ?intent.putExtra("aspectY", 1); ?? ? ? ?intent.putExtra("outputX", outputX); ?? ? ? ?intent.putExtra("outputY", outputY); ?? ? ? ?intent.putExtra("scale", true); ?? ? ? ?intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); ?? ? ? ?intent.putExtra("return-data", false); ?? ? ? ?intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); ?? ? ? ?intent.putExtra("noFaceDetection", true); // no face detection ?? ? ? ?startActivityForResult(intent, requestCode); ?? ?} ? ?? ?public void cropPic(View view) { ?? ??? ?switch (view.getId()) { ?? ??? ?case R.id.btnCropFromGallery://從相冊選擇照片進(jìn)行裁剪 ?? ??? ??? ?cropFromGallery(); ?? ??? ??? ?break; ?? ??? ?case R.id.btnCropFromTake://從相機拍取照片進(jìn)行裁剪 ?? ??? ??? ?cropFromTake(); ?? ??? ??? ?break; ?? ??? ?case R.id.btnOriginal://從相冊選擇照片不裁切 ?? ??? ??? ?selectFromGallery(); ?? ??? ??? ?break; ?? ??? ?case R.id.btnTakeOriginal://從相機拍取照片不裁剪 ?? ??? ??? ?selectFromTake(); ?? ??? ??? ?break; ? ?? ??? ?default: ?? ??? ??? ?break; ?? ??? ?} ?? ?} ?? ?/** ?? ? * 從相冊選擇原生的照片(不裁切) ?? ? */ ?? ?private void selectFromGallery() { ?? ??? ?// TODO Auto-generated method stub ?? ??? ?Intent intent=new Intent(); ?? ??? ?intent.setAction(Intent.ACTION_PICK);//Pick an item from the data ?? ??? ?intent.setType("image/*");//從所有圖片中進(jìn)行選擇 ?? ??? ?startActivityForResult(intent, SELECT_ORIGINAL_PIC); ? ? ?? ?} ?? ?/** ?? ? * 從相冊選擇照片進(jìn)行裁剪 ?? ? */ ?? ?private void cropFromGallery() { ?? ??? ?// TODO Auto-generated method stub?? ??? ? ?? ??? ?Intent intent=new Intent(); ?? ??? ?intent.setAction(Intent.ACTION_PICK);//Pick an item from the data ?? ??? ?intent.setType("image/*");//從所有圖片中進(jìn)行選擇 ?? ??? ?intent.putExtra("crop", "true");//設(shè)置為裁切 ?? ??? ?intent.putExtra("aspectX", 1);//裁切的寬比例 ?? ??? ?intent.putExtra("aspectY", 1);//裁切的高比例 ?? ??? ?intent.putExtra("outputX", 600);//裁切的寬度 ?? ??? ?intent.putExtra("outputY", 600);//裁切的高度 ?? ??? ?intent.putExtra("scale", true);//支持縮放 ?? ??? ?intent.putExtra("return-data", false); ?? ??? ?intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//將裁切的結(jié)果輸出到指定的Uri ?? ??? ?intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());//裁切成的圖片的格式 ?? ??? ?intent.putExtra("noFaceDetection", true); // no face detection ?? ??? ?startActivityForResult(intent, SELECT_PIC); ? ? ?? ?} ?? ?/** ?? ? * 拍取照片不裁切 ?? ? */ ?? ?private void selectFromTake() { ?? ??? ?// TODO Auto-generated method stub ?? ??? ?Intent intent=new Intent(); ? ?? ??? ?intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//設(shè)置Action為拍照 ? ?? ??? ?intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//將拍取的照片保存到指定URI ? ?? ??? ?startActivityForResult(intent, TAKE_ORIGINAL_PIC); ? ?? ?}?? ? ?? ?/** ?? ? * 從相機拍取照片進(jìn)行裁剪 ?? ? */ ?? ?private void cropFromTake() { ?? ??? ?// TODO Auto-generated method stub ?? ??? ?Intent intent=new Intent(); ? ? ? ? ? intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//設(shè)置Action為拍照 ? ? ? ? ? intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//將拍取的照片保存到指定URI ? ? ? ? ? startActivityForResult(intent, TAKE_PIC); ? ?? ?} }
原文鏈接:https://blog.csdn.net/fengyuzhengfan/article/details/40782313
相關(guān)推薦
- 2022-08-17 R包ggtreeExtra繪制進(jìn)化樹_R語言
- 2022-05-28 Pyinstaller打包Pytorch框架所遇到的問題_python
- 2022-11-07 PostgreSQL常用優(yōu)化技巧示例介紹_PostgreSQL
- 2022-05-24 Python函數(shù)之zip函數(shù)的介紹與實際應(yīng)用_python
- 2022-04-19 Python的閉包和裝飾器你真的了解嗎_python
- 2022-09-22 Python 閉包與裝飾器
- 2022-08-15 創(chuàng)建型設(shè)計模式之建造者模式
- 2022-09-18 70行Python代碼實現(xiàn)一個桌面自動翻譯工具_(dá)python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支