Android實現(xiàn)從相冊截圖的功能
在這篇文章中,我將向大家展示如何從相冊截圖。
先看看效果圖:
上一篇文章中,我就拍照截圖這一需求進行了詳細的分析,試圖讓大家了解Android本身的限制,以及我們應當采取的實現(xiàn)方案。大家可以回顧一下:Android實現(xiàn)拍照截圖功能
根據(jù)我們的分析與總結,圖片的來源有拍照和相冊,而可采取的操作有
- 使用Bitmap并返回數(shù)據(jù)
- 使用Uri不返回數(shù)據(jù)
前面我們了解到,使用Bitmap有可能會導致圖片過大,而不能返回實際大小的圖片,我將采用大圖Uri,小圖Bitmap的數(shù)據(jù)存儲方式。
我們將要使用到URI來保存拍照后的圖片:
private static final String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg";//temp file Uri imageUri = Uri.parse(IMAGE_FILE_LOCATION);//The Uri to store the big bitmap
不難知道,我們從相冊選取圖片的Action為Intent.ACTION_GET_CONTENT。
根據(jù)我們上一篇博客的分析,我準備好了兩個實例的Intent。
一、從相冊截大圖:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null); intent.setType("image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 2); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 600); intent.putExtra("outputY", 300); intent.putExtra("scale", true); intent.putExtra("return-data", false); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, CHOOSE_BIG_PICTURE);
二、從相冊截小圖
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null); intent.setType("image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 2); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 200); intent.putExtra("outputY", 100); intent.putExtra("scale", true); intent.putExtra("return-data", true); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, CHOOSE_SMALL_PICTURE);
三、對應的onActivityResult可以這樣處理返回的數(shù)據(jù)
switch (requestCode) { case CHOOSE_BIG_PICTURE: Log.d(TAG, "CHOOSE_BIG_PICTURE: data = " + data);//it seems to be null if(imageUri != null){ Bitmap bitmap = decodeUriAsBitmap(imageUri);//decode bitmap imageView.setImageBitmap(bitmap); } break; case CHOOSE_SMALL_PICTURE: if(data != null){ Bitmap bitmap = data.getParcelableExtra("data"); imageView.setImageBitmap(bitmap); }else{ Log.e(TAG, "CHOOSE_SMALL_PICTURE: data = " + data); } break; default: break; } private Bitmap decodeUriAsBitmap(Uri uri){ Bitmap bitmap = null; try { bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri)); } catch (FileNotFoundException e) { e.printStackTrace(); return null; } return bitmap; }
以上就是Android實現(xiàn)拍照截圖功能的方法,希望對大家的學習有所幫助。
- Native.js獲取監(jiān)聽開關等操作Android藍牙設備實例代碼
- native.js獲取手機硬件基本信息實例代碼android版
- Dcloud的native.js直接撥打電話Android實例代碼
- DCloud的native.js調用系統(tǒng)分享實例Android版代碼
- Android中通過view方式獲取當前Activity的屏幕截圖實現(xiàn)方法
- Android中如何獲取視頻文件的截圖、縮略圖
- Android模擬器中窗口截圖存成文件實現(xiàn)思路及代碼
- 詳解有關Android截圖與錄屏功能的學習
- Android實現(xiàn)截圖和分享功能的代碼
- Android獲取常用輔助方法(獲取屏幕高度、寬度、密度、通知欄高度、截圖)
- Android實現(xiàn)拍照截圖功能
- android截圖事件監(jiān)聽的原理與實現(xiàn)
- Android屏幕及view的截圖實例詳解
- Android截屏截圖的幾種方法總結
- Android實現(xiàn)截圖分享qq 微信功能
- Android 中WebView 截圖的實現(xiàn)方式
- Android App內監(jiān)聽截圖加二維碼功能代碼
- Android 5.0及以上編程實現(xiàn)屏幕截圖功能的方法
- Android仿銀行客戶簽名并且保存簽名的截圖文件并命名為本地時間
- Android 截圖功能源碼的分析
- Android使用WebView實現(xiàn)截圖分享功能
- Native.js屏幕截圖實例代碼
相關文章
android開發(fā)教程之android的handler使用方法
這篇文章主要介紹了android的handler使用方法,大家參考使用吧2014-01-01Android user版通過adb_enable開啟adb 調試 不提示對話框的流程分析
這篇文章主要介紹了Android user版通過adb_enable開啟adb 調試 不提示對話框的流程分析,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05