Error while getting image from gallery in android - android

I know this this question was asked a lot but yeah it don't work for me :S
I normally tried to use it but there always error messages.
Here is the thing I tried to import:
public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem, float pMenuItemLocalX, float pMenuItemLocalY)
{
switch(pMenuItem.getID())
{
case BACK:
createMenuChildScene();
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
return true;
}
}
For some reasone the startActivityForResult is red underlined if I change the name of the onActivityResult to on ActivityForResult on the code down here it's still red underlined.
public 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);
}
}
}
Over here the get Content resolver does not work or is undefined :/
I have no Idea what I'm doing wrong :/

You're trying image pick right?
the one you're using is outdated and works only for builds below KitKat.
i suggest you use document provider by google its faster and efficient and doest give OutOfMemory Errors.
https://developer.android.com/guide/topics/providers/document-provider.html
also for your startActivityForResult should work normally, try File->Invalidate Caches and Select Invalidate Caches and Restart it should resolve all the unnecessary errors. Good Luck!

Try this snippet.
In onActivityResult put this code when resultcode == Result_ok
case REQ_CODE_PICK_IMAGE:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
imageview.setImageBitmap(bitmap);
This solves your problem.

Related

Pick image from gallery file path returns NULL

I want to pick an image from the gallery and get it's path. Here is the code I use to open the gallery
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), 1111);
and onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if (resultCode == RESULT_OK) {
Uri selectedImageUri = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImageUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Log.d("TAG", "onActivityResult: " + filePath);
}
}
filePath always is null if I select the image from the default system image picker, although it is working fine if select an image from the gallery app. What is wrong with my code ?
There is no requirement for ACTION_GET_CONTENT to return a Uri from the MediaStore or a Uri that otherwise has a DATA column.
Also note that you do not have access to the DATA column for MediaStore Uri values on Android 10 and above, so "working fine" is only true temporarily.
I want to pick an image from the gallery and get it's path
There is no path. The user could be choosing a piece of content from a cloud storage provider, such as Google Drive.
See also:
Getting the Absolute File Path from Content URI for searched images
Android - Get real path of a .txt file selected from the file explorer
onActivityResult's intent.getPath() doesn't give me the correct filename
private void loadGallery() {
Intent choose = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(choose, PICK_IMAGE_GALLERY);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_GALLERY) {
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
}
}
}
MediaStore.Images.Media.DATA is deprecated from android 10. Though it will work, if you add the following to AndroidManifest.xml
android:requestLegacyExternalStorage="true"
But it is a temporary solution, if you want to read a file and do something with it you should store it to the app-specific temporary storage, that is there for every android application. Using the app-specific storage do not requires any permission from the user.
So you can use the below code, if you already have the Uri of selected file.
val parcelFileDescriptor =
contentResolver.openFileDescriptor(selectedImageUri!!, "r", null) ?: return
val inputStream = FileInputStream(parcelFileDescriptor.fileDescriptor)
val file = File(cacheDir, contentResolver.getFileName(selectedImageUri!!))
val outputStream = FileOutputStream(file)
inputStream.copyTo(outputStream)
And the function to get the file name from the Uri is
fun ContentResolver.getFileName(fileUri: Uri): String {
var name = ""
val returnCursor = this.query(fileUri, null, null, null, null)
if (returnCursor != null) {
val nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
returnCursor.moveToFirst()
name = returnCursor.getString(nameIndex)
returnCursor.close()
}
return name
}
Source: Android Upload File to Server

Take photo on Android tablet not working

I can successfully take a photo on an Android phone but not on a Nexus 10 tablet. The tablet returns a null value even though I took a picture. To initiate the photo take I use the following code:
String fileName = "myimage.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera");
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE );
startActivityForResult( intent, RESULT_LOAD_IMAGE);
And my code on returning from taking the photo looks like this:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Log.d("selectedImage=", String.valueOf(selectedImage));
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
The activity returns a null value for selectedImage when using the Nexus tablet but returns a valid value for the Samsung Galaxy 4. What could be different between the 2 devices and how do I accommodate the differences? I should note that when taking a photo with the tablet, the photo is not placed in my gallery either. Any help is appreciated.

Android show gallery with album

I would like to show an user experience like this:
to let user select images that he want... and then i'll use it.
How can i achieve this?
Thanks!
You can use intent to open default Gallery in Android and let user select image.
final static int RESULT_CHOOSE_IMAGE = 1;
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_CHOOSE_IMAGE);
After user selects image, onActivityResult() will be called which you will have to override as follows,
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == RESULT_CHOOSE_IMAGE && data != null)
{
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 bitmapPath = cursor.getString(columnIndex); // path to user selected image
cursor.close();
// get bitmap from bitmap path
Bitmap bitmap = BitmapFactory.decodeFile(bitmapPath , null);
}
}
Since bitmaps can be big in size, it may cause OutOfMemoryException while decoding from file. Refer this link for info regarding displaying bitmaps effeciently.
Android native gallery doesn't support multiple image selection by default, so you will have to use custom gallery for that purpose. See this link for tutorial .
In a future, try to be more specific in your questions so we can help you in a better way.
With the information you provided, i can just tell you that you could use a GridView to achieve what you want.
GridView Android Developers

BitmapFactory.decodeFile returns exception on Android 4.4 KitKat

I want to display an image using the following code:
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case SELECT_PHOTO:
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();
Log.wtf("M3K", "Above decode");
Bitmap logoBMP = BitmapFactory.decodeFile(filePath);
Log.wtf("M3K", "Below decode");
//Display image on layout
Log.wtf("M3K", "Above display");
logo.setImageBitmap(logoBMP);
Log.wtf("M3K", "Below display");
}
}
}
The issue is on Bitmap logoBMP = BitmapFactory.decodeFile(filePath); where on Android 4.4 (tested on my Nexus 7) it will return a file not found exception, with the reason being EACCES (Permission denied). This works perfectly on an ASUS Transformer Infinity running 4.2 and worked perfectly on my Nexus 7 running 4.3. Does anyone know what has to change for KitKat compatibility?
Note: I get the image URI through the following code:
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
You can try to put the following in your manifestfile:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

How to display the mobile gallery page in a custom view in android? [duplicate]

This question already has answers here:
populate gallery with images from web
(2 answers)
Closed 7 months ago.
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
The above will take us to Gallery application which is there in our device,so we will be getting the Gallery screen which occupies the whole display of our device.But I want to display that Gallery application view in a Small Layout(something like I shown in below picture,instead of EditText,Buttons ,I want to get My device Gallery screen in that small layout).So that I can give user a feeling that he is not moving out of application.
I have seen this in one of the IPAD app (HelloAlbums).
IS it possible to achieve this in android?
I am using the above code to call phone gallery application.
But I want to display the gallery page in a custom view.
Suppose like in a view which I used below.
How can I achieve this?
please suggest
Thanks
try this
public void ChoosePicture(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
{
if (resultCode == RESULT_OK)
{
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);
}catch(Exception e)
{}
}
}// resultCode
}// case 1
}// switch, request code
}

Categories

Resources