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

  1. Help Plz. My phon can't run my app any more
    when i try to run app this error show to me.
    /storage/emulated/0/.sketchware/mysc/604/app/src/main/res/values/styles.xml:2: Error retriving parent for item: No resource found that matches the given name '@android:style/Theme.Material.Light.DarkActionBbar'.,,/
    ...
    ...
    and so on
    can you help me plz.

    ReplyDelete
  2. I want to know how to create an appwidget with sketchware. Help me please

    ReplyDelete
  3. Show de informação cara parabéns.

    ReplyDelete

Post a Comment

Popular posts from this blog

Simple car racing android game in Sketchware

Creating a Drawing View in Sketchware

Enable Fullscreen for Youtube videos in WebView

How to enable upload from webview in Sketchware?

List of Calendar Format symbols valid in Sketchware