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.
Related
I've written code in Android to capture an image and then use that image as follows:
private static Intent i;
final static int cons = 0;
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
public void takePicture()
{
i= new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //Open camera
startActivityForResult(i, cons);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Log.d("Testing if condition", "Test"); //This code does not execute
Bundle ext = data.getExtras();
Log.d("Upload image", ext.toString());
sendForUpload(data); // function to upload file to server
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
}
This code lets me take a photo and it saves to the SD card. However, I'm not sure if the file is sent to the sendForUpload function where I've handled getting the path and uploading the file. In fact nothing inside the if (resultCode == RESULT_OK) block works. How do I use the image captured from this activity?
Well, you have a few problems.
First, you are calling startActivityForResult(i, cons);. cons is 0. You are trying to compare 0 with CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE. Since CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE is 100, that will not work.
Second, you are calling data.getExtras().toString() (split over two Java statements). I would expect this to return a value like android.os.Bundle#34334143. If that is what you want to upload, fine. I am guessing that you want to upload the image. Since you did not include EXTRA_OUTPUT in your ACTION_IMAGE_CAPTURE Intent, you get a Bitmap of the thumbnail image taken by the camera via data.getParcelableExtra("data").
Third, you are making inappropriate assumptions:
This code lets me take a photo and it saves to the SD card.
There is no requirement for the camera app to save the image anywhere, let alone to removable storage. In fact, I would argue that this is a bug in your camera app. That is not surprising, as ACTION_IMAGE_CAPTURE implementations have bugs.
where I've handled getting the path and uploading the file
There is no path. You did not include EXTRA_OUTPUT in your ACTION_IMAGE_CAPTURE Intent, and so all you get back is the thumbnail Bitmap.
I'm using this to put the image in a ImageView:
Uri uri = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
You can use bitmap or uri objects for upload or show your picture..
Hope it helps you.
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 have a problem. I want to make file uploader app. I'm using Intent.ACTION_GET_CONTENT to pick any file from my device. I am receiving the file in onActivityResult like that:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CHOOSER:
if (resultCode == RESULT_OK) {
final Uri uri = data.getData();
File file = FileUtils.getFile(uri);
String fileName = file.getName();
String filePath = file.getPath();
int fileSize = Integer.parseInt(String.valueOf(file.length()/1024));
tvName.setText(fileName);
tvPath.setText(filePath);
tvSize.setText(String.valueOf(fileSize) + "kB");
}
}
}
I want to show user information about picked file. In general everything is fine, but when I choose Image from gallery then:
The file name shows no format - it's a number (probably reference to file in memory). When I pick Image via installed file explore I get sth like imagename.png etc, but picked from gallery is like "195493"
File.length created from data picked from gallery is 0.
Is there any way to access real image name and size after picking image from Gallery via intent?
Just input URI from the intent and get the size of any file
uri = data.getData();
Cursor returnCursor = getContentResolver().query(uri, null, null, null, null);
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
returnCursor.moveToFirst();
Log.e("TAG", "Name:" + returnCursor.getString(nameIndex));
Log.e("TAG","Size: "+Long.toString(returnCursor.getLong(sizeIndex)));
I think this will help you.
When picking images from the gallery, you don't get a reference to the actual file but to a row in the database. You have to retrieve the actual file path with a Cursor like in this answer.
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.