日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android大图片裁剪终极解决方案

發(fā)布時間:2025/3/15 Android 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android大图片裁剪终极解决方案 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

根據(jù)我們的分析與總結(jié),圖片的來源有拍照和相冊,而可采取的操作有

  • 使用Bitmap并返回數(shù)據(jù)
  • 使用Uri不返回數(shù)據(jù)

????前面我們了解到,使用Bitmap有可能會導致圖片過大,而不能返回實際大小的圖片,我將采用大圖Uri,小圖Bitmap的數(shù)據(jù)存儲方式。

????我們將要使用到URI來保存拍照后的圖片:

?
1 2 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。

????一、從相冊截大圖:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 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);

????二、從相冊截小圖

?
1 2 3 4 5 6 7 8 9 10 11 12 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ù)

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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; }
?
1 2 3 4 5 6 7 8 9 10 private Bitmap decodeUriAsBitmap(Uri uri){ ????Bitmap bitmap = null; ????try { ????????bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri)); ????} catch (FileNotFoundException e) { ????????e.printStackTrace(); ????????return null; ????} ????return bitmap; }

????效果圖:

大圖小圖

轉(zhuǎn)載(http://my.oschina.net/ryanhoo/blog/86853)

轉(zhuǎn)載于:https://www.cnblogs.com/cimu/p/4319823.html

總結(jié)

以上是生活随笔為你收集整理的Android大图片裁剪终极解决方案的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。