Getting error while calling the method

107 Views Asked by At

I am trying to make an app where when ever the app is open it will show a different image in the background but when I tried to call the method. I am getting error.I am making blur image for api lower than 16 this is what I used

public class MainActivity extends AppCompatActivity {
int roll[]={R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5,R.drawable.img6};
int number;
private final Paint mPaint = new Paint();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Random num= new Random();
number=num.nextInt(6);
    ImageView img =(ImageView)findViewById(R.id.imageView);

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),roll[number]);
    Bitmap blurredBitmap = blur(""+roll[number]);
    img.setImageBitmap(blurredBitmap);
}


public Bitmap blur(final String pathToBitmap) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    final Bitmap normalOne = BitmapFactory.decodeFile(pathToBitmap, options);
    final Bitmap resultBitmap = Bitmap.createBitmap(options.outWidth, options.outHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(resultBitmap);
    mPaint.setAlpha(180);
    canvas.drawBitmap(normalOne, 0, 0, mPaint);
    int blurRadius = 12;
    for (int row = -blurRadius; row < blurRadius; row += 2) {
        for (int col = -blurRadius; col < blurRadius; col += 2) {
            if (col * col + row * row <= blurRadius * blurRadius) {
                mPaint.setAlpha((blurRadius * blurRadius) / ((col * col + row * row) + 1) * 2);
                canvas.drawBitmap(normalOne, row, col, mPaint);
            }
        }
    }
    normalOne.recycle();
    return resultBitmap;
}
}

where i am doing wrong ? I am using this method because in google most of the blur effect is don't work in api<16. Just want to know what I am doing wrong in the code.

1

There are 1 best solutions below

0
On BEST ANSWER

When you try to use this code:

Bitmap blurredBitmap = blur(""+roll[number]);

This path(""+roll[number]) does not match the absolute path because drawable resources are added to binary at run time, so cannot be accessed by path. Try To use

BitmapFactory.decodeResource(getResources(),resourceID);

 public Bitmap blur(int resourceId) {
        final Bitmap normalOne = BitmapFactory.decodeResource(getResources(),resourceId);
        final Bitmap resultBitmap = Bitmap.createBitmap(normalOne.getWidth(), normalOne.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(resultBitmap);
        mPaint.setAlpha(180);
        canvas.drawBitmap(normalOne, 0, 0, mPaint);
        int blurRadius = 12;
        for (int row = -blurRadius; row < blurRadius; row += 2) {
            for (int col = -blurRadius; col < blurRadius; col += 2) {
                if (col * col + row * row <= blurRadius * blurRadius) {
                    mPaint.setAlpha((blurRadius * blurRadius) / ((col * col + row * row) + 1) * 2);
                    canvas.drawBitmap(normalOne, row, col, mPaint);
                }
            }
        }
        normalOne.recycle();
        return resultBitmap;
    }