Create a full screen dialog with CropImageView

To create a fullscreen dialog with CropImageView in Sketchware, follow the steps given below.

1. Add an ImageView imageview1 for displaying image.

2. Add a FilePicker component fp: image/* for picking images.

3. In imageview1 onClick event use block FilePicker fp pick files.

4. Download CropUtils.zip from this url:

http://apktutor.com/wp-content/uploads/2021/11/CropUtils.zip

5. Extract CropUtils.java at .sketchware/mysc/(Project number)/app/src/main/java/(package name)/CropUtils.java

6. Open CropUtils.java and change package name (package com.my.dmchat;) to your project's package name.

7. Create a more block extra. Here declare a CropImageView crp, a Bitmap image and define rotateBitmap(Bitmap).

}

CropUtils.CropImageView crp;

Bitmap image;

public Bitmap rotateBitmap(Bitmap bitmap){

android.graphics.Matrix matrix = new android.graphics.Matrix();

matrix.postScale((float)1, (float)1);

matrix.postRotate(90);

Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

return bitmap2;

}

{

8. Create a Custom View crop_dialog.xml, with an ImageView close, a LinearLayout crop_linear, a TextView rotate, and a TextView crop.



9.  In FilePicker onFilesPicked event put following codes:

Uri uri = null;

if (_data.getClipData() != null){

uri = _data.getClipData().getItemAt(0).getUri();

} else {

uri = _data.getData();

}

crp = new CropUtils.CropImageView(ProfileActivity.this);

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

lp.setMargins(0, 0, 0, 0);

crp.setLayoutParams(lp);

crp.setScaleType(ImageView.ScaleType.FIT_CENTER);

crp.setAdjustViewBounds(true);

crp.setFixedAspectRatio(true);

crp.setAspectRatio(1, 1);

try {

ParcelFileDescriptor parcelFD = getContentResolver().openFileDescriptor(uri, "r");

java.io.FileDescriptor fileDescriptor = parcelFD.getFileDescriptor();

image = BitmapFactory.decodeFileDescriptor(fileDescriptor);

parcelFD.close();

crp.setImageBitmap(image);

} catch (java.io.IOException e) {

showMessage(e.toString());

}

final Dialog dialog2 = new Dialog(ProfileActivity.this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);

dialog2.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

dialog2.setContentView(R.layout.crop_dialog);

ImageView close_dialog = dialog2.findViewById(R.id.close);

LinearLayout crop_linear = dialog2.findViewById(R.id.crop_linear);

TextView rotate = dialog2.findViewById(R.id.rotate);

TextView crop = dialog2.findViewById(R.id.crop);

crop_linear.addView(crp);

close_dialog.setOnClickListener(new View.OnClickListener(){

@Override

public void onClick(View v){

dialog2.dismiss();

}

});

rotate.setOnClickListener(new View.OnClickListener(){

@Override

public void onClick(View v){

image = rotateBitmap(image);

crp.setImageBitmap(image);

}

});

crop.setOnClickListener(new View.OnClickListener(){

@Override

public void onClick(View v){

Bitmap bitmap = crp.getCroppedImage();

imageview1.setImageBitmap(bitmap);

dialog2.dismiss();
}
});

dialog2.show();

10. Save and run the project.



Code for uploading Bitmap image to Firebase:

Bitmap bitmap = crp.getCroppedImage();

java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

byte[] data = baos.toByteArray();

String filename = FirebaseAuth.getInstance().getCurrentUser().getUid() + ".jpg";

fstore.child(filename).putBytes(data).addOnFailureListener(_fstore_failure_listener).addOnProgressListener(_fstore_upload_progress_listener).continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {

@Override

public Task<Uri> then(Task<UploadTask.TaskSnapshot> task) throws Exception {

return fstore.child(filename).getDownloadUrl();

}}).addOnCompleteListener(_fstore_upload_success_listener);

Code for retrieving profile data to a Map variable user_map (Here user_id is FirebaseAuth uid of the user, and user_map is a Map variable):

_firebase.getReference().child("users").child(user_id)

.addValueEventListener(new ValueEventListener() {

@Override

public void onDataChange(DataSnapshot _param1) {

GenericTypeIndicator < HashMap< String, Object>> _ind = new GenericTypeIndicator<HashMap< String, Object>>() {};

try {

user_map = _param1.getValue(_ind);

//Get keys of user_map and use them to display data in TextViews or ImageViews

} catch (Exception e) {

showMessage(e.toString());

}

}

@Override

public void onCancelled(DatabaseError databaseError) {

}

});



Comments

Popular posts from this blog

A Flash Light App in Sketchware

Firebase auth in Sketchware for Login Activity

Simple car racing android game in Sketchware

Easiest way to create an App for your website

Create a PDF reader android App in Sketchware