I have taken thumbnail image from gallery which is smaller in size and resized into 300*300 size.
By doing that the image looking so blurred.
getting image from gallery
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case 0:
if (resultCode == RESULT_OK) {
try {
flag = 1;
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex); // file
// path
// of
// selected
// image
cursor.close();
// Convert file path into bitmap image using below line.
yourSelectedImage = download.getResizedBitmap(
image.decodeImage(filePath),270,228);
// put bitmapimage in your imageview
profile_image.setImageBitmap(
yourSelectedImage);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Image resizing
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth)
{
try
{
if(bm!=null)
{
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
}
}
catch(Exception e)
{
e.printStackTrace();
}
return resizedBitmap;
}
Images get blurred when you enlarge them (unless you are using an vector image and you are using a bitmap). Your getResizedBitmap method doesn't do anything much other than stretching the image to fit the new size. The only way you are going to solve your problem is by selecting larger images (but then eventually you will run into the aspect ratio problem, so you should really rethink your scaling algorithm).
Of course enlarging a bitmap image will be pixelated..
Related
I am using the following code to let a user import an image from their gallery into the app.
public void loadImagefromGallery(View view) {
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
ImageView imgView = (ImageView) findViewById(R.id.imgView);
// Set the Image in ImageView after decoding the String
imgView.setImageBitmap(BitmapFactory
.decodeFile(imgDecodableString));
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
But, I want the size of the image to be a certain size no matter what the image size they have imported is. Whenever I import an image it always fill the whole screen. Let's say I want the image size to be 50 width by 50 height.
There are two ways to set the imageview size
from the xml
<ImageView
android:id="#+id/image_view"
android:layout_width="200dp"
android:layout_height="100dp"
android:scaleType="fitCenter"
android:src="#drawable/iclauncher"
android:scaleType="fitXY"/>
from programmatically
image_view.getLayoutParams().height = 20;
image_view.getLayoutParams().width = 20;
this is the way to resize bitmap image
public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(
bm, 0, 0, width, height, matrix, false);
bm.recycle();
return resizedBitmap;
}
As suggested by saeed:
// Set the Image in ImageView after decoding the String
imgView.setImageBitmap(decodeUri(this, Uri.parse(imgDecodableString), 50));
Where decodeUri is from here.
In my app, I have built a camera function in the Activity B. The selected image will then placed in the imageView B.
Activity B ---> one imageView B, used to place the selected image
The selected image can be displayed on ImageView B, but it looked blur. Please tell me what should I do . Thanks in advance !
Activity B
private void activeGallery() {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode,Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_LOAD_IMAGE:
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null,null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap a = (BitmapFactory.decodeFile(picturePath));
photo=scaleBitmap(a,200,200);
imageView.setImageBitmap(photo);
}
}
}
public static Bitmap scaleBitmap(Bitmap bitmap, int wantedWidth, int wantedHeight) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float scaleWidth = ((float) wantedWidth) / width;
float scaleHeight = ((float) wantedHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, wantedWidth, wantedHeight, false);
return resizedBitmap;
}
Original image
Place in ImageView B (looked blur)
how can I fix the size of the imageView ?
You may try this
<ImageView
android:id="#+id/YourImageViewId"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentRight="true"
android:scaleType="centerInside"
android:gravity="right|center_vertical"
android:padding="10px"
/>
Source get from here
This question already has answers here:
Pick an image and resize to 72x72
(3 answers)
Closed 9 years ago.
I picked an image from gallery and decoded it. Now I just want to resize that bitmap to standard 72x72 size in order to use as an profile photo.
I searched a lot but nothing worked, some of them rotated my image for no reason, some of them makes image very low quality. Is it that hard?
Check scaled 72x72 and original one:
72x72 (As you see, rotated and very bad quality)
http://imgim.com/9958incic3494599.png
original:
http://imgim.com/3847incix7666386.png
Code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent)
{
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode)
{
case 100: // SELECT_PHOTO
if(resultCode == RESULT_OK)
{
Uri selectedImage = imageReturnedIntent.getData();
InputStream imageStream;
try
{
imageStream = getContentResolver().openInputStream(selectedImage);
}catch (Exception e){ return; }
Bitmap bm = BitmapFactory.decodeStream(imageStream);
bm = Bitmap.createScaledBitmap(bm, 72, 72, true);
UpdateAvatar(bm);
}
break;
}
}
Try this function :
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
It helps for me.
When I pick an image from the gallery it is too large and I need to resize it. Can anyone give me suggestions on how I might be able to accomplish this?
See code below:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_GALLERY && resultCode == RESULT_OK) {
Uri uri = data.getData();
try {
bitmap = Media.getBitmap(this.getContentResolver(), uri);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
imageView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_GALLERY);
imageView.setImageBitmap(bitmap);
}
});
This post has some excellent samples for you: How to Resize a Bitmap in Android?
In your case, this method helps
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
Edit
Also from the same post, this fits your need in an easier way
Bitmap resizedBitmap = Bitmap.createScaledBitmap(originalBitmap, newWidth, newHeight, false);
Update
You can use the code in this way:
// Set the new width and height you want
int newWidth;
int newHeight;
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_GALLERY && resultCode == RESULT_OK) {
Uri uri = data.getData();
try {
bitmap = Media.getBitmap(this.getContentResolver(), uri);
bitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, false);
} catch (Exception e) {
e.printStackTrace();
}
}
}
I am implementing an android program that allows users to upload photos from their device gallery to an ImageView. saving it in the cloud. My code works fine with small photos, but bigger photos caused the application to stop. I'm getting this error:
Bitmap too large to be uploaded into a texture (4128x2322, max=4096x4096)
I tried to resize the uploaded photo before displaying it using suggestions from previous questions, but they didn't work. I am not sure what the problem with my code is.
Any help would be appreciated. Here is the code from my last attempt:
{
// omitted code segment from onCreate...
browseButton = ((Button) findViewById(R.id.browse_button));
browseButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
}
);
}
//end on create
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if (requestCode == SELECT_PICTURE)
{
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath);
// Convert it to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Compress image to lower quality scale 1 - 100
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
image = stream.toByteArray();
bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
Bitmap toyImageScaled = Bitmap.createScaledBitmap(bitmap, 200, 200
* bitmap.getHeight() / bitmap.getWidth(), false);
// Override Android default landscape orientation and save portrait
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotatedScaledToyImage = Bitmap.createBitmap(toyImageScaled, 0,
0, toyImageScaled.getWidth(), toyImageScaled.getHeight(),
matrix, true);
toyPreview.setImageBitmap(bitmap);
}
}
}
public String getPath(Uri uri)
{
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
You're still showing the big image at:
toyPreview.setImageBitmap(bitmap);
You should be showing the scaled image, toyImageScaled or rotatedScaledToyImage.
I think you should use the examples of the documentation, and use InSampleSize.
see this : http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
From DOCS:
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}