android之图片截取
16lz
2021-01-26
本文主要记录一些零碎的东西
android上的相机是经常需要用到的好东西,比如用户头像啦这些,一开始我没有截取部分,而是直接上传,后来获取图片时给我报了这样一个错:
java.lang.OutOfMemoryError: Failed to allocate a 14745612 byte allocation with 12949099free bytes and 12MB until OOM
内存不足呀,看来需要在上传图片时就截取部分上传控制图片的大小。
主要思路就是打开系统相册,截取图片,显示,这里都是在activity里调用startActivityForResult完成
先打开相册:
private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择private static final int PHOTO_REQUEST_CUT = 3;// 结果Intent intent = new Intent(Intent.ACTION_PICK);intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");startActivityForResult(intent, PHOTO_REQUEST_GALLERY);再 看看回调函数,使用系统的com.android.camera.action.CROP进行图片截取
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if ( resultCode == Activity.RESULT_OK) { switch (requestCode){ case PHOTO_REQUEST_GALLERY://从相册中选择 //截取图片 startPhotoZoom(data.getData(), 150); break; case PHOTO_REQUEST_CUT://截取的结果 Bitmap photo = null; //这里只会在图片没有截取时执行,默认是需要截取图片的 Uri photoUri = data.getData();// Log.i("slack","photoUri:"+photoUri); if (photoUri != null) { photo = BitmapFactory.decodeFile(photoUri.getPath()); } // end if (photo == null) { Bundle extra = data.getExtras(); if (extra != null) { photo = (Bitmap)extra.get("data"); ByteArrayOutputStream stream = new ByteArrayOutputStream(); photo.compress(Bitmap.CompressFormat.JPEG, 100, stream); } } showPhoto(photo); break; }}private void startPhotoZoom(Uri uri, int size) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // crop为true是设置在开启的intent中设置显示的view可以剪裁 intent.putExtra("crop", "true"); // aspectX aspectY 是宽高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX,outputY 是剪裁图片的宽高 intent.putExtra("outputX", size); intent.putExtra("outputY", size); intent.putExtra("outputFormat", "JPEG"); intent.putExtra("noFaceDetection", true); intent.putExtra("return-data", true); startActivityForResult(intent, PHOTO_REQUEST_CUT); } private void showPhoto(Bitmap bitmap) { userPic.setImageBitmap(bitmap); }关键是下面这段代码:
Bundle extra = data.getExtras(); if (extra != null) { photo = (Bitmap)extra.get("data"); ByteArrayOutputStream stream = new ByteArrayOutputStream(); photo.compress(Bitmap.CompressFormat.JPEG, 100, stream); }上面就可以实现图片截取了,在小米和魅族上亲测有效
现在的图片格式是Bitmap的,上传时可以转成文件,或者直接转成byte字节数字
/** * Bitmap transfer to bytes * 位图转移到字节 * @param byteArray * @return */public static byte[] bitmapToBytes(Bitmap bm) {byte[] bytes = null;if (bm != null) {ByteArrayOutputStream baos = new ByteArrayOutputStream();bm.compress(Bitmap.CompressFormat.PNG, 100, baos);bytes = baos.toByteArray();}return bytes;}
/** * Check the SD card * @return * 检查SD卡 */public static boolean checkSDCardAvailable(){return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);}/** * Save image to the SD card 将图像保存到SD卡 * @param photoBitmap * @param photoName * @param path */public static void savePhotoToSDCard(Bitmap photoBitmap,String path,String photoName){if (checkSDCardAvailable()) {File dir = new File(path);if (!dir.exists()){dir.mkdirs();}File photoFile = new File(path , photoName + ".png");FileOutputStream fileOutputStream = null;try {fileOutputStream = new FileOutputStream(photoFile);if (photoBitmap != null) {if (photoBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)) {fileOutputStream.flush();//fileOutputStream.close();}}} catch (FileNotFoundException e) {photoFile.delete();e.printStackTrace();} catch (IOException e) {photoFile.delete();e.printStackTrace();} finally{try {fileOutputStream.close();} catch (IOException e) {e.printStackTrace();}}} }/** * Delete the image from SD card从SD卡删除的图像 * @param context * @param path * file:///sdcard/temp.jpg */public static void deleteAllPhoto(String path){if (checkSDCardAvailable()) {File folder = new File(path);File[] files = folder.listFiles();for (int i = 0; i < files.length; i++) {files[i].delete();}}}public static void deletePhotoAtPathAndName(String path,String fileName){if (checkSDCardAvailable()) {File folder = new File(path);File[] files = folder.listFiles();for (int i = 0; i < files.length; i++) {System.out.println(files[i].getName());if (files[i].getName().equals(fileName)) {files[i].delete();}}}}
更多相关文章
- ArrayList动态删除 自定义Adapter (附源码)
- Android(安卓)Gallery 的简单使用
- Android:TextView显示富文本信息
- Android(安卓)登陆页面 图片验证码
- Android好用的okhttp网络封装(get,post,上传,下载)
- java获取http:图片下载代码——android基础编
- react-native 修改app应用名称
- android实用技巧:android实现listview异步加载图片
- Android(安卓)客户端通过HTTP POST发布图片和文字源代码