My app is crashed while adding large size images from my device gallery to my app. I had gone through some solutions like resizing, scaling etc but it didn't work out.
Following code part is executed when I select an image from gallery
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case REQ_CODE_PICK_IMAGE:
if(resultCode == RESULT_OK){
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);
cursor.close();
//Convert Bitmap to Byte Array:-
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
//Pass byte array into intent:-
Intent intent = new Intent(UserAccount.this,RetrieveImage.class);
intent.putExtra("picture", byteArray);
startActivity(intent);
} } }
RetrieveImage.java
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView img = (ImageView) findViewById(R.id.imageView1);
img.setImageBitmap(bmp);
How can I solve this problem.
Use this for Scaling :
Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath),
THUMBSIZE, THUMBSIZE);
InputStream stream = context.getContentResolver().openInputStream(imageUri);
stream = ctx.getContentResolver().openInputStream(imageUri);
Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options);
stream.close();
Use above to get bitmap image from image uri create your options.
Try this code
File f = new File("getfile from media");
if(f.exists())
{
FileInputStream is = null;
try {
is = new FileInputStream(f);
} catch (FileNotFoundException e) {
Log.d("error: ", String.format(
"ShowPicture.java file[%s]Not Found", fileName));
return;
}
Bitmap bm = BitmapFactory.decodeStream(is, null, null);
pic.setImageBitmap(bm);
}
Related
In my Android App, I have an option where a user can take his photograph or upload an image from his Gallery at the time of registration. This image, once successfully taken by camera or selected from Gallery, is shown in an ImageView. Below is the onClick of ImageView code that starts this process.
This is from Activity Layout.
<ImageView
android:id="#+id/imgUserImage"
android:layout_width="150dp"
android:layout_height="150dp"
android:scaleType="centerCrop"
android:onClick="changePicture"
app:srcCompat="#drawable/profile_image_not_provided"
android:layout_below="#+id/textView2"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp" />
This is from Activity.java
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
ImageView imgProfilePicture = (ImageView)findViewById(R.id.imgUserImage);
int exif_orn_a;
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bitmap mphoto = (Bitmap) data.getExtras().get("data");
Uri selectedImage = data.getData();
imgProfilePicture.setImageURI(selectedImage);
imgProfilePicture.setImageBitmap(mphoto);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
mphoto.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bitmap is required image which have to send in Bitmap form
byte[] byteArray = baos.toByteArray();
encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
}
else if (requestCode == PICK_IMAGE_GALLERY) {
Uri selectedImage = data.getData();
String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
Bitmap lphoto = null;
Cursor cur = this.getContentResolver().query(selectedImage, orientationColumn, null, null, null);
int orientation = -1;
if (cur != null && cur.moveToFirst()) {
orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
Toast.makeText(Register.this, "Picture Orientation "+orientation, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(Register.this, "Wrong Orientation", Toast.LENGTH_LONG).show();
}
if (cur != null) cur.close();
try {
lphoto = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
imgProfilePicture.setImageURI(selectedImage);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
lphoto.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bitmap is required image which have to send in Bitmap form
byte[] byteArray = baos.toByteArray();
encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
}
}
I have managed to get the Orientations properly. Now the problem is, how should I rotate the image before imgProfilePicture.setImageURI(selectedImage);.
I managed to find the solution. Please feel free to post a better solution, if you think this can be improvised.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
ImageView imgProfilePicture = (ImageView)findViewById(R.id.imgUserImage);
int exif_orn_a;
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bitmap mphoto = (Bitmap) data.getExtras().get("data");
Uri selectedImage = data.getData();
imgProfilePicture.setImageURI(selectedImage);
imgProfilePicture.setImageBitmap(mphoto);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
mphoto.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bitmap is required image which have to send in Bitmap form
byte[] byteArray = baos.toByteArray();
encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
}
else if (requestCode == PICK_IMAGE_GALLERY) {
Uri selectedImage = data.getData();
//Starting from here, I am checking the Orientation and storing the result in orientation variable.
String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
Bitmap lphoto = null;
Cursor cur = this.getContentResolver().query(selectedImage, orientationColumn, null, null, null);
int orientation = -1;
if (cur != null && cur.moveToFirst()) {
orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
Toast.makeText(Register.this, "Picture Orientation "+orientation, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(Register.this, "Wrong Orientation", Toast.LENGTH_LONG).show();
}
if (cur != null) cur.close();
//This is the end of Orientation Check.
try {
lphoto = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
imgProfilePicture.setImageURI(selectedImage);
//Based on the Orientation Check result, I rotate the ImageView Image to make it straight.
if (orientation == 270) {
imgProfilePicture.setRotation(-90);
} else if (orientation == 0) {
imgProfilePicture.setRotation(0);
}
//This is the end of Image Rotation based on the Orientation Check Result.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
lphoto.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bitmap is required image which have to send in Bitmap form
byte[] byteArray = baos.toByteArray();
encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
}
}
I'm trying to get an image from the Gallery and send it to a server.
I do get an base64 encoded string but it's a thin line, not the whole image.
For example, I used Motobit to decode this random image. The base64 string I got
works fine. But the encoded string that I get from my app for the same image is really smaller and when you convert into an image it becomes this.
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_profile);
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case SELECT_PHOTO:
if(resultCode == RESULT_OK){
try {
Uri selectedImage = imageReturnedIntent.getData();
String imageStream = getRealPathFromURI(context, selectedImage);
Bitmap bitmap = BitmapFactory.decodeFile(imageStream);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
String encodedString = Base64.encodeToString(byteArray, Base64.DEFAULT);
Log.e(LOG_TAG, encodedString);
}catch (Exception e){
e.printStackTrace();
}
}
}
}
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
What is making my encodedString becomes only a piece of the image? Thanks!
Maybe the string is correct but the Log method has a character limit.
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();
....
}
I'm trying to load an image from the gallery, but toSave.recycle() doesn't work.
I mean when toSave.recycle() is called the screen becomes white, the app doesn't crash but the image is not shown.
Do you have any suggestions?
Here the code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == LOAD_IMAGE_RESULT && resultCode == RESULT_OK && data != null) {
Uri pickedImage = data.getData();
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]));
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
postImage.setImageBitmap(bitmap);
saveImage(bitmap);
cursor.close();
}
}
Here the method that converts bitmap image and save it in Firebase.
public void saveImage(Bitmap toSave){
if (toSave!=null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
toSave.compress(Bitmap.CompressFormat.JPEG, 50, stream);
toSave.recycle();
byte[] byteArray = stream.toByteArray();
String imageString = Base64.encodeToString(byteArray, Base64.DEFAULT);
simplepostRef.child("image").setValue(imageString);
}
Thank you a lot!
bitmap.recycle() is working in your case. You just dont need to recycle the bitmap because you are showing this bitmap in a imageview.Have a look at the docs.
MY SOLUTION: Ok some good answers. This is what I came up with. Not sure to answer my own question or put it here for proper stackoverflowness so if anyone knows please share.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case cameraData:
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
iv.setVisibility(View.VISIBLE);
break;
case SELECT_PICTURE:
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
File imgFile = new File(selectedImagePath);
bmp = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
break;
}
iv.setVisibility(View.VISIBLE);
iv.setImageBitmap(bmp);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 50, stream); // compress
byte[] ba = stream.toByteArray();
image_str = Base64.encodeBytes(ba);
}
}
//////////////////////////////////////////////////////////////////////////////////////////
OK I have a path to picture in my gallery. I want to take that picture and turn it into a bundle so I can 64 encode it to upload to my server. here is my onActivityResult. I have it working from taking a picture with the camera just not getting it from the gallery.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
switch(requestCode){
case cameraData:
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
Log.e("picture","Take Picture");
break;
case SELECT_PICTURE:
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
Log.e("picture",selectedImagePath);
File imgFile = new File(selectedImagePath);
bmp = (Bitmap) BitmapFactory.decodeFile(imgFile.getAbsolutePath());
Bundle extras1 = ((Cursor) imgFile).getExtras();
// bmp = (Bitmap) extras1.get("data");
Log.e("picture","from Gallery");
break;
}
}
}
the base 64 code is not mine its from this site: http://blog.sptechnolab.com/2011/03/09/android/android-upload-image-to-server/
getPath:
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);
}
Why would you want to turn the bitmap into a bundle? And why do you want to turn the file into a base64 String before you upload?
Here and here are examples that upload a file.
http://pastebin.com/3A5gMFsF
Seems to do what you want. Looks like when you get a file URI back you need to dig through some Content Resolvers to actually get the file itself.
Uri photoUri = data.getData();
if (photoUri != null)
{
try {
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
bMap_image = BitmapFactory.decodeFile(filePath);
ImageView img = (ImageView) findViewById(R.id.gallery1);
img.setImageBitmap(bMap_image);
Edit: The only difference I see between this version and your version is that this version doesn't convert the filePath into a File before calling BitMap.Decode. It calls BitmapFactory.decodeFile directly on the file path returned from the content resolover.
You can directly convert the bitmap to base64.
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
byte[] dataToUpload = Base64.encode(bitmapdata , DEFAULT);
To convert the bitmap to Bundle use bundle.putParcelable("dataToUpload", bitmap)
and retireve it using bundle.getParcelable("dataToUpload").