I'm new to android and I'm developing an app which uses gallery and image picking from gallery, but I'm getting an error which I have tried to solve in different way,as
I'm using gallery located on SD card when I pick the horizontal image (width greater than height) it loads the image in image view perfectly fine but the problem is with vertical image (height greater than width) it show the image in image view rotated on the left side(or saying -90 degree from original position). I have printed the width and height of the both images (horizontal and vertical) its show width=2592 and height=1936 for both images,
I'm really struck here and haven't able to solve it for about 3 weeks please anyone help me.
Thanks in advance
here is my code:
for starting gallery intent
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
after selecting Image:
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent)
{
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
try{ 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]);
filePath = cursor.getString(columnIndex);
cursor.close();
File img = new File(filePath); Bitmap yourSelectedImage = BitmapFactory.decodeStream(new FileInputStream(img));
int imgHeight = yourSelectedImage.getHeight(); int imgWidth = yourSelectedImage.getWidth();
Toast.makeText(getApplicationContext(), "Original size: Width ="+imgWidth+" Height="+imgHeight, Toast.LENGTH_LONG).show();
float myScalingFactor = MyScaleFactor(imgHeight,imgWidth);
int destW = (int)(imgWidth*myScalingFactor);
int destH = (int)(imgHeight*myScalingFactor);
Toast.makeText(getApplicationContext(), "Scaled size: Width ="+destW+" Height="+destH + "Scale" +myScalingFactor, Toast.LENGTH_LONG).show();
yourSelectedImage = Bitmap.createScaledBitmap(yourSelectedImage, destW, destH, true);
picUpload.setImageBitmap(yourSelectedImage);
}
I wrote an article about this. It depends on a library I made but you can get most of the functionality using the code that's in the article itself. Here's a summary...
Proper way to launch the activity. Basically, if someone quits the app while in the picking from the gallery and then returns to it 2 hours later via the launcher icon. It's disorienting to bring them back to the Gallery since they won't understand or remember why they were there.
final Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
activity.startActivityForResult(intent, requestCode);
Retrieve the result...
final InputStream is = context.getContentResolver().openInputStream(intent.getData());
final Bitmap imageData = BitmapFactory.decodeStream(is, null, options);
is.close();
This code wraps handles more cases than that DB lookup stuff can. It's also cleaner.
That'll get you most bang for the buck and might even be enough for production code. I go into more details like the perils of loading very large images and how to handle them using BitmapFactory.Options.inJustDecodeBounds.
When you load the bitmap, you have to take orientation of the photo into consideration. That being said, you have to get exif information from image file or via content resolver.
You can check the rotate image part as described in https://gist.github.com/Mariovc/f06e70ebe8ca52fbbbe2 or check out my library droid-image-picker for selecting/croping images.
Related
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.
I want to upload images from gallery to an apk. How can I attach images without web services?
Should I use sqlite for this or content provider ??
Once the image attached I want it to show in image viewer.
Ok first some things. The images you see in gallery are stored in phone memory or in a SDCard. The images you store in the assets folders inside your app (resources folder in eclipse) are for internal app use, when the apk is build you can't put nothing in there. What you want is associate some image with your app when it is running so there is some ways to do that you just need to pick the fits you.
First you need to open the Image Gallery from your app by Intent, something like this:
//Constant to compare result
private static int RESULT_LOAD_IMAGE = 1;
//Intent that call the gallery
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//Start another activity that returns a result to yours
startActivityForResult(i, RESULT_LOAD_IMAGE);
//Override the callback to get the result for the activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//If everythig ok and a image is selected
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
//Get the image path
Uri selectedImage = data.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 picturePath = cursor.getString(columnIndex);
cursor.close();
//Show the image in your ImageView
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
Ok now that you have your image you want to decide what to do with it and that's the "upload in my apk" I think you want. The image you have now is in memory phone or SDCard and now you have the path to that image. If you want to duplicate it and store it in your you app you can copy the image to your app private folder, change the image name and store the new path the way you want (sqlite, sharedPrefs, change the name to some logic you can get the image name, etc.) and now the image is "uploaded" to your app. To get the app private folder use Context.getFilesDir(). But again you have the image file already, just do what you want with it. And be careful with permissions. The other ways are web services to store images in a server, sqlite to store the image in a database.
Hello !! I am trying to write a very simple android program. When I press the share button on a selected image from the image gallery a menu pops up and tells me to choose between my application and the messaging application to open the image with. When I choose my application I want it to view the image in an image view. For the part that I already done is the implicit intent when I press the share button it pops up a menu for me to choose which application I want to use. The second part is the tricky part since the image is saved on the SD Card of the phone how do I access that particular image and how do I view it in the image view ? And I am using the Android Emulator.
first create an emulator having sdcard then how to save image in emulator
for picking an image from sdcard u can follow this tutorial
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();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
yourImageView.setImageBitmap(yourSelectedImage);
}
}
}
for showing that image in ur imageview use this
ImageView img=(ImageView) findViewById(R.id.ur_imageview);
Bitmap bmp = BitmapFactory.decodeFile(filePath);
img.setImageBitmap(bmp);
I want a functionality in my app in which the user could select a background(.png/.jpg file) from the phone's memory. I guess 80% of my task has been completed by using this link http://www.dreamincode.net/forums/topic/190013-creating-simple-file-chooser/
The code from the above link just shows the view of SD card and now I want to set any image file present there as my app background. How can i achieve this?
If you want just select image and set background, I think the simpliest way is something like this:
.....
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, SELECT_PICTURE_ACTIVITY_REQUEST_CODE);
....
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case SELECT_PICTURE_ACTIVITY_REQUEST_CODE:
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
setBackgroundDrawable(new BitmapDrawable(bitmap));
}
cursor.close();
}
break;
}
}
Depending on your app, a FrameLayout around every existing layout, begun with an ImageView that you set appropriately.
If you want to do it dynamically use
setBackgroundColor(int color)
or
setBackgroundResource(int resourceID)
on your parentview of your activity.
Yuo can try this:
Bitmap srcBitmap = BitmapFactory.decodeStream(new FileInputStream(new File("path/to/file.jpg")));
BitmapDrawable bmpDrawable = new BitmapDrawable(srcBitmap);
YourView.setBackgroundDrawable(bmpDrawable);
I suppose your problem is that you don't know how to load image picked from a gallery. #appserv's answer solves your problem, but if you need to load images from any source (including your app resources), look at how it is implemented in ImageView
WHAT I HAVE DONE NOW:
i am picking images on my app with android, and showing them on a ImageView of my layout, and sometimes i got an exception java.lang.OutOfMemoryError: bitmap size exceeds VM budget. This happens when the photos are higher than 500kb.
WHAT I NEED TO DO (AND I DONT KNOW HOW TO DO IT):
then, what i want to do is to allow the user only to select photos of maximum 500kb. And if the user selects a photo Higher than 500kb, then, my app should show a toast telling the user that only can select photos of max 500kb.... ¿how to do it?
this is my code:
changeImageButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case 1:
{
setResult(1);
finish();
}
case ACTIVITY_SELECT_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();
selectedPhoto = BitmapFactory.decodeFile(filePath);
//profileImage.setImageBitmap(selectedPhoto);
profileImage.setImageBitmap(Bitmap.createScaledBitmap(selectedPhoto, 80, 80, false));
}
}
}
profileImage is a ImageView of my layout. and i use scaled butmap to resice the image to 80x80
please give me some help to do that, im searching on google and i can't find nothing
Can't you just use some standard Java to get the file size, then check against your limit?
private long getFileSize(String path) {
File file = new File(path);
if (file.exists() && file.isFile()) {
return file.length();
} else {
return 0L;
}
}
Why do you hate your users so much? You are asking the wrong question... you don't need to cap the file size at all. It's the decompressed image size that's the problem, and you can control that.
Use the version of BitmapFactory.decodeFile() which takes a BitmapFactory.Options parameter, and then set the inSampleSize field of the options parameter to an appropriate value (2 or 4 or something). You may even want to use inJustDecodeBounds first to work out a reasonable value.