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 &&.
Related
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);
}
}
I've been developed app to take a picture to be saved in gallery. I've searched online and the easiest way that I could practice was to use data.getExtras().get("data") . So below are the code to take picture from camera. Note that I'm using fragment in this class.
img22a1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,CAMERA_01);
}
});
Get the captured image and convert it to string
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_01) {
if (resultCode == Activity.RESULT_OK) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream output = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output);
byte[] byte_arr = output.toByteArray();
((SiteProgress) getActivity().getApplication()).seti22a1(Base64.encodeToString(byte_arr, Base64.DEFAULT));
BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap);
img22a1.setBackgroundDrawable(ob);
} else if (resultCode == Activity.RESULT_CANCELED) {
}
}
}
I save the string into global variable so another activity can access it.
In another activity, I have a button to save the image into folder/gallery by accessing the image string.
for(int i=0;i<=namefile.length;i++){
Bitmap[] bitmap = new Bitmap[namefile.length];
FileOutputStream outputStream = new FileOutputStream(String.valueOf(namefile[i]));
bitmap[i] = BitmapFactory.decodeByteArray(decodedString[i],0,decodedString[i].length);
bitmap[i].compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.close();
MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), namefile[i].getAbsolutePath(), namefile[i].getName(), namefile[i].getName());
}
It worked to save the image into folder/gallery....But the quality of image was very low...I could barely see text in the image.
They said data.getExtras().get("data") supposed to return thumbnail and not the actual image. What I want here is to get the full image and not the thumbnail one.
Any answer will be very appreciated.
From: Android - How to get the image using Intent data
if(data != null)
{
Uri selectedImageUri = data.getData();
filestring = selectedImageUri.getPath();
Bitmap thumbnail = BitmapFactory.decodeFile(filestring, options2);
System.out.println(String.format("Bitmap(CAMERA_IMAGES_REQUEST): %s", thumbnail));
System.out.println(String.format("cap_image(CAMERA_IMAGES_REQUEST): %s", cap_image));
cap_image.setImageBitmap(thumbnail);
}
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){}
}
}
I understand this question has been asked before and I have researched possible solutions with no luck so here it goes. I am trying to save an cropped image into a sqlite database. I am using the Android-Image-Cropper cropper library as follows in my onActivityResult() method:
Everything works except the part where I am trying to save the image as a bitmap. I keep getting null. I can however populate my ImageView with the line
ivTroopPhoto.setImageURI(result.getUri());
If anyone has used this API an can help me out with this issue.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if ((requestCode == PIC_FROM_CAMERA) || (requestCode == PIC_FROM_GALLERY)) {
if (resultCode == RESULT_OK) {
Uri picUri = data.getData();
CropImage.activity(picUri)
.setGuidelines(CropImageView.Guidelines.ON)
.setFixAspectRatio(true)
.setAspectRatio(1, 1)
.start(this);
}
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
ibCameraIcon.setVisibility(View.GONE);
ivTroopPhoto.setVisibility(View.VISIBLE);
ivTroopPhoto.setImageURI(result.getUri());
tvTroopPhoto.setText(R.string.change_photo);
Log.d("dozer74", "==============================> Image Uri: " + result.getUri().getPath());
//Uri imageUir = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), result.getUri());
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
Log.d("dozer74", "==========> Encoded Base64 Image: " + encoded);
} catch (IOException e) {
e.printStackTrace();
}
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Toast.makeText(this, String.format("Cropping failed: %s", result.getError()), Toast.LENGTH_LONG).show();
}
}
}
I was able to work it out. I used the api's imageview and was able to get the image as a bitmap.
What i am having: i am using a camera to capture a image and i want to display it in a listview by passing bitmap to adapter in android
private void startCameraCapture() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(cameraIntent, 1024);
}
}
public void onActivityResult(int requestCode, int resultCode, Intent myData) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == 1024) {
.............. mycode
}
}
What i want to do :: i want to convert the data i received as myData into a bitmap in android
Convert byte[] to bitmap
Bitmap bmp = myData.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();