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);
Related
I'm trying to add Pick image with Crop feature by aspect 1:4, it's 2000 width, 500 height => 1:4, and i want the selected image by user to replace an image located in drawable/custom_image.jpg.
Note : the image "custom_image" isn't showed as imageview in the app.
I've searched Stackoverflow, and found the ways, but i'm having a problem with cropping it & replacing that selected image with "R.drawable.custom_image".
EDIT : Forgot the cropping, got problems in it, i just need the replace drawable code in OnActivityResult.
I've created a button, when pressed it calls a method, here's the code :
Button mSelectbutton = (Button) view.findViewById(R.id.selectimage);
mSelectbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SelectImageActivity(v);
}
});
SelectImageActivity :
public void SelectImageActivity(View v) {
Intent selectintent = new Intent();
// Show only images, no videos or anything else
selectintent.setType("image/*");
selectintent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(selectintent, "Select Picture"), PICK_IMAGE_REQUEST);
}
And here's the method :
OnActivityResult Code :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && null != data) {
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();
Drawable imageView = (Drawable)findViewById(R.drawable.custom_header);
imageView.setImageDrawable(getDrawable(R.drawable.custom_header);
}
}
I have used this library to tackle this problem because keeping outputx etc.. is not working for some versions of android:
Add this library to gradle:
//image cropping library to show as square for android older versions to work
compile 'com.soundcloud.android:android-crop:1.0.1#aar'
this method performs cropping:
private void performCrop(Uri mImageCaptureUri) {
//this creates a new image file comment below if you want file creation code
Uri destinationUri = MediaUtils.getOutputMediaFileUri(GlobalConstants.CREATE_IMAGE);
Crop.of(mImageCaptureUri,destinationUri).asSquare().start(getActivity(),this);
}
In your on activity result you can get Cropped image catch it:
if(requestCode == Crop.REQUEST_CROP) {
Uri outPutImageUri = Crop.getOutput(data);
Comment below if you want any futher info
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.
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
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.
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.