android选择相册图片另存为并在Imageview中显示的相关问题
来源:互联网 发布:sql内连接和外连接 编辑:程序博客网 时间:2024/05/18 09:50
网上有很多选择相册图片并且裁剪的方法,今天我们这里介绍一种不裁减图片,并且使用输入输出流把图片另存为一个文件夹。
直接看代码:
public void saveImage(Uri uri) {// LogUtil.augusLogW("one");// LogUtil.augusLogW(String.valueOf(uri)); File readFile = getFileFromMediaUri(this, uri); if (readFile.exists() && readFile.length() > 0) { try { LogUtil.augusLogW("two"); //读 FileInputStream fis = new FileInputStream(readFile); byte[] buf = new byte[(int) readFile.length()]; fis.read(buf); fis.close(); //写 FileOutputStream fos; switch (type) { case "up": upImage = new File(BaseApplication.CACHE_PATH, "up.png"); fos = new FileOutputStream(upImage); fos.write(buf); fos.close(); if (upImage.exists() && upImage.length() > 0) { isUp = true; } else { Common.staticToast(baseFragActivity, "该图片不存在"); } new BitmapWorkerTask(miv_upImage).execute(BaseApplication.CACHE_PATH + "up.png"); break; case "down": downImage = new File(BaseApplication.CACHE_PATH, "down.png"); fos = new FileOutputStream(downImage); fos.write(buf); fos.close(); if (downImage.exists() && downImage.length() > 0) { isDown = true; } else { Common.staticToast(baseFragActivity, "该图片不存在"); } new BitmapWorkerTask(miv_downImage).execute(BaseApplication.CACHE_PATH + "down.png"); break; case "hand": handImage = new File(BaseApplication.CACHE_PATH, "hand.png"); fos = new FileOutputStream(handImage); fos.write(buf); fos.close(); if (handImage.exists() && handImage.length() > 0) { isHand = true; } else { Common.staticToast(baseFragActivity, "该图片不存在"); } new BitmapWorkerTask(miv_handImage).execute(BaseApplication.CACHE_PATH + "hand.png"); break; } } catch (IOException e) { e.printStackTrace(); } } }相关的类和几个方法:
public static File getFileFromMediaUri(Context ac, Uri uri) { if (uri.getScheme().toString().compareTo("content") == 0) { ContentResolver cr = ac.getContentResolver(); Cursor cursor = cr.query(uri, null, null, null, null);// 根据Uri从数据库中找 if (cursor != null) { cursor.moveToFirst(); String filePath = cursor.getString(cursor.getColumnIndex("_data"));// 获取图片路径 cursor.close(); if (filePath != null) { return new File(filePath); } } } else if (uri.getScheme().toString().compareTo("file") == 0) { return new File(uri.toString().replace("file://", "")); } return null;}public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { if (width > height) { inSampleSize = Math.round((float) height / (float) reqHeight); } else { inSampleSize = Math.round((float) width / (float) reqWidth); } } return inSampleSize;}public static Bitmap decodeSampledBitmapFromFile(String filename, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filename, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(filename, options);}
class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> { private final WeakReference<ImageView> imageViewReference; private String data = null; public BitmapWorkerTask(ImageView imageView) { // Use a WeakReference to ensure the ImageView can be garbage collected imageViewReference = new WeakReference<ImageView>(imageView); } // Decode image in background. @Override protected Bitmap doInBackground(String... params) { data = params[0]; return decodeSampledBitmapFromFile(data, 100, 100); } // Once complete, see if ImageView is still around and set bitmap. @Override protected void onPostExecute(Bitmap bitmap) { if (imageViewReference != null && bitmap != null) { final ImageView imageView = imageViewReference.get(); if (imageView != null) { imageView.setImageBitmap(bitmap); } } }}注意:其中在给imageview设置图片的时候不能直接使用bitmap,因为如果图片过大的话,是显示不出来的,因此显示时要压缩显示。
0 0
- android选择相册图片另存为并在Imageview中显示的相关问题
- Android调用系统相册和相机选择图片并显示在imageview中
- Android开发——相册拍照_02.将拍照得到或相册中选择的图片显示在ImageView中
- Android 拍照并显示在ImageView中(解决图片压缩失真的问题)
- android:ImageView选择本地图片并显示
- Android打开本地相册选取图片并显示在ImageView上
- 在Android中如何获取视频的第一帧图片并显示在一个ImageView中
- 在Android中如何获取视频的第一帧图片并显示在一个ImageView中
- Android 多媒体应用:从相册选择照片显示(解决相册中图片显示不出来的问题)
- Fragment跳转相册并将选中的图片显示在Fragment的ImageView
- Android 拍照并显示在ImageView中
- [Android]Layout中ImageView中图片的对齐显示问题
- android 开启本地相册选择图片并返回显示
- Android 调用系统相册选择图片并显示
- Android 调用系统相册选择图片并显示
- android 开启本地相册选择图片并返回显示
- Android 调用系统相册选择图片并显示
- android 部分手机拍照后在imageView中显示预览图片被旋转的问题
- 机器学习(周志华)习题解答2.1-2.9
- Mbed在自己的stm32系列平台移植适配(一)
- javaBean四个作用域范围
- 修改新建的ionic项目中默认根据id进行的通讯录信息匹配
- 建造者模式dialog
- android选择相册图片另存为并在Imageview中显示的相关问题
- 在Hadoop2.X上搭建Hbase
- AngularJS2 表单
- 物联网模块WIFI的AP和STA的区别
- 常见 jar包详解
- 定时任务 Crontab命令 详解
- [Leetcode] #98 Validate Binary Search Tree
- C盘空间爆满原因之1--CAB开头的临时文件
- sparkMlib逻辑回归分析实现