I need to pick an image from gallery and then convert it into byte data. I know how to pick image from gallery. Also I know how to convert image to byte data. But problem is i convert image that are in drawable but now I need to pick it from gallery and convert it to byte code. Any help
THanks
In onClick function I am using this code to pick image from gallery
Intent image = new Intent(Intent.ACTION_GET_CONTENT);
image.setType("Image/*");
startActivityForResult(image, 0);
And I have used following code to convert image that is in drawable to byte data.
bm = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
data = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 40 , data);
bitmapdata = data.toByteArray();
Now how would i convert image from gallery to byte data.
Thanks
In onActivityResult you will receive the Uri to your selected image like this:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PICK_IMAGE && data != null && data.getData() != null){
Uri imageUri = data.getData();
//....
}
}
Then to retrieve it from the MediaStore you should use :
Bitmap bitmap =
MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
after that, you should process the Bitmap like you do it now.
Related
My app contains a section where I have to load images from gallery and show them there. Please guide me how I can do that. I also want to make folders in which the photos will be placed. Thanks in advance for your help!
Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery,PICK_IMAGE);
I've tried the above code it selects the image and shows it on my app but when I restart my app the image disappears.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
imageUri = data.getData();
imageView.setImageURI(imageUri);
}
}
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
Bitmap newPicture;
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inPurgeable = true;
newPicture = BitmapFactory.decodeStream(imageStream);
myPicturesArrayList.add(newPicture);
picturesAdapter = new MyPicturesAdapter(myPicturesArrayList); //Your custom adapter
myRecyclerView.setAdapter(picturesAdapter );
In that case you have to save the images first in your database locally or on server...and next time fetch from database
After you load the image from gallery, save the image Uri in SharedPreferences. Next time load it directly (Get the uri from the Sharedpreference).
For details pls refer Android Shared preferences example
Project: Open Gallery From a Button in One Activity, Select The Images from there and Display it on the initial Activity screen.
Solution: I know that the solution for the problem above is given but it only deals with the multiple selection of images part and getting the data of the images.
My Problem:
From here, i am unable to use the uri data to convert to bitmap and hence store in my image view. I even tried
ImageView imageView = findViewById(R.id.imageView);
Bitmap bitmap = BitmapFactory.decodeFile(imageUri.getPath()) ;
imageView.setImageBitmap(bitmap); but the image doesn't show
How to get dynamic number of image views?
I want the image views on my activity.xml to be dynamic,i.e it changes its number depending on the number of selections
for eg: if the user selects 5 images on opening the gallery app, then i want to be able to display the 4 images on the initial Activity screen. If 6 images selected then 6, if 2 then 2. It should change the image view count depending on the number of selections as I dont want to hardcode the number of image views in the xml file.
How i am trying to get the bitmap is by
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
I am getting Unhandled exceptions error , java.io.FileNotFoundException for the getBitmap part below
I have attached my code below
public void onClick(View view){
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), PICK_IMAGE_MULTIPLE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_IMAGE_MULTIPLE) {
if(resultCode == Activity.RESULT_OK) {
if(data.getClipData() != null) {
int count = data.getClipData().getItemCount(); //evaluate the count before the for loop --- otherwise, the count is evaluated every loop.
for (int i = 0; i < count; i++){
Uri imageUri = data.getClipData().getItemAt(i).getUri();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri) ; //MAIN ERROR OVER HERE
//OR
ImageView imageView = findViewById(R.id.imageView);
Bitmap bitmap = BitmapFactory.decodeFile(imageUri.getPath()) ;
imageView.setImageBitmap(bitmap); //STILL NOT WORKING
}
}
} else if(data.getData() != null) {
String imagePath = data.getData().getPath();
ImageView imageView = findViewById(R.id.imageView); //WANT TO AVOID THIS AND MAKE IMAGE VIEW NUMBER AS DYNAMIC
Bitmap bmImg = BitmapFactory.decodeFile(imagePath);
imageView.setImageBitmap(bmImg);
}
}
}
InputStream iStream = getContentResolver().openInputStream(selectedImageUri);
byte[] inputData = Utils.getBytes(iStream);
dbHelper.insertImage(inputData);
http://www.coderzheaven.com/2012/12/23/store-image-android-sqlite-retrieve-it/
I am trying to take a picture with the camera of "Nexus 5X API 26" and show it in ImageView field, before uploading it to Firebase.
First problem is that after taking a picture, it does not show up in imageView. I am doing the following:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, CAMERA_REQUEST_CODE);}
And then I was trying to show the picture in the same way I do for the pictures taken from Gallery:
filePath = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
The part which I do not understand is how data.getData() works in both cases (for Gallery and for Camera)?
I guess that the uploadImage() method, should be the same for both Gallery and Camera uploads (it works for Gallery already so...).
So the thing that I am currently missing is that I am not getting the filePath, I guess?
Is it necessary to "temporarily save" the camera's picture in order to .getData()? Or it can work without any kind of "saving"?
I just want the user to take a picture and it should be sent to Firebase. Not necessary for user to see it in imageView first, just to get the uri (data) in order to upload it.
view when i open the camera
view after taking a picture
This will help you.
Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,7);
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
bitmap= (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream baos=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);
byte[] imageAsBytes = Base64.decode(imageEncoded.getBytes(), Base64.DEFAULT);
InputStream is=new ByteArrayInputStream(imageAsBytes);
bitmap1=BitmapFactory.decodeStream(is);
img.setImageBitmap(bitmap1);
}
This question already has answers here:
Android Camera Intent: how to get full sized photo?
(8 answers)
Closed 5 years ago.
I am currently doing a fault reporting page whereby the end user is able to report an image either by capturing using the camera or selecting a photo from the gallery. I am not sure why but when the image is taken using the camera, the height and width returned is about 220 by 180 which is a very blur image. I have tried looking online for other tutorials but my code seems to be the same as the others. Not too sure if it is my phone or what.
My code for Camera Intent:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
My code after Capturing image from Camera:
thumbnail = (Bitmap) data.getExtras().get("data");
bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
ivImage.setVisibility(View.VISIBLE);
ivImage.setImageBitmap(thumbnail);
I've tried getting the width and height before and after compression, both returns the same value.
May be you should not compress the image rather simply use BitmapFactory.decodeFile(filePath) or any suitable method for getting your image from file into a bitmap (if not done already) and then set the bitmap object to the ImageResource like this:
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
mImageView.setImageBitmap(bitmap);
And don't simply type cast the data received from intent. As far as I know about camera activity class, it will return a URI of the captured image. Try to receive it in onActivityResult() like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) return;
if (requestCode == REQUEST_CAMERA) {
Uri photoUri = data.getData();
// Calculate the bitmap here according to the width of the device
((ImageView) findViewById(R.id.captured_image)).setImageBitmap(bitmap);
}
super.onActivityResult(requestCode, resultCode, data);
}
I wanto to load an image into an ImageView on android. Now, i know that if the image is too large, i have to scale it so that it doesn't take too much memory.
I want to do that doint what it says here: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
Now, the way i get the image is via it's URI like this:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"text"),1);
protected void onActivityResult(int reqCode, int resCode, Intent data){
super.onActivityResult(reqCode, resCode, data);
if (reqCode == 1 && resCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
}
}
And if i want to use some decode*() method, i cannot do that with the URI. So my frist choice was to use:
decodeFile(imageUri.getPath(),options);
But when i do that, the path i get from the gallery are something like: /documents/image:9023
How should i do this?
I just need to open the gallery and get and image from displaying and also be able to save it as a bitmap so i can edit it.
Even I was stuck with similar problem few days before,
1.) First convert your Uri to string
String SelectedImageString= selectedImage.toString();
2.)once you will get String path of image you can convert it to bitmap and also resize it using bitmapFactory options.
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap bitmap;
bitmap=BitmapFactory.decodeFile(SelectedImageString, options);
imageView.setImageBitmap(bitmap);
It worked for me I hope it will help you.
And if i want to use some decode*() method, i cannot do that with the URI
Sure you can. Call openInputStream() on ContentResolver, and pass that InputStream to the decodeStream() method on BitmapFactory.
A Uri is not necessarily a file, so do not attempt to treat it as one.