android选择相册图片另存为并在Imageview中显示的相关问题

网上有很多选择相册图片并且裁剪的方法,今天我们这里介绍一种不裁减图片,并且使用输入输出流把图片另存为一个文件夹。

直接看代码:

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值