从相册、启动相机选择图片上传

入口处 :


private void changeAvatar() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("选择照片");
        builder.setPositiveButton("相机", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
// Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");  Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File file = new File(Environment.getExternalStorageDirectory(), "workupload.jpg");
                Uri imageUri = Uri.fromFile(file);
//指定照片保存路径(SD卡),workupload.jpg为一个临时文件,每次拍照后这个图片都会被替换  cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(cameraIntent, 0);
                file.delete();
            }
        });
        builder.setNegativeButton("相册", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                startActivityForResult(new Intent(CreateHelpActivity.this, ImagesPickerActivity.class)
                        .putExtra("data", selectedImages)
                        .putExtra("count_limit", 9)
                        , 100);
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }



在OnActivityResult中  :



if (requestCode == 0) {
    Bitmap camorabitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/workupload.jpg");
    if (null != camorabitmap) {
        // 下面这两句是对图片按照一定的比例缩放,这样就可以完美地显示出来。  int scale = reckonThumbnail(camorabitmap.getWidth(), camorabitmap.getHeight(), 500, 600);
        bitmap = PicZoom(camorabitmap, camorabitmap.getWidth() / scale, camorabitmap.getHeight() / scale);
        //由于Bitmap内存占用较大,这里需要回收内存,否则会报out of memory异常  camorabitmap.recycle();
        //将处理过的图片显示在界面上,并保存到本地  try {
            String photoLocalPath = savaPhotoToLocal(data, bitmap);
            selectedImages.add(photoLocalPath);
            adapter.replaceAll(selectedImages);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}


其中三个封装的方法为 :



public int reckonThumbnail(int oldWidth, int oldHeight, int newWidth, int newHeight) {
        if ((oldHeight > newHeight && oldWidth > newWidth)
                || (oldHeight <= newHeight && oldWidth > newWidth)) {
            int be = (int) (oldWidth / (float) newWidth);
            if (be <= 1)
                be = 1;
            return be;
        } else if (oldHeight > newHeight && oldWidth <= newWidth) {
            int be = (int) (oldHeight / (float) newHeight);
            if (be <= 1)
                be = 1;
            return be;
        }
        return 1;
    }

    public Bitmap PicZoom(Bitmap bmp, int width, int height) {
        int bmpWidth = bmp.getWidth();
        int bmpHeght = bmp.getHeight();
        Matrix matrix = new Matrix();
        matrix.postScale((float) width / bmpWidth, (float) height / bmpHeght);

        return Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeght, matrix, true);
    }


    public String savaPhotoToLocal(Intent data, Bitmap btp) throws FileNotFoundException {
        // 如果文件夹不存在则创建文件夹,并将bitmap图像文件保存  String saveDir = Environment.getExternalStorageDirectory() + "/meitian_photos";
        // 新建目录  File dir = new File(saveDir);
        if (!dir.exists()) dir.mkdir();
        // 生成文件名  SimpleDateFormat t = new SimpleDateFormat("yyyyMMddssSSS");
        String filename = "MT" + (t.format(new Date())) + ".jpg";
        // 新建文件  File file = new File(saveDir, filename);
        // 打开文件输出流  FileOutputStream fileOutputStream = new FileOutputStream(file);
        // 生成图片文件 // bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);  // 相片的完整路径  String filePath = file.getPath();
        try {
            // bitmap转为jpg文件保存  FileOutputStream fileOut = new FileOutputStream(file);
            btp.compress(Bitmap.CompressFormat.JPEG, 100, fileOut);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return filePath;
    }

其中有个小Bug就是启动相机之后 再直接返回 不拍照,list里还会添加上次文件保存的图片。 所以在每次启动之后删除之前的文件。

file.delete();


已此记录。   2015 - 10 - 12


你可能感兴趣的:(从相册、启动相机选择图片上传)