I need to use a bitmap image but i dont know how - android

Im trying to change the google maps marker in a Android application, I tried this way:
mMap.addMarker(new MarkerOptions().position(player).title("player").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher_foreground)));
but it says that I have to use a bitmap image, what should I do?
FATAL EXCEPTION: main Process: com.example.crosilla.mappeprova2, PID: 18930
com.google.maps.api.android.lib6.common.apiexception.b: Failed to decode image. The provided image must be a Bitmap

Your code looks ok. Try with a resource from the mipmap folder.

call this on button click:
startActivityForResult(Intent.createChooser(intent, "Select Picture"), RESULT_LOAD_IMAGE);
then :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
dialog = new ProgressDialog(PhotoUpload.this);
dialog.setMessage("Please wait......");
dialog.setCancelable(false);
dialog.show();
Uri selectedImage = data.getData();
SharedPreferences sharedPreferences = getSharedPreferences("SavedPhoto", Context.MODE_PRIVATE);
Bitmap photo = null;
try {
photo = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage);
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
byte[] decodedString = Base64.decode(encodedImage , Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
CircularImageView imageView = findViewById(R.id.profileimage);
imageView.setImageBitmap(decodedByte);
}
}

Related

Code crashes when displaying image in imageview

Thia code crashes when i took a photo from camera. but when i pick a photo from gallery it works fine. please guide me. I debug this code. the value of data is getting null when i took a picture from camera
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK || requestCode == 200||requestCode==CAMERA_REQUEST) {
Glide.with(this).load(data.getData()).into(mDishUploadImg);
Bitmap bm = null;
try {
bm = MediaStore.Images.Media.getBitmap(mainActivity.getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 50, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
base64img = Base64.encodeToString(b, Base64.NO_WRAP);
}
}
Try Adding data!=null && data.getData() != null into your if Statement and also instead of using || use &&.

Bad photo quality captured from camera or gallery

I have a problem in my app:
i must get photo from camera or gallery, show it in a small imageView and sent it to server in base64 format (i don't need save image as file in the phone).
i can take photo but with very bad quality!
The solutions found in internet don't work for me
Can you help me?
here is the code
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case 0:
if(resultCode == RESULT_OK){
bitmap_photo = (Bitmap) imageReturnedIntent.getExtras().get("data");
Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap_photo,
70, 70, true);
userImage.setImageBitmap(newBitmap);
bitmap_photo = newBitmap;
}
break;
case 1:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
userImage.setImageURI(selectedImage);
try {
bitmap_photo = MediaStore.Images.Media.getBitmap(this.getContentResolver(),selectedImage);
Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap_photo,
70, 70, true);
bitmap_photo = newBitmap;
} catch (IOException e) {
e.printStackTrace();
}
}
break;
}
}
and this is the code to convert image to base64:
public String getBase64Image(){
if(bitmap_photo!=null){
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap_photo.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
String base64image = Base64.encodeToString(byteArray, Base64.NO_WRAP);
return base64image;
} else {
return "";
}
}
thank you!

Android convert Bitmap to Base64 string issue

i have a problem with converting Bitmap to Base64. I want to post some images from camera or gallery. When i get them as image and i must convert to base64 but when i'm getting the string and testing with http://codebeautify.org/base64-to-image-converter to see my image. Result looks very bad quality. How can i fix this pls help me ?
Here is my code:
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode)
{
case 0:
if(resultCode == RESULT_OK)
{
Bundle extras = imageReturnedIntent.getExtras();
Bitmap selectedImage = (Bitmap)extras.get("data");
secilenImage.setImageBitmap(selectedImage);
String encodedImage;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try
{
selectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] b = stream.toByteArray();
encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
images.add(i, encodedImage);
Log.d("StringForCamera: ", encodedImage);
stream.close();
}
catch(IOException e)
{
e.printStackTrace();
}
secilenImage.setImageBitmap(selectedImage);
secilenImage.setEnabled(false);
}
break;
case 1:
if(resultCode == RESULT_OK)
{
Uri imageUri = imageReturnedIntent.getData();
Bitmap selectedImage = null;
String encodedImage;
try
{
selectedImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream();
selectedImage.compress(Bitmap.CompressFormat.PNG, 100, byteArrayBitmapStream);
byte[] b = byteArrayBitmapStream.toByteArray();
encodedImage = Base64.encodeToString(b, Base64.NO_WRAP);
images.add(i, encodedImage);
Log.d("StringForGallery: ",encodedImage);
}
catch(IOException e)
{
e.printStackTrace();
}
secilenImage.setImageBitmap(selectedImage);
secilenImage.setEnabled(false);
}
break;
}
++i;
}
(Bitmap)extras.get("data");
That is only a thumbnail of the image. Hence the low quality.

Data not return from Google Photo Intent android

I have been trying to load an image from Google Photos App into my application. When I choose an image in Google Photos, my application crashes, but if I select the image from the Gallery App, it works. Please what am I missing out? Find my code below.
itemImage.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
pickImage();
}
});
public void pickImage() {
// 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);
galleryIntent.putExtra("crop", "true");
galleryIntent.putExtra("outputX", 100);
galleryIntent.putExtra("outputY", 100);
galleryIntent.putExtra("scale", true);
galleryIntent.putExtra("return-data", true);
// Start the Intent
startActivityForResult(galleryIntent, 1);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {
try {
//Uri uri = data.getData();
Bitmap bmp = (Bitmap) data.getExtras().get("data");
itemImage.setImageBitmap(bmp);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String encodedImageString = Base64.encodeToString(b, Base64.DEFAULT);
byte[] bytarray = Base64.decode(encodedImageString, Base64.DEFAULT);
bmimage = BitmapFactory.decodeByteArray(bytarray, 0,
bytarray.length);
} catch (Exception e){}
}
}
I have tweaked the code a little bit. Now I can import all images on the Gallery and Photos app except for camera images. See my new code below:
public void pickImage() {
// Create intent to Open Image applications like Gallery, Google Photos
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, 1);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {
try {
//Uri uri = data.getData();
final Uri uri = data.getData();
final InputStream imageStream = getActivity().getContentResolver().openInputStream(uri);
final Bitmap bmp = BitmapFactory.decodeStream(imageStream);
itemImage.setImageBitmap(bmp);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String encodedImageString = Base64.encodeToString(b, Base64.DEFAULT);
byte[] bytarray = Base64.decode(encodedImageString, Base64.DEFAULT);
/*bmimage = BitmapFactory.decodeByteArray(bytarray, 0,
bytarray.length);*/
} catch (Exception e){}
}
}

how to send from onactivityresult uri path to another activity and change it to image

In the below code I am getting an image from sd card , and sending the image through an intent,
however I want to change the code to send the path of the image , and not the image itself
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {
//file name
filePath = data.getData();
try {
// Bundle extras2 = data.getExtras();
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
byte imageInByte[] = stream.toByteArray();
Intent i = new Intent(this, AddImage.class);
i.putExtra("image", imageInByte);
startActivity(i);
} catch (IOException e) {
e.printStackTrace(); } } }
and here I am receiving the image
byte[] byteArray = getIntent().getByteArrayExtra("image");
// encodedImage = Base64.encodeToString(byteArray, Base64);
bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView imageview = (ImageView) findViewById(R.id.imageView);
imageview.setImageBitmap(bmp);
Also I tried this but it didn't work:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
Intent i = new Intent(this, AddImage.class);
i.putExtra("imagepath", filePath);
startActivity(i);
} catch (IOException e) {
e.printStackTrace(); } }}
but how to receive the uri path
geturi().getintent() ?
and then get the image
edit--
I got error nullpointerexception uri string here
img.setImageURI(Uri.parse(imagePath ));
this is the code
String imagePath = getIntent().getStringExtra("imagePath");
ImageView img=(ImageView) findViewById(R.id.imageView);
img.setImageURI(Uri.parse(imagePath ));
Bitmap bitmap = ((BitmapDrawable)img.getDrawable()).getBitmap();
Bitmap out = Bitmap.createScaledBitmap(bitmap, 500, 500, false);
// bitmap is the image
ByteArrayOutputStream stream = new ByteArrayOutputStream();
out.compress(Bitmap.CompressFormat.JPEG, 60, stream);
out.recycle();
/* byte[] byteArray = getIntent().getByteArrayExtra("image");
// encodedImage = Base64.encodeToString(byteArray, Base64);
// bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);*/
// ImageView imageview = (ImageView) findViewById(R.id.imageView);
img.setImageBitmap(out);
i am sending intent like this
filePath = data.getData();
Intent i = new Intent(this,
AddImage.class);
i.putExtra("imagepath", filePath);
startActivity(i);
AddImage Activity
onCreate(...)
{
....
String imagePath = getIntent().getStringExtra("imagePath");
ImageView img=(ImageView) findViewById(R.id.imageView1);
img.setImageURI(Uri.parse(imagePath ));
Bitmap bitmap = ((BitmapDrawable)img.getDrawable()).getBitmap();
Bitmap out = Bitmap.createScaledBitmap(bitmap, 500, 500, false);
// bitmap is the image
ByteArrayOutputStream stream = new ByteArrayOutputStream();
out.compress(Bitmap.CompressFormat.JPEG, 60, stream);
img.setImageBitmap(out);
bitmap.recycle();
....
}

Categories

Resources