I am saving small image about width = 30px and height = 30px and file type is png on sdcard. After saving the image when I view it on my sdcard, the image appears bigger with low resolution. The original image however is crystal clear. Below is my code, please tell me how can I make the image quality better and also let the image be it's original size and not appear bigger. Thanks
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.b_01);
ByteArrayOutputStream bStream1 = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, bStream1);
File f1 = new File(Environment.getExternalStorageDirectory() + File.separator + "AppName" + "/bmp.png");
if(!f1.exists()) {
try {
f1.createNewFile();
FileOutputStream fs1 = new FileOutputStream(f1);
fs1.write(bStream1.toByteArray());
fs1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Related
I am working with Canvas in Android Studio, have .png image on canvas and want to save it to sd card. Is it possible?
if yes then how?
thanks and regards.
This code may help you (Saving canvas to bitmap on Android)
Bitmap toDisk = null;
try {
// TODO: Get the size of the canvas, replace the 640, 480
toDisk = Bitmap.createBitmap(640,480,Bitmap.Config.ARGB_8888);
canvas.setBitmap(toDisk);
toDisk.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("arun.jpg")));
} catch (Exception ex) {
}
You should have created Canvas with new Canvas(myBitmap);. So when you draw on the Canvas, it draws to your bitmap.
String fileName = Environment.getExternalStorageDirectory() + "/test.png";
OutputStream stream = new FileOutputStream(fileName);
/* Write bitmap to file using JPEG or PNG and 80% quality hint for JPEG. */
myBitmap.compress(CompressFormat.PNG, 80, stream);
stream.close();
In my app I have a rectangle ImageView that contains an white background with a hole cut out from the middle. Behind it I have set a camera preview so only the hole shows the camera. How would I just take a picture of the IMAGEVIEW.X AND Y AND HEIGHT AND WIDTH so I can still see the face. Basically crop the screenshot to the size of the imageview
Hope this Helps.
private void takeScreenshot() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
openScreenshot(imageFile);
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
}
I want to load a bitmap from sdcard with fill full height width of imageview i have done this code
im = (ImageView) findViewById(R.id.imageView1);
im.setDrawingCacheEnabled(true);
b = im.getDrawingCache();
// im.setImageBitmap(b);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "TEST1.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
File imgFile = new File(Environment
.getExternalStorageDirectory()
+ File.separator
+ "TEST1.jpg");
if (imgFile.exists()) {
im.setScaleType(ScaleType.CENTER_CROP);
// im.setScaleType(ScaleType.FIT_XY);//this is also not giving me proper o/p
// im.setVisibility(View.GONE);
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile
.getAbsolutePath());
im.setImageBitmap(myBitmap);
}
but it not working
It display like this But Actually it must display in full screen
Actually what i want to do is load any bitmap resizing it and want to display only selected portion of image in same imageview but selected image portion in fill imageview
Updated: Just saw the comment saying you want to zoom etc.
Save yourself the headache of reinventing the wheel and check-out Chris Banes' great PhotoView library.
After saving the image my image is appearing small and inside the canvas and also blur and showing black background.Can any one tell me why?
Bitmap mBackground = Bitmap.createBitmap(myBitmap.getWidth() ,myBitmap.getHeight(),myBitmap.getConfig());
mCanvas = new Canvas(mBackground);
//mCanvas.drawBitmap(mBackImage, (mCanvas.getWidth() / 2), 0, null);
mCanvas.drawBitmap(myBitmap ,mCanvas.getWidth(),mCanvas.getHeight(), null);
mCanvas.drawBitmap(myBitmap1,x-dx,y-dy, null);
try
{
mBitmapDrawable = new BitmapDrawable(mBackground);
Bitmap mNewSaving = mBitmapDrawable .getBitmap();
String ftoSave = mTempDir + mSaveImageName;
File mFile = new File(ftoSave);
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
FileOutputStream out = new FileOutputStream(mFile);
mNewSaving.compress(CompressFormat.JPEG,0, out);
Toast.makeText(getApplicationContext(), "Image Saved",Toast.LENGTH_SHORT).show();
out.flush();
out.close();
}
In mNewSaving.compress(CompressFormat.JPEG,0, out); the second parameter is as follows:
Hint to the compressor, 0-100. 0 meaning compress for small size, 100
meaning compress for max quality. Some formats, like PNG which is
lossless, will ignore the quality setting
So if you want a large image, you should put it 100
mNewSaving.compress(CompressFormat.JPEG, 100, out);
I'm making bitmap (that will be printed on paper, later) and using canvas to draw on it.
But after saving it always have 72 dpi resolution. I tried to use bitmap.setDensity(96);but it does not seems to work.
This is how I make bitmap and save it, nothing fancy
Bitmap outBitmap = Bitmap.createBitmap(378,559,Bitmap.Config.RGB_565);
OutputStream outStream = null;
File file = new File(Environment.getExternalStorageDirectory(),
"96dpiBitmap.png");
try {
outStream = new FileOutputStream(file);
outBitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
// doh
} catch (IOException e) {
// doh
}
So. How do I save bitmap with dpi > 72?
you have to create scaled bitmap from the original one for ex :
MyNewBitmap = Bitmap.createScaledBitmap(myOldOne,612,612,false);
where 612 width and 612 height the result will be image squared. i'm using this method to prevent instagram from scaling or cutting my image so its perfectly fit into instagram image cropping :).
anyway you have to find the proper way to scale your image to fit 72dpi. i guess 800x600 will do the trick. try to create new bitmap and scale the old one and then save the newBitmap.
good luck