android-bitmap
Caused by java.lang.NullPointerException: Attempt to invoke virtual method ‘int android.graphics.Bitmap.getWidth()’ on a null object reference
The problem you are facing is that you are trying to getWidth() on your unscaledBitmap in the createScaledBitmap function. Clearly, your unscaledBitmap is null sometimes; and calling getWidth() is causing the Null Pointer exception. The root cause is that decodeResource is returning you a null for whatever reason. The reasons can include – No read … Read more
How do you get the RGB values from a Bitmap on Android?
This may be slightly late, but to clear up the confusion with the use of &0xff: In Java ints are 32 bits, so the (A)RGB values for each pixel are packed in 4 bytes. In other words, a pixel with the values R(123), G(93), B(49) = FF7B 5D31 in the ARGB_8888 model. Where Alpha = … Read more
Flip a Bitmap image horizontally or vertically
Given cx,cy is the centre of the image: Flip in x: matrix.postScale(-1, 1, cx, cy); Flip in y: matrix.postScale(1, -1, cx, cy); Altogether: public static Bitmap createFlippedBitmap(Bitmap source, boolean xFlip, boolean yFlip) { Matrix matrix = new Matrix(); matrix.postScale(xFlip ? -1 : 1, yFlip ? -1 : 1, source.getWidth() / 2f, source.getHeight() / 2f); return … Read more
Android – ImageView: setImageBitmap VS setImageDrawable
There is no difference between the two internally setImageBitmap is calling setImageDrawable. Below code is picked from ImageView.java of AOSP public void setImageBitmap(Bitmap bm) { // if this is used frequently, may handle bitmaps explicitly // to reduce the intermediate drawable object setImageDrawable(new BitmapDrawable(mContext.getResources(), bm)); }
Drawable to byte[]
Drawable d; // the drawable (Captain Obvious, to the rescue!!!) Bitmap bitmap = ((BitmapDrawable)d).getBitmap(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); byte[] bitmapdata = stream.toByteArray();
android get Bitmap or sound from assets
public static Bitmap getBitmapFromAsset(Context context, String filePath) { AssetManager assetManager = context.getAssets(); InputStream istr; Bitmap bitmap = null; try { istr = assetManager.open(filePath); bitmap = BitmapFactory.decodeStream(istr); } catch (IOException e) { // handle exception } return bitmap; } the path is simply your file name fx bitmap.png. if you use subfolder bitmap/ then its bitmap/bitmap.png