How can I bitmap my gallery image in android studio - java

Hi everyone I am trying to bitmap my image but it gives me an empty bitmap every time I select any image from my gallery. Please help
public void start(View view) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, Gallery_Intent);
}
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uri = data.getData();
imageView.setImageURI(uri);
Uri pickedImage = data.getData();
// Let's read picked image path using content resolver
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
cursor.moveToFirst();
String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
if (bitmap == null)
Toast.makeText(MainActivity.this, "failed1", Toast.LENGTH_SHORT).show();
}

You can get bitmap of image more easier with Picasso or Glide, the example is using Picasso
Bitmap bitmap = Picasso.get().load(uri).get();

Related

android take image from phone album and store it in sqlite database

i'm making an app where the user can choose images from the phone's album and store it into a table in sqlite database, i have seen many posts here about this issue but couldn't understand the solutions (neither did they work for me), in my activity i have an imageview which when clicked will open the album and allow the user to choose an image and also i have a button which when clicked will store the image in sqlite database, i'm just able to choose the image but after that i'm stuck, i got to this method in my code:
public void getImage(View view) throws IOException {
ImageView v=(ImageView)view;
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent,1);
}
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (resultCode == RESULT_OK) {
try {
final Uri imageUri = data.getData();
final InputStream imageStream = getContext().getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
image.setImageBitmap(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
now what should i do after this to store the image in the db?
If you are sure about the images will not be removed from internal storage, you can save the image path. If want to save the image data, you can do the following.
for selecting image
private void selectImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, IMAGE_REQ);
}
In onActivityResult, from Intent data ...
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == IMAGE_REQ && resultCode == Activity.RESULT_OK && data != null) {
Uri path = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), path);
Bitmap.createScaledBitmap(bitmap, 150, 150, true);
} catch (IOException e) {
e.printStackTrace();
}
}
}
get byte[] from bitmap
public static byte[] getByteArrayFromBitmap(Bitmap bitmap) {
if(bitmap == null) return null;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, byteArrayOutputStream);
return byteArrayOutputStream.toByteArray();
}
then save the byte[] as blob in sqlite
for getting the images as bitmap
public static Bitmap getBitmapFromByteArray(byte[] blob){
if(blob == null) return null;
Bitmap bitmap = BitmapFactory.decodeByteArray(blob, 0, blob.length);
return bitmap;
}

How to add image in Sqlite database from phone gallery?

This is my code to get data from gallery and save it in SQLite database but its not working
.....
case PICK_FROM_GALLERY:
Bundle extras2 = data.getExtras();
if (extras2 != null) {
Bitmap yourImage = extras2.getParcelable("data");
// convert bitmap to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
yourImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte imageInByte[] = stream.toByteArray();
Log.e("output before conversion", imageInByte.toString());
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addContact(new Contact(resultCode, "Android", imageInByte));
Intent i = new Intent(Editor.this, Editor.class);
startActivity(i);
finish();
}
break;
}
}
Store Uri of an Image, through Uri you can set an Image in ImageView
Uri imageUri;
Intent pickUpImage = new Intent(Intent.ACTION_PICK);
pickUpImage.setType("image/*");
startActivityForResult(pickUpImage, 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
imageUri = uri;
profilePic.setImageURI(imageUri);
}
}

Image String data send to the next activity

I select image from gallery and take image through camera.The code is following:
ib2 = (ImageButton) findViewById(R.id.imageButton2);
ib2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
private static final int PICK_IMAGE = 150;
Uri imageUrl;
private void openGallery() {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(intent, PICK_IMAGE);
}
#Override
protected void onActivityResult(int requestcode, int resultcode, Intent data) {
super.onActivityResult(requestcode, resultcode, data);
if (resultcode == RESULT_OK && requestcode == PICK_IMAGE) {
imageUrl = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),imageUrl );
// Log.d(TAG, String.valueOf(bitmap));
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
// im.setImageURI(imageUrl);
}
if (requestcode == CAMERA_REQUEST && resultcode == RESULT_OK) {
Bitmap photo;
photo = (Bitmap) data.getExtras().get("data");
im.setImageBitmap(photo);//set image to Imageview
}
}
Then trying to convert this image into base 64 string. The code is following:
private String imageToString(){
Bitmap photo = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
Then send the data to another activity. The code is following:
Intent next = new Intent(MainActivity.this, EventAdding.class);
Bundle bn = new Bundle();
bn.putString("image", imageToString());
startActivity(next);
The problem is that I am unable to synchronize all those program. The converting image string data don't send perfectly to the next activity.Image data don't send. when I was trying to show the image, a blank image is shown. Please, inform me the correct code
you can send image from one activity to other activity using the following code..
Bitmap bitmap = youimage
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("image", bitmap);
startActivity(intent);
now get the image in the secondActivity like this.
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("image");
Then display bitmap in ImageView.
You can use the intent.getParcelableExtra(key) method

Get low quality image after sending

The problem here is when I send an image to server it will send very low quality image like what show in imageview, how to send more good quality even after compressing?
bitmap1 is String that will hold a bitmap after compressing
private void takeImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, selectedImage);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
selectedImage = data.getData();
photo = (Bitmap) data.getExtras().get("data");
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
....
ByteArrayOutputStream bao = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, bao);
bitmap1 = Base64.encodeToString(bao.toByteArray(), Base64.DEFAULT);

Android intent.getData() Nullpointer exceptipon

I have an android app that start the smartphone camera
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST );
To display the taken picture I use this piece of code,
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
setImage=true;
if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) {
if(data!=null)
{
ImageView image = (ImageView) findViewById(R.id.imagePreview);
Bundle extras = data.getExtras();
Bitmap mImageBitmap = (Bitmap) extras.get("data");
image.setImageBitmap(mImageBitmap);
}
}
}
This works pretty fine but if i want to get the path of the taken picture, i have to use (intent)data.getData() but this returns a null value. what should i do to solve this problem?
Try this hope it helps you.
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();
String imageName = picturePath.substring(picturePath.lastIndexOf(
"/", picturePath.length()));
Try out as below:
Bitmap m_photo = (Bitmap) p_data.getExtras().get("data");
if (m_photo != null)
{
ByteArrayOutputStream m_upByteArrayOutputStream = new ByteArrayOutputStream();
m_photo.compress(Bitmap.CompressFormat.PNG, 40, m_upByteArrayOutputStream);
Drawable m_imageFromCamera = new BitmapDrawable(m_photo);
image.setBackgroundDrawable(m_photo);
}
EDITED:
To get the Path of the image try out below code:
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 imagePath = cursor.getString(columnIndex); <---- Here is your image path.
cursor.close();

Categories