android通过手机拍照或本地相册上传清晰的图片

首先是调出相机拍照的功能,我是先将该图片存储在本地中

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 调用android自带的照相机
                // f_c_image.jpg 表示发美丽的照相图片。
                Uri imageUri = Uri.fromFile(new File(getImageCachePath(),
                        "f_c_image.jpg"));
                //
                // intent.putExtra("crop", "true"); // 出现裁剪窗口。
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                //
                startActivityForResult(intent, 0);

public String getImageCachePath()//给图片一个存储路径
    {
        if (!isExistSDCard())
        {
            return "";
        }
        
        String sdRoot = Environment.getExternalStorageDirectory().getAbsolutePath();
        String result = sdRoot +
                "/" + MAIN_SOFT_FOLDER_NAME + "/" + CACHE_FOLDER_NAME;
        if (new File(result).exists() && new File(result).isDirectory())
        {
            return result;
        }
        else {
            return sdRoot;
        }
    }
    

另一种则是在本地相册中调出图片

Uri imageUri = Uri.fromFile(new File(getImageCachePath(),
                        "f_c_image.jpg"));

                Intent openAlbumIntent = new Intent(Intent.ACTION_GET_CONTENT);
                openAlbumIntent.setType("image/*");
                openAlbumIntent.putExtra("crop", "true");
                // openAlbumIntent.putExtra("aspectX", 1);
                // openAlbumIntent.putExtra("aspectY", 1);
                // openAlbumIntent.putExtra("outputX", 240);
                // openAlbumIntent.putExtra("outputY", 240);
                // openAlbumIntent.putExtra("return-data", true);

                // lixi 直接从图库选择图片。
                openAlbumIntent.putExtra("output", imageUri);
                openAlbumIntent.putExtra("outputFormat", "JPEG");

                startActivityForResult(openAlbumIntent, 1);

所有对取到的图片的处理都是在android的下面这个方法中进行,现在已经将图片保存于本地,下面方法的功能是将该图片放在另外一个activity显示

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == -1) {

            Intent intent = new Intent();
            intent.setClass(getActivity(), AddNewPictureActivity.class);
            Bundle bundle = new Bundle();
            bundle.putInt("FromWhere", FromWhere);

            switch (requestCode) {

            case TAKE_PICTURE:
               
                ImageTools.ClearNewTempImages(this.getActivity());
                // 判断图片还在不在?
                String sdPath = FileUtil.getImageCachePath();
                if (!ImageTools.findPhotoFromSDCard(sdPath, "f_c_image")) {
                    //  拍照的图片没有保存到本地目录下。
                    // TODO:这里要提示一个文言。
                    Toast.makeText(getActivity(), "拍照失败!", Toast.LENGTH_SHORT)
                            .show();
                    
                    
                    //ImageTools.deleteLatestPhoto(this.getActivity());
                    return;

                }
                
                //ImageTools.deleteLatestPhoto(this.getActivity());
                // 会莫名其妙的启动两次?
                // 启动图片预览和保存的界面。
                bundle.putInt("CurrentPage", TAKE_PICTURE);

                intent.putExtras(bundle);
                intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(intent);

             

                break;

            case CHOOSE_PICTURE:
                // Bitmap smallBitmap = (Bitmap) data.getExtras().get("data");
                if (!ShowToastUtil.showNoSDCToast(getActivity()))
                {
                    return;
                }

                
                String sdPath1 = FileUtil.getImageCachePath();
                if (!ImageTools.findPhotoFromSDCard(sdPath1, "f_c_image")) {
                    // 选择的图片没有保存到本地目录下。
                    // TODO:这里要提示一个文言。
                    Toast.makeText(getActivity(), "取图失败!", Toast.LENGTH_SHORT)
                            .show();
                    return;

                }

                bundle.putInt("CurrentPage", CHOOSE_PICTURE);
                intent.putExtras(bundle);

                intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(intent);

               
                break;

            default:
                break;
            }
        }
    }

 在另一个activity中获得Bitmap格式的文件显示,在文件中使用的destW和destH都是800,fixRotating为true(其实有点不明白destW和destH是干什么用的)

public static Bitmap decodePhotoFromSDCard(String path, String photoName, int destW, int destH, boolean fixRotating)
    {
        String imgpath = path + "/" +photoName +".jpg";
        InputStream is = null;
        InputStream is2 = null;
        try {
            is = new FileInputStream(imgpath);
            int heightRatio = 1;
            int widthRatio = 1;
            BitmapFactory.Options opts=new BitmapFactory.Options();
            // 先解析图片的边缘获取它的尺寸。
            opts.inJustDecodeBounds = true;
            Bitmap temp =BitmapFactory.decodeStream(is,null, opts); 
            
            is.close();
            is = null;
            
            heightRatio = (int)Math.ceil(opts.outHeight/(float)destH);  
            widthRatio = (int)Math.ceil(opts.outWidth/(float)destW);
            
            temp = null;
            
            
            
            //2.为位图设置100K的缓存
            opts.inTempStorage = new byte[100 * 1024];
            //3.设置位图颜色显示优化方式
            //ALPHA_8:每个像素占用1byte内存(8位)
            //ARGB_4444:每个像素占用2byte内存(16位)
            //ARGB_8888:每个像素占用4byte内存(32位)
            //RGB_565:每个像素占用2byte内存(16位)
            opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
            //4.设置图片可以被回收,创建Bitmap用于存储Pixel的内存空间在系统内存不足时可以被回收
            opts.inPurgeable = true;
            
            if (heightRatio > 1 && widthRatio > 1)  
            {  
                int tempRatio = heightRatio > widthRatio ? heightRatio:widthRatio; 
                opts.inSampleSize = tempRatio / 2 *2;  // 保证不出错,使用2的倍数来。
            } 
            
            opts.inJustDecodeBounds = false;  
            
            //6.设置解码位图的尺寸信息
            opts.inInputShareable = true; 
            
            //7.解码位图
            is2 = new FileInputStream(imgpath);
            Bitmap result1 =BitmapFactory.decodeStream(is2,null, opts);    
            
            // 解决图片拍照后旋转的问题。
            if (fixRotating)
            {
                int degree = ImageTools.readPictureDegree(imgpath);
                Bitmap result = ImageTools.rotaingImageView(degree, result1);
                
                return result;
            }
            else
            {
                return result1;
            }
            
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return null;
        }
        finally
        {
            try {
                if (is != null)
                {
                    is.close();
                }
                if (is2 != null)
                {
                    is2.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
        
    }

这是我写的第一篇博客,写的目的是希望自己能坚持将项目中遇到问题的解决方法记录下来。

转载于:https://www.cnblogs.com/xuexue-bit/p/3551706.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值