-1

I'm playing with a drawing activity in Java converted/decompiled from this Kotlin sample.

I'm simplifying its functionalities and, as it is now, it allows me to click on "Save" button and a preview pops up with a text saying "Saved!", but I'd like to know what needs to be done to simply throw the resulting image to the Android photo gallery anytime the button is clicked (let's say, after saved, the image must become a standalone picture inside the camera gallery).

It seems it has to do with FileOutputStream/Bitmap.CompressFormat/MediaStore.Images and I can foresee some difficulties in terms of naming files in a way they don't overwrite and I'm reading a lot of answers around here, but I still didn't get the logic so any idea is appreciated.

It's the first time I'm trying to do something similar so I'm sort of lost and I come here to ask for some directions.

Here is the single activity:

public final class SampleActivity extends AppCompatActivity implements OnSeekBarChangeListener, OnClickListener {

    private HashMap _$_findViewCache;

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_sample);
        (this._$_findCachedViewById(id.close)).setOnClickListener(this);
        (this._$_findCachedViewById(id.save)).setOnClickListener(this);
        (this._$_findCachedViewById(id.undo)).setOnClickListener(this);
        (this._$_findCachedViewById(id.clear)).setOnClickListener(this);
        ((SeekBar)this._$_findCachedViewById(id.red)).setOnSeekBarChangeListener(this);
        ((SeekBar)this._$_findCachedViewById(id.green)).setOnSeekBarChangeListener(this);
        ((SeekBar)this._$_findCachedViewById(id.blue)).setOnSeekBarChangeListener(this);
        ((SeekBar)this._$_findCachedViewById(id.width)).setOnSeekBarChangeListener(this);
    }

    public void onProgressChanged(@Nullable SeekBar seekBar, int progress, boolean fromUser) {
        int var10000;
        SeekBar var10001;
        label58: {
            label50: {
                if (seekBar != null) {
                    var10000 = seekBar.getId();
                    var10001 = (SeekBar)this._$_findCachedViewById(id.red);
                    Intrinsics.checkExpressionValueIsNotNull(var10001, "red");
                    if (var10000 == var10001.getId()) {
                        break label50;
                    }
                }

                if (seekBar != null) {
                    var10000 = seekBar.getId();
                    var10001 = (SeekBar)this._$_findCachedViewById(id.green);
                    Intrinsics.checkExpressionValueIsNotNull(var10001, "green");
                    if (var10000 == var10001.getId()) {
                        break label50;
                    }
                }

                if (seekBar == null) {
                    break label58;
                }

                var10000 = seekBar.getId();
                var10001 = (SeekBar)this._$_findCachedViewById(id.blue);
                Intrinsics.checkExpressionValueIsNotNull(var10001, "blue");
                if (var10000 != var10001.getId()) {
                    break label58;
                }
            }

            SeekBar var8 = (SeekBar)this._$_findCachedViewById(id.red);
            Intrinsics.checkExpressionValueIsNotNull(var8, "red");
            int r = var8.getProgress();
            var8 = (SeekBar)this._$_findCachedViewById(id.green);
            Intrinsics.checkExpressionValueIsNotNull(var8, "green");
            int g = var8.getProgress();
            var8 = (SeekBar)this._$_findCachedViewById(id.blue);
            Intrinsics.checkExpressionValueIsNotNull(var8, "blue");
            int b = var8.getProgress();
            int color = Color.argb(255, r, g, b);
            ((FingerPaintImageView)this._$_findCachedViewById(id.finger)).setStrokeColor(color);
            (this._$_findCachedViewById(id.colorPreview)).setBackgroundColor(color);
            return;
        }

        if (seekBar != null) {
            var10000 = seekBar.getId();
            var10001 = (SeekBar)this._$_findCachedViewById(id.width);
            Intrinsics.checkExpressionValueIsNotNull(var10001, "width");
            if (var10000 == var10001.getId()) {
                ((FingerPaintImageView)this._$_findCachedViewById(id.finger)).setStrokeWidth((float)progress);
            }
        }

    }

    public void onClick(@Nullable View v) {
        if (Intrinsics.areEqual(v, this._$_findCachedViewById(id.undo))) {
            ((FingerPaintImageView)this._$_findCachedViewById(id.finger)).undo();
        } else if (Intrinsics.areEqual(v, this._$_findCachedViewById(id.clear))) {
            ((FingerPaintImageView)this._$_findCachedViewById(id.finger)).clear();
        } else if (Intrinsics.areEqual(v, this._$_findCachedViewById(id.close))) {
            this.hidePreview();
        } else if (Intrinsics.areEqual(v, this._$_findCachedViewById(id.save))) {
            this.showPreview();
        }

    }

    private final void showPreview() {
        RelativeLayout var10000 = (RelativeLayout)this._$_findCachedViewById(id.previewContainer);
        Intrinsics.checkExpressionValueIsNotNull(var10000, "previewContainer");
        var10000.setVisibility(View.VISIBLE);
        ImageView var1 = (ImageView)this._$_findCachedViewById(id.preview);
        FingerPaintImageView var10001 = (FingerPaintImageView)this._$_findCachedViewById(id.finger);
        Intrinsics.checkExpressionValueIsNotNull(var10001, "finger");
        var1.setImageDrawable(var10001.getDrawable());
    }

    private final void hidePreview() {
        RelativeLayout var10000 = (RelativeLayout)this._$_findCachedViewById(id.previewContainer);
        Intrinsics.checkExpressionValueIsNotNull(var10000, "previewContainer");
        var10000.setVisibility(View.GONE);
    }

    public void onStartTrackingTouch(@Nullable SeekBar seekBar) {
    }

    public void onStopTrackingTouch(@Nullable SeekBar seekBar) {
    }

    public void onBackPressed() {
        RelativeLayout var10000 = (RelativeLayout)this._$_findCachedViewById(id.previewContainer);
        Intrinsics.checkExpressionValueIsNotNull(var10000, "previewContainer");
        if (var10000.getVisibility() == View.VISIBLE) {
            this.hidePreview();
        } else {
            super.onBackPressed();
        }

    }

    public View _$_findCachedViewById(int var1) {
        if (this._$_findViewCache == null) {
            this._$_findViewCache = new HashMap();
        }

        View var2 = (View)this._$_findViewCache.get(var1);
        if (var2 == null) {
            var2 = this.findViewById(var1);
            this._$_findViewCache.put(var1, var2);
        }

        return var2;
    }

    public void _$_clearFindViewByIdCache() {
        if (this._$_findViewCache != null) {
            this._$_findViewCache.clear();
        }

    }
}

Thanks in advance!

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
JorgeAmVF
  • 1,660
  • 3
  • 21
  • 32

1 Answers1

0

I was able to overcome this issue by taking another paint-like sample (a simpler one and in Java) called Android Drawable View.

This different sample and tips from previous answers available here on StackOverflow like this one and this other one were enough to put the project together so I'll try to explain how to.

First, you need to add permission to WRITE_EXTERNAL_STORAGE in your Manifest.xml:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Later, you just need to add a save button to your activity_main.xml:

    <Button
        android:id="@+id/saveButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save" />

Then, you initialize the button view onCreate and associate the new saveButton with a setOnClickListener and don't forget to request permission in realtime:

    Button saveButton = findViewById(R.id.saveButton);


    saveButton.setOnClickListener(new View.OnClickListener() {

        @Override public void onClick(View v) {
            if (ContextCompat.checkSelfPermission(getBaseContext(), Manifest.permission.CAMERA) ==
                    PackageManager.PERMISSION_GRANTED) {
                drawableView.setEnabled(true);
            }
            else {
                ActivityCompat.requestPermissions(MainActivity.this, new String[]
                        { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
            }
            Bitmap bm = drawableView.obtainBitmap();
            MediaStore.Images.Media.insertImage(getContentResolver(), bm, "title" , "description");
        }
    });

By using the method described above, I've been able to save a new media file inside a folder in the default gallery app on the emulator as you can see below:

working preview

However, it's still getting an unintended black background that I must overcome now, but I consider the initial issue solved as it answers my own original question.

JorgeAmVF
  • 1,660
  • 3
  • 21
  • 32