I am writing a function, which takes a bitmap, edits it and saves it.
But the problem is that the saved image is not having the same dimensions.
Can anyone help me fix it? I want the output image to be of same length and width as that of the original image.
This is my code -
public void bitTest(View v) throws IOException {
Drawable t=getResources().getDrawable(R.drawable.slider_touch,getTheme());//the image to be processed
Bitmap bit=((BitmapDrawable) t).getBitmap();//its bitmap
Bitmap finalImage=Bitmap.createBitmap(bit.getWidth(),bit.getHeight(),bit.getConfig());//creating another bitmap of same dimensions
Context context=getApplicationContext();
FileOutputStream fos = context.openFileOutput("test.png", Context.MODE_PRIVATE);//saving
finalImage.compress(Bitmap.CompressFormat.PNG,100, fos);
fos.close();
}
Here the original size is having dimensions of 288x192 but the output image has dimensions of 605x403.
Where am i wrong?
Also i have tried changing the quality, but it doesn't works.
When placing the image resources in drawable directory, the decoded image size depends upon the screen size of the running device.
Keep your drawable inside the folder drawable-nodpi rather than other drawable directories so that the image size remains preserved.
Also rather than using BitmapDrawable, you can directly decode a bitmap from resource using BitmapFactory.decodeResource()
Related
I tried to get full size bitmap from drawable but not get.My image store into drawable folder and its sized 1082*1673 and when i get bitmap from drawable its sized 662*1024
I tried to all possible changes
My code shown below
[1] Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.t_0001);
[2] BitmapFactory.Options o = new BitmapFactory.Options();
o.inTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.t_0001, o);
Put that image must be in drawable-xxhdpi folder
I guess if you put your images under the Assets folder (or subfolder inside assets), the auto resize will not happen.
I have read multiple posts like this about memory usage of background image.
my background image is 2048x1365 59KB JPEG; its uncompressed bitmap is 11MB
the background on the view for the particular device would be 480x605, so usage would be 1.1MB (480x605x4)
my app originally uses 12MB without background image
placing the image in drawable-nodpi/ and set it in the layout XML cause the memory usage to 23MB; so exactly base + BMP size
Using BitmapFactory to decode the image (moved to raw/) according to the advice results in 33MB of memory usage. (See codes below.)
Codes to set the background
View view = findViewById(R.id.main_content);
Rect rect = new Rect();
view.getLocalVisibleRect(rect);
BitmapFactory.Options options = new BitmapFactory.Options();
options.outHeight = rect.height();
options.outWidth = rect.width();
options.inScaled = false;
Bitmap backgroundBitmap = BitmapFactory.decodeResource(getResources(), backgroundId, options);
view.setBackgroundDrawable(new BitmapDrawable(getResources(), backgroundBitmap));
What goes wrong? What else can I do to shrink the memory usage?
The trick to getting BitmapFactory to give you a low-memory image is to fill in inSampleSize on the BitmapFactory.Options. This tells BitmapFactory to downsample the image as it loads, giving you a lower-resolution image, but one that is better tuned to whatever use you plan to put it to. You would need to calculate the desired inSampleSize that you want, based on the resolution of the ImageView (or whatever) that you are using the image for.
This sample app demonstrates loading some images out of assets/ with different inSampleSize values.
I have experienced this too but with much smaller images. I found out of that this was happening because I was using the same image size for all screen resolutions. I recommend you have different sizes of the same image and put them in the appropriate folders.
I would like to create an image chooser from my drawable resources. I think I have found an "easy" way to do this. I was going to create a listview that displays the drawables , and the user would be able to pick one. I would then pass back , the path to this drawable resource, the paths would be hard-coded in. I then would set a bitmap to this drawable path. I was trying to do a simple test of this with just hard coded values, (no listview yet) but I do not get a visible bitmap back. I think it "works" , but maybe the bitmap is to small or not set to visible?
bm2 = BitmapFactory.decodeFile("/gds2/res/drawable-hdpi/feel_like_a_sir.png");
foreground_imageview.setImageBitmap(bm2);
Try to load your drawable in this way
bm2 = BitmapFactory.decodeResource(getResources(), R.drawable.feel_like_a_sir);
foreground_imageview.setImageBitmap(bm2);
new to android. I don't think any of the questions on here are the same as mine.
I have images loaded into my res folder. I put them into a drawable folder. How can I get the pixels of an image named bb.png in the res.drawable folder?
I will need a simple explanation on how to get the image file into a variable, and what 'getPixel(...)' command I will need to use. I don't need to display the image, just get the pixel array from it, and check if the pixel is black or white. Any help is appreciated, thanks!
Mike
It's actually really easy!
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);
Once, you have a Bitmap object, there's a couple of options.
bm.getPixel(x,y) will return an int that corresponds to an int in the Color class, such as Color.BLACK or Color.WHITE.
Additionally, bm.copyPixelsToBuffer(Buffer destination) will copy all of the pixels into a Buffer object, which you could search pixel-by-pixel.
Check out the documentation for further details.
Bitmap Documentation
Color Documentation
Here is a sample snippet of code, assuming that you have an image in your /res/drawable folder called 'image'.
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);
int pixelColor = bm.getPixel(10,10); //Get the pixel at coordinates 10,10
if(pixelColor == Color.BLACK) {
//The pixel is black
}
else if(pixelColor == Color.WHITE) {
//The pixel was white
}
Obviously, you should be careful about getting pixels. Make sure the pixel exists, and that the coordinate is not bigger than the image. To get the dimensions of a Bitmap, simply use bm.getHeight() and bm.getWidth(), respectively.
In my project I read a bitmap picture from assets directory and I want to change the quality of this picture and save it again. I am using this code:
FileOutputStream fos = new FileOutputStream(path);
image.compress(Bitmap.CompressFormat.PNG, 10, fos);
The problem is in second line whether I put 10 or 100, result is the same in terms of pixels of width, height and size of the picture. How can I resize or change a quality?
Thanks
Saving in a compressed format should not by itself change the image dimensions. The quality factor will merely effect the amount of data used to represent an compressed image having the same dimensions as the original.
If you want to rescale it too, you might want to use createScaledBitmap() first.