android选择相册图片另存为并在Imageview中显示的相关问题
2017-02-15 15:02
519 查看
网上有很多选择相册图片并且裁剪的方法,今天我们这里介绍一种不裁减图片,并且使用输入输出流把图片另存为一个文件夹。
直接看代码:
相关的类和几个方法:
注意:其中在给imageview设置图片的时候不能直接使用bitmap,因为如果图片过大的话,是显示不出来的,因此显示时要压缩显示。
直接看代码:
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,因为如果图片过大的话,是显示不出来的,因此显示时要压缩显示。
相关文章推荐
- Android调用系统相册和相机选择图片并显示在imageview中
- Android开发——相册拍照_02.将拍照得到或相册中选择的图片显示在ImageView中
- 【Android 问题集锦】关于android ImageView前景图片显示很小的问题
- Android中ImageView实现选择本地图片并显示功能
- Android ImageView 不显示JPEG图片的问题解决
- Android自定义控件:imageview重写onMeasure方法实现图片按指定比例显示,拉伸永不变形,解决屏幕适配问题
- Android 多媒体应用:从相册选择照片显示(解决相册中图片显示不出来的问题)
- 安卓学习笔记(一)Android ImageButton、ImageView控件属性设置 图片显示问题
- Android在打开一个新的Activity浏览大图时使用ImageView/PhotoView的时候图片显示在屏幕上部的问题
- android:ImageView选择本地图片并显示
- android 部分手机拍照后在imageView中显示预览图片被旋转的问题
- Android-ImageView显示选择本地图片或拍照
- [Android]Layout中ImageView中图片的对齐显示问题
- android-ImageView显示选择本地图片或拍照
- 关于真机测试的android选择图片在ImageView上显示,调用setImageBitmap无法显示和闪退、控件消失
- Android-ImageView显示选择本地图片或拍照
- Android打开本地相册选取图片并显示在ImageView上
- 关于Android ImageView 图片显示的几个问题
- android 自定义ImageView -图片显示相关1
- android用ImageView显示网络图片