Hi guys please help me with this. i want to crop image which is captured from camera and save it to memory. but am getting could'nt load image error. can anyone please check this code and give me an idea where am going wrong.
Code:
private void launchcamera() {
Intent intent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE );
startActivityForResult( intent, REQUEST_IMAGE_CAPTURE );
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
//open camera
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get( "data" );
performcrop();
long height = imageBitmap.getHeight();
long width = imageBitmap.getWidth();
long filesizeinBytes1 = imageBitmap.getByteCount();
long fileSizeInKB1 = filesizeinBytes1 / 1024;
Log.d( TAG, "Textview message :"+ height + " , " + width + " , " + fileSizeInKB1 );
System.out.print( fileSizeInKB1 );
if (fileSizeInKB1 >= 500) {
Bitmap convertedimage = getResizedBitmap( imageBitmap, 400 );
savefile( convertedimage );
} else {
savefile( imageBitmap );
}
Intent intent = new Intent( ScancodeActivity.this, Display.class );
startActivity( intent );
}
}
private void performcrop(){
try {
Intent cropintent = new Intent( "com.android.camera.action.CROP" );
cropintent.setDataAndType( picuri, "image/*" );
cropintent.putExtra( "crop", "true" );
cropintent.putExtra( "aspectX", 1 );
cropintent.putExtra( "aspectY", 1 );
cropintent.putExtra( "outputX", 256 );
cropintent.putExtra( "outputY", 256 );
cropintent.putExtra( "returndata", true );
startActivityForResult( cropintent, PIC_CROP );
}catch(ActivityNotFoundException anfe){
String errormessage="Sorry your device doesn't support the crop action";
Toast toast= Toast.makeText( this,errormessage,Toast.LENGTH_LONG );
toast.show();
}
}
private void savefile(Bitmap image) {
File destination = new File( Environment.getExternalStorageDirectory(), "capture_01.bmp" );
picuri= Uri.fromFile( destination );
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
image.compress( Bitmap.CompressFormat.JPEG, 100, bytes );
long filesizeinBytes = image.getByteCount();
long fileSizeInKB = filesizeinBytes / 1024;
FileOutputStream fo;
try {
fo = new FileOutputStream( destination );
fo.write( bytes.toByteArray() );
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
}