网上有很多选择相册图片并且裁剪的方法,今天我们这里介绍一种不裁减图片,并且使用输入输出流把图片另存为一个文件夹。
直接看代码:
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,因为如果图片过大的话,是显示不出来的,因此显示时要压缩显示。