如何简单地将图像保存到相册

时间:2018-12-13 19:12:33

标签: android

我正在玩从this Kotlin sample转换/反编译的Java绘图活动。

我正在简化其功能,现在,它允许我单击“保存”按钮,并弹出一个带有“已保存!”的文本的预览,但是我想知道需要做什么。只要将按钮单击就可以将生成的图像简单地扔到Android图片库中(也就是说,保存后,该图像必须成为相机库中的独立图片)。

似乎与FileOutputStream / Bitmap.CompressFormat / MediaStore.Images 有关,并且我可以预见到文件命名方面的一些困难,即它们不会被覆盖,在这里阅读了很多答案,但是我仍然不明白逻辑,因此不胜感激。

这是我第一次尝试做类似的事情,所以我有点迷失了,我来这里问一些方向。

这是单个活动:

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();
        }

    }
}

谢谢!

1 个答案:

答案 0 :(得分:0)

我能够通过抽取另一个类似Android Drawable View的类似绘画的示例(用Java编写的更简单的示例)来克服此问题。

与之前在this onethis other one上在StackOverflow上获得的答案相比,这些不同的示例和提示足以将项目放在一起,因此我将尝试说明如何实现。

首先,您需要在Manifest.xml中向WRITE_EXTERNAL_STORAGE添加权限:

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

稍后,您只需要在activity_main.xml中添加一个保存按钮:

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

然后,您初始化按钮视图onCreate并将新的saveButtonsetOnClickListener关联起来,不要忘记实时请求权限:

    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");
        }
    });

通过使用上述方法,我已经能够在模拟器上默认图库应用程序的文件夹中保存一个新的媒体文件,如下所示:

working preview

但是,它仍然有我现在必须克服的意外的黑色背景,但是我认为最初的问题已经解决,因为它回答了我自己的原始问题。