Android:读取本地相册与相机获取图片上传到服务器(用字符串的形式上传)

private Uri imageUri;
//打开照相机
private void openCamera() { 
Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
imageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"image.jpg")); //指定照片保存路径(SD卡),image.jpg为一个临时文件,每次拍照后这个图片都会被替换 openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(openCameraIntent, CAMERA_REQUEST_CODE);
}

//打开图库
private void openPhones() {
    // 图库选择
    // 激活系统图库,选择一张图片
    Intent intent_gallery = new Intent(Intent.ACTION_PICK);
    intent_gallery.setType("image/*");
    startActivityForResult(intent_gallery, IMAGE_REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == IMAGE_REQUEST_CODE) {//相册
        imageUri= data.getData();
        //获取照片路径
        String[] filePathColumn = {MediaStore.Audio.Media.DATA};
        Cursor cursor = getContentResolver().query(imageUri, filePathColumn, null, null, null);
        cursor.moveToFirst();
        photoPath = cursor.getString(cursor.getColumnIndex(filePathColumn[0]));
        cursor.close();
        crop(imageUri);

    } else if (requestCode == CAMERA_REQUEST_CODE) {//相机
        photoPath = Environment.getExternalStorageDirectory() + "/image.jpg";
        crop(imageUri);
    } else if (requestCode == RESULT_REQUEST_CODE) {
        setImageBitmap();
        img_touxiang.setImageBitmap(bitmap);
        bitmapToString = bitmapToString(bitmap);
       //请求服务器的接口,上传到服务器
    }
}

 
/**
 * 剪切图片
 */
private void crop(Uri uri) {
    // 裁剪图片意图
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(uri, "image/*");
    intent.putExtra("crop", "true");
    // 裁剪框的比例,1:1
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    // 裁剪后输出图片的尺寸大小
    intent.putExtra("outputX", 250);
    intent.putExtra("outputY", 250);
    // 图片格式
    intent.putExtra("outputFormat", "PNG");
    intent.putExtra("noFaceDetection", true);// 取消人脸识别
    intent.putExtra("return-data", true);// true:不返回uri,false:返回uri
    startActivityForResult(intent, RESULT_REQUEST_CODE);//同样的在onActivityResult中处理剪裁好的图片
}

/**
 * 压缩图片
 */
private void setImageBitmap() {
    //获取imageview的宽和高
    int targetWidth = img_touxiang.getWidth();
    int targetHeight = img_touxiang.getHeight();

    //根据图片路径,获取bitmap的宽和高
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(photoPath, options);
    int photoWidth = options.outWidth;
    int photoHeight = options.outHeight;

    //获取缩放比例
    int inSampleSize = 1;
    if (photoWidth > targetWidth || photoHeight > targetHeight) {
        int widthRatio = Math.round((float) photoWidth / targetWidth);
        int heightRatio = Math.round((float) photoHeight / targetHeight);
        inSampleSize = Math.min(widthRatio, heightRatio);
    }

    //使用现在的options获取Bitmap
    options.inSampleSize = inSampleSize;
    options.inJustDecodeBounds = false;
    bitmap = BitmapFactory.decodeFile(photoPath, options);
    img_touxiang.setImageBitmap(bitmap);
}

//把bitmap转换成字符串
public static String bitmapToString(Bitmap bitmap) {
    String string = null;
    ByteArrayOutputStream btString = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, btString);
    byte[] bytes = btString.toByteArray();
    string = Base64.encodeToString(bytes, Base64.DEFAULT);
    return string;
}

 
 
 

 

你可能感兴趣的:(android)