裁剪图像并将其保存到mysqldatabse中

时间:2017-05-09 07:02:47

标签: android database android-volley

我能够捕获图像并且还能够裁剪图像。但是当我上传图像时,我无法保存裁剪的图像。而不是正在上传完整图像,我想上传裁剪的图像只有...

public class imageuploadtest extends AppCompatActivity implements View.OnClickListener {
private ImageView Imageview;
EditText Name;
private Button Upload, Capture,camera;
private final int IMG_REQUEST = 1;
public Bitmap bitmap;
ImageView imageView;
Button buttonCamera, buttonGallery, submit;
File file;
Uri uri;
EditText date;
Intent CamIntent, GalIntent, CropIntent;
public static final int RequestPermissionCode = 1;
DisplayMetrics displayMetrics;
int width, height;
String server_url = "http://10.0.0.01/adcube/imageupload.php";

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.imageupload);
    Imageview = (ImageView) findViewById(R.id.imageView);
    Upload = (Button) findViewById(R.id.upload);
    Capture = (Button) findViewById(R.id.capture);
    Name = (EditText) findViewById(R.id.name);
    camera= (Button) findViewById(R.id.camera);
    Upload.setOnClickListener(this);
    Capture.setOnClickListener(this);
    camera.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.upload:
            selectimage();
            break;
        case R.id.capture:

            uploadImage();
            break;
        case R.id.camera:
            ClickImageFromCamera();
    }
}

public void ClickImageFromCamera() {

    CamIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

    file = new File(Environment.getExternalStorageDirectory(),
            "file" + String.valueOf(System.currentTimeMillis()) + ".jpg");
    uri = Uri.fromFile(file);

    CamIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);

    CamIntent.putExtra("return-data", true);

    startActivityForResult(CamIntent, 0);

}
private void selectimage() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(intent, IMG_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 0 && resultCode == RESULT_OK) {
        ImageCropFunction();

    } else if (requestCode == 2) {

        if (data != null) {

            uri = data.getData();

            ImageCropFunction();
        }
    } else if (requestCode == 1) {

        if (data != null) {

ImageCropFunction();
                try {
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                Bundle bundle = data.getExtras();

                Bitmap bitmap =bundle.getParcelable("data");
                Imageview.setImageBitmap(bitmap);
                Imageview.setVisibility(View.VISIBLE);
                Name.setVisibility(View.VISIBLE);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}



private void uploadImage() {
    StringRequest stringrequest = new StringRequest(Request.Method.POST, server_url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonObject = new JSONObject(response);
                String Response = jsonObject.getString("cropIntent");
                Toast.makeText(imageuploadtest.this, Response, Toast.LENGTH_SHORT).show();
                Imageview.setImageResource(0);
                Imageview.setVisibility(View.GONE);
                Name.setText("");
                Name.setVisibility(View.GONE);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            Toast.makeText(imageuploadtest.this, "successful", Toast.LENGTH_SHORT).show();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(imageuploadtest.this, error.toString(), Toast.LENGTH_SHORT).show();
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> parms = new HashMap<>();
            parms.put("name", Name.getText().toString().trim());
            parms.put("image", imageToString(bitmap));

            return parms;
        }
    };
    Mysingleton.getInstance(imageuploadtest.this).addToRequest(stringrequest);
}
public void ImageCropFunction() {

    // Image Crop Code
    try {
        CropIntent = new Intent("com.android.camera.action.CROP");

        CropIntent.setDataAndType(uri, "image/*");

        CropIntent.putExtra("crop", "true");
        CropIntent.putExtra("outputX", 180);
        CropIntent.putExtra("outputY", 180);
        CropIntent.putExtra("aspectX", 3);
        CropIntent.putExtra("aspectY", 4);

        CropIntent.putExtra("scaleUpIfNeeded", true);
        CropIntent.putExtra("return-data", true);

        startActivityForResult(CropIntent, 1);

    } catch (ActivityNotFoundException e) {

    }
}


//cconvert image to string for database

public static String imageToString(Bitmap BitmapData) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    BitmapData.compress(Bitmap.CompressFormat.JPEG, 100, bos);
    byte[] byte_arr = bos.toByteArray();
    String encoded = Base64.encodeToString(byte_arr, Base64.DEFAULT); //appendLog(file);
    return encoded;
  }
}

1 个答案:

答案 0 :(得分:0)

onActivityResult 应该是这样的

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0 && resultCode == RESULT_OK) {
    ImageCropFunction();

} else if (requestCode == 2) {

    if (data != null) {

        uri = data.getData();

        ImageCropFunction();
    }
} else if (requestCode == 1) {

    if (data != null) {
ImageCropFunction(); 
try {

            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
            Bundle bundle = data.getExtras();

            bitmap =bundle.getParcelable("data");
            Imageview.setImageBitmap(bitmap);
            Imageview.setVisibility(View.VISIBLE);
            Name.setVisibility(View.VISIBLE);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我刚删除了一个额外的位图

Bitmap bitmap =bundle.getParcelable("data");

应该只是这样,

bitmap =bundle.getParcelable("data");