java.lang.StackOverflowError: размер стека 8 МБ при настройке захвата фонового изображения с камеры

#android #android-camera #android-custom-view

#Android #android-камера #android-пользовательский просмотр

Вопрос:

Я пытаюсь захватить изображение с камеры и установить изображение в качестве фона для пользовательского представления, чтобы я мог рисовать на нем. но я получил сообщение об ошибке. Ниже приведен код, вызывающий ошибку, может ли кто-нибудь направить меня по правильному пути.

  private CustomView mCustomView;
    Uri uri;
    File file;
    Drawable myDrawable;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_draw_view);

        mCustomView = (CustomView)findViewById(R.id.custom_view);
        //mCustomView.setBackground(mCustomView,R.drawable.one);
        //mCustomView.setBackgroundResource();
        Bundle extras = getIntent().getExtras();
        uri = Uri.parse(extras.getString("imageUri"));

        try {
            InputStream inputStream = getContentResolver().openInputStream(uri);
            myDrawable = Drawable.createFromStream(inputStream, uri.toString() );

            mCustomView.setBackgroundResource(myDrawable);
        } catch (FileNotFoundException e) {
            myDrawable = getResources().getDrawable(R.drawable.one);
        }




    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case R.id.undo:
                mCustomView.onClickUndo();
                return true;
            case R.id.save:
                saveThisDrawing();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void saveThisDrawing()
    {
        if (requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            String path = Environment.getExternalStorageDirectory().toString();
            path = path   "/"   getString(R.string.app_name);
            File dir = new File(path);
            //save drawing
            mCustomView.setDrawingCacheEnabled(true);

            //attempt to save
            String imTitle = "Drawing"   "_"   System.currentTimeMillis()   ".png";
            String imgSaved = MediaStore.Images.Media.insertImage(
                    getContentResolver(), mCustomView.getDrawingCache(),
                    imTitle, "a drawing");

            try {
                if (!dir.isDirectory() || !dir.exists()) {
                    dir.mkdirs();
                }
                mCustomView.setDrawingCacheEnabled(true);
                File file = new File(dir, imTitle);
                FileOutputStream fOut = new FileOutputStream(file);
                Bitmap bm = mCustomView.getDrawingCache();
                bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);


            } catch (FileNotFoundException e) {
                e.printStackTrace();
                AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.setTitle("Uh Oh!");
                alert.setMessage("Oops! Image could not be saved. Do you have enough space in your device?1");
                alert.setPositiveButton("OK", null);
                alert.show();

            }

            if (imgSaved != null) {
                Toast savedToast = Toast.makeText(getApplicationContext(),
                        "Drawing saved to Gallery!", Toast.LENGTH_SHORT);
                savedToast.show();
            }

            mCustomView.destroyDrawingCache();
        }
    }
    private boolean requestPermission(String writeExternalStorage) {
        if(Build.VERSION.SDK_INT >= 24){

            requestPermissions(new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE},2);
            return true;
        }
        return true;
    }
  

Мой пользовательский код выглядит следующим образом:

 public class CustomView extends View {

    private static final String LOG_CAT = CustomView.class.getSimpleName();

    //drawing path
    private Path drawPath;

    //defines what to draw
    private Paint canvasPaint;

    //defines how to draw
    private Paint drawPaint;

    //initial color
    private int paintColor = 0xFF660000;

    private Paint _paintBlur;


    //canvas - holding pen, holds your drawings
    //and transfers them to the view
    private Canvas drawCanvas;

    //canvas bitmap
    private Bitmap canvasBitmap;

    //brush size
    private float currentBrushSize, lastBrushSize;

    private ArrayList<Path> paths = new ArrayList<Path>();
    private ArrayList<Path> undonePaths = new ArrayList<Path>();
    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;

    private void init(){
        currentBrushSize = 20;
        lastBrushSize = currentBrushSize;

        drawPath = new Path();
        drawPaint = new Paint();
        drawPaint.setColor(paintColor);
        drawPaint.setAntiAlias(true);
        drawPaint.setStrokeWidth(currentBrushSize);
        drawPaint.setStyle(Paint.Style.STROKE);
        drawPaint.setStrokeJoin(Paint.Join.ROUND);
        drawPaint.setStrokeCap(Paint.Cap.ROUND);

        canvasPaint = new Paint(Paint.DITHER_FLAG);

        this._paintBlur = new Paint();
        this._paintBlur.set(drawPaint);
        this._paintBlur.setAntiAlias(true);
        this._paintBlur.setDither(true);
        this._paintBlur.setStyle(Paint.Style.STROKE);
        this._paintBlur.setStrokeJoin(Paint.Join.ROUND);
        this._paintBlur.setStrokeCap(Paint.Cap.ROUND);
        this._paintBlur.setColor(Color.RED);
        this._paintBlur.setStrokeWidth(6);
        this._paintBlur.setMaskFilter(new BlurMaskFilter(10.0F, BlurMaskFilter.Blur.OUTER));


    }

    public void setBackgroundResource(Drawable drawable){


        setBackgroundResource(drawable);

    }

  /*  public void setBackground(View view, Drawable background) {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            view.setBackground(background);
        } else {
            view.setBackgroundDrawable(background);
        }
    }*/

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);

        init();
       // setBackgroundResource(R.drawable.one);
    }

    @Override
    protected void onDraw(Canvas canvas) {


        for (Path p : paths) {
            canvas.drawPath(p, drawPaint);

        }


        canvas.drawPath(drawPath, drawPaint);
       // canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.one), 0, 0, canvasPaint);
       // canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
       //create canvas of certain device size.
        super.onSizeChanged(w, h, oldw, oldh);


        //create Bitmap of certain w,h
       canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

        //apply bitmap to graphic to start drawing.
        drawCanvas = new Canvas(canvasBitmap);



    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float touchX = event.getX();
        float touchY = event.getY();

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                touch_start(touchX, touchY);
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                touch_move(touchX, touchY);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                touch_up();
                invalidate();
                break;
            default:
                return false;
        }
        return true;
    }


    private void touch_start(float x, float y) {
        undonePaths.clear();
        drawPath.reset();
        drawPath.moveTo(x, y);
        mX = x;
        mY = y;
    }

    private void touch_up() {
        drawPath.lineTo(mX, mY);
        drawCanvas.drawPath(drawPath, drawPaint);
        paths.add(drawPath);
        drawPath = new Path();

    }

    private void touch_move(float x, float y) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            drawPath.quadTo(mX, mY, (x   mX)/2, (y   mY)/2);
            mX = x;
            mY = y;
        }
    }


    public void onClickUndo () {
       if (paths.size()>0)
        {
            undonePaths.add(paths.remove(paths.size()-1));
            invalidate();
        }

    }
  

Только часть ошибки:

 2019-04-14 20:16:26.196 15300-15300/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.undopaint1, PID: 15300
    java.lang.StackOverflowError: stack size 8MB
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
2019-04-14 20:16:26.200 15300-15300/? E/AndroidRuntime:     at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
2019-04-14 20:16:26.560 15300-15314/? I/zygote64: Background concurrent copying GC freed 339(60KB) AllocSpace objects, 0(0B) LOS objects, 28% free, 58MB/82MB, paused 147.156ms total 517.058ms
    , len = 6
2019-04-14 20:16:27.013 15300-15314/? I/zygote64: Background concurrent copying GC freed 528864(56MB) AllocSpace objects, 1(2MB) LOS objects, 30% free, 55MB/79MB, paused 398us total 280.785ms
2019-04-14 20:16:27.426 15300-15314/? I/zygote64: Background concurrent copying GC freed 528919(55MB) AllocSpace objects, 0(0B) LOS objects, 31% free, 52MB/76MB, paused 547us total 240.932ms
2019-04-14 20:16:27.579 15300-15300/? I/chatty: uid=10307(u0_a307) com.example.undopaint1 identical 3038 lines
2019-04-14 20:16:27.580 15300-15300/? E/AndroidRuntime:     at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
2019-04-14 20:16:27.580 15300-15300/? E/AndroidRuntime:     at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.CustomView.setBackgroundResource(CustomView.java:85)
        at com.example.undopaint1.DrawView.onCreate(DrawView.java:44)
        at android.app.Activity.performCreate(Activity.java:7383)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3256)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3411)
        at android.app.ActivityThread.-wrap12(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994)
        at android.os.Handler.dispatchMessage(Handler.java:108)
        at android.os.Looper.loop(Looper.java:166)
        at android.app.ActivityThread.main(ActivityThread.java:7529)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
2019-04-14 20:16:27.863 15300-15314/? I/zygote64: Background concurrent copying GC freed 484245(50MB) AllocSpace objects, 4(212KB) LOS objects, 25% free, 70MB/94MB, paused 430us total 263.626
  

Если есть какая-либо альтернатива для установки изображения, а также логика отмены не затрагивается, пожалуйста, направьте меня, поскольку я не эксперт.Я впервые имею дело с изображениями и пользовательскими представлениями.

Ответ №1:

Лучший способ работы с изображением — использовать Piccasso ,

 Picasso.get().load(path or url).into(imageView);
  

и никогда не используйте огромные изображения в качестве фона!

Создайте свой чертеж из пути :

 Drawable d = Drawable.createFromPath(pathName)
  

А затем измените его размер, прежде чем устанавливать его в качестве фона

 Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
// Scale it to 50 x 50
Drawable d = newBitmapDrawable(getResources(),Bitmap.createScaledBitmap(bitmap,50, 50, true));
  

Комментарии:

1. спасибо за ответ, но не могли бы вы предложить код изменения размера

2. Drawable d = Drawable.createFromPath(uri.toString()); это приемлемо

3. все та же ошибка после редактирования кода, подобного этой строке path = uri.getPath(); Drawable d = Drawable.createFromPath(путь); d = new ScaleDrawable(d, 0, 50, 50).getDrawable(); d.setBounds(0, 0, 50, 50); мАкустоМвью.setBackgroundResource (d);

4. спасибо за ваши усилия, но та же ошибка по-прежнему сохраняется.

5. могу ли я хотя бы знать, что вы действительно хотите сделать, может быть, мы сможем найти другой подход к этому