burhanrashid52 / PhotoEditor

A Photo Editor library with simple, easy support for image editing using paints,text,filters,emoji and Sticker like stories.
MIT License
4.12k stars 989 forks source link

Text editor not showing text, no saving in Android 10 #403

Closed kccodeguy closed 2 years ago

kccodeguy commented 2 years ago

I tried the app on a Samsung Galaxy A6 with Android 10 and text editor does not render text during typing. It also fails when saving.

Kahorotech commented 2 years ago

Facing the same issue here

burhanrashid52 commented 2 years ago

@kccodeguy Any stack trace ?

Kahorotech commented 2 years ago

Saying failed to save image in the stack trace nothing more

kccodeguy commented 2 years ago

Hope this helps. I did some hacks which may NOT be the right way but may help in others finding the "professional" solution:

1) For adding text, I added a 'mAddTextEditText.requestFocus();' in the TextEditorDialogFragment -> onViewCreated class method:

public class TextEditorDialogFragment extends DialogFragment {
    ...
        @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
        ....

    mInputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    // Focus the textbox
    mAddTextEditText.requestFocus();  // Line I added          
}

I think I also customized the layout for the layout file 'add_text_dialog.xml' (NOT SURE) but the requestFocus() call should be enough.

2) For saving, I modified EditImageActivity->saveImage() method to use MediaStore for android Q References: Quick google search 'save image with mediastore' -> https://stackoverflow.com/questions/56904485/how-to-save-an-image-in-android-q-using-mediastore/56990305 https://newbedev.com/how-to-save-an-image-in-android-q-using-mediastore

saveImage(){

        if(isSdkHigherThan28()){
            ...
            ***Save with MediaStore & content resolver***
        }else{
            ...
            ***Save with old method***
       }

      or if you prefer:
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
            ...
            ***Save with MediaStore & content resolver***
        }else{
            ...
            ***Save with old method***
       }
Kahorotech commented 2 years ago

how do I get the edited image from the PhotoEditorView and save it using the above method

kccodeguy commented 2 years ago

Use:

        mPhotoEditorView.setDrawingCacheEnabled(true);
        editorBitmap = mPhotoEditorView.getDrawingCache();

Also, getDrawingCache() has been deprecated for Android 9 (Pie) so you could get the view bitmap with functions

/**
 * Gets bitmap from the view instead of using deprecated view.getDrawingCache
 * @param view
 * @return
 */
public static Bitmap getBitmapFromView(View view)
{
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);
    return bitmap;
}

/**
 * Gets bitmap with background color from the view instead of using deprecated view.getDrawingCache
 * @param view
 * @param defaultColor
 * @return
 */
public static Bitmap getBitmapFromView(View view,int defaultColor)
{
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawColor(defaultColor);
    view.draw(canvas);
    return bitmap;
}

so getting bitmap from view with platform dependency:

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        // Only for Pie Android 9 (API level 28) and newer versions
        // setDrawingCacheEnabled and getDrawingCache are Deprecated
        editorBitmap = getBitmapFromView(mPhotoEditorView, Color.rgb(255,255,255));
    }else{
        // Android Versions earlier than Pie Android 9 (API level 28)
        mPhotoEditorView.setDrawingCacheEnabled(true);
        editorBitmap = mPhotoEditorView.getDrawingCache();
    }
Kahorotech commented 2 years ago

Thank you that has worked

kccodeguy commented 2 years ago

Thank you that has worked

Update - Found way to get Editor Bitmap I found that the library provides a way to grab a bitmap from the PhotoEditor with no need to take it from the view (Which was not saving all my edits). You can use the provided mPhotoEditor.saveAsBitmap method with SaveSettings builder and OnSaveBitmap callback:

    if(isSdkHigherThan28()){

            mPhotoEditor.saveAsBitmap(new SaveSettings.Builder().setClearViewsEnabled(false).build(),new OnSaveBitmap() {
                @Override
                public void onBitmapReady(Bitmap saveBitmap) {

                    ...
                    ***Save 'saveBitmap' with MediaStore & content resolver***

                }

                @Override
                public void onFailure(Exception e) {

                }
            });

    }else{
        ...
        ***Save with old method***
   }
github-actions[bot] commented 2 years ago

This issue is stale because it has been open 20 days with no activity. Remove stale label or comment or this will be closed in 4 days.

github-actions[bot] commented 2 years ago

This issue was closed because it has been stalled for 5 days with no activity.