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);
Related
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;
}
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();
User can capture or select a image from gallery. I would like to upload that image to server.
This is my code for camera -
camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//String encode = ImageBase64.encode(i);
startActivityForResult(i, CAMERA_REQUEST);
}
});
And this is my code to select a image from gallery -
gallary.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent g = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(g, GALLARY_REQUEST);
}
});
Also, I am displaying that image with below code -
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK) {
if(requestCode == CAMERA_REQUEST) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
uploaded_img.setImageBitmap(bitmap);
}
}
if(requestCode == GALLARY_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
uploaded_img.setImageURI(uri);
}
}
Now, I would like to convert this image to base64 and upload to server.
Here you go:
public static String convertToBase64(String path) {
Bitmap bm = BitmapFactory.decodeFile(path);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] byteArrayImage = baos.toByteArray();
String encodedImage = android.util.Base64.encodeToString(byteArrayImage, android.util.Base64.DEFAULT);
return encodedImage;
}
public static Bitmap decodeFromBase64ToBitmap(String encodedImage) {
byte[] decodedString = android.util.Base64.decode(encodedImage, android.util.Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
return decodedByte;
}
I've made it static in my project, so i don't have to create class instance.
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);
}
}
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