import images from gallery into fragment - android

im trying to implement a fragment inside a navigation slider. I need to create a button in 1 of my fragments to import images from my default gallery. I have tried many codes online, but they don't seem to be working.

It depend on android version you are using if you test your app in android 6.0 than you should ask permission at runtime else image is not returned by android.
For Pre-Marshmallow you can use code:
Intent pickIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
startActivityForResult(pickIntent, 0);
and then override method
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, requestCode, data);
try {
// When an Image is picked
if (requestCode == 0 && resultCode == Activity.RESULT_OK && null != data) {
Uri selectedImage = data.getData();
image.setImageBitmap(BitmapFactory.decodeFile(getRealPathFromURI(selectedImage)));
} else
new ShowErrorToast(getActivity(), "Hey! your Android phone is busy");
} catch (Exception e) {
}
}
than use method to get path of image
public String getRealPathFromURI(Uri data) {
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(data
, filePathColumn, null, null, null);
String picturePath = "";
if (cursor != null) {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
}
return picturePath;
}
For Marshmallow you should ask for permission runtime and follow above code

Related

Place a 2D image on the top of the background

I am working on the android application with following requirements
Take images from gallery and display it as background.
This is done as follows:
btn_select.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"SelectPicture"),PICK_IMAGE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == requestCode && 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);
imageView.setImageURI(Uri.parse(picturePath));
cursor.close();
}
2.Place a 2D iamge on the top of the background image.
3.Reposition the image by dragging
4.Scale the image.
Anyone please suggest the ways to complete steps 2,3 and 4.
Thanks in Advance!

Image path from Uri returns null

I'm trying to get the path of the selected file from gallery, but it is returning null and I don't know why. Every code I see uses the same approach, but it doesn't work for me. Here is my code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// LOAD_FILE_REQUEST is a global variable:
// private static final int LOAD_FILE_REQUEST = 1;
if (requestCode == LOAD_FILE_REQUEST && resultCode == RESULT_OK && data != null) {
if(data.getData() == null) {
System.out.println("NULL");
} else {
System.out.println("NOT NULL"); // <--- Printed
}
currImageURI = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(currImageURI, filePathColumn, null, null, null);
if(cursor.moveToFirst()){
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String yourRealPath = cursor.getString(columnIndex);
System.out.println("REAL PATH "+yourRealPath);
} else {
System.out.println("NO ROWS!!!"); // <-- Not printed
}
cursor.close();
}
}
Did you add the following line in your manifest ? =]
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Could you explain a little more what your problem is? Where are you getting a null?
In which Android version are you running this code? From Android 4.4 onwards, the filechooser which opens when you send the intent for picking up an image returns a relative uri, since it shows not only the files that are stored in your device but also the ones stored in the cloud. So, it could be happening that you're getting a relative URI and when you query for it's location on the device you're getting null, since the ContentResolver doesn't have the path of that file.
If that's the case (Actually even if you're not, since you should develop your app with compatibility for Android's new versions) i'd recommend you to use Content Resvolver to open a InputStream to get the file (openInputStream(Uri), since it will allow you to fetch a file from any location (both local and cloud).
I hope it helps :)
Well, here is how i do in my live wallpaper (Noiraude, have a look :P )
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case 100:
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
#SuppressWarnings("unused")
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(
selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
sharedPreferences = getSharedPreferences("NLP_settings", 0);
Editor editor = sharedPreferences.edit();
editor.putString("key_bit", getPath(selectedImage));
editor.commit();
restartThis();
}
}
}
public String getPath(Uri uri) {
// just some safety built in
if (uri == null) {
return null;
}
// try to retrieve the image from the media store first
// this will only work for images selected from gallery
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
// this is our fallback here
return uri.getPath();
}

How to pick photo from gallery in android 4.4

I am new to android. I want to pick image from gallery. For 4.3 and below following code works fine. But not in 4.4. Please help.
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
#Override
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 };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
Looks like they have completely changed the way to pick a photo from the Gallery in Android 4.4, you have to use the Storage Access Framework as described here:
Storage Access Framework

How to have the option of having a user add an image in android

I am developing an android application for the first time and I was wondering how can I have a option of having the user upload an image. Like for example, in a contact manager the user has the option of uploading an image of a contact. I was wondering how can I do that in my android application. Any help would be appreciated.
You can start from scratch..
1)Take a look at INTENT
Android Intent
Then take a look at this blogpost
Image Upload
Hope this helps you
In this one you want to click the image view to get image from sd card.. If you want to change to button then replace the listener from image to button.
int RESULT_LOAD_IMAGE = 1;
image.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
getting image from sd card:
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 };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
image.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}

Unable to select particular images using ACTION_PICK intent

I'm using an intent like this:
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
And in onActivityResult() I have this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK) {
return; // user cancelled
}
Uri imageUri = data.getData();
if (imageUri == null) {
// (code to show error message goes here)
return;
}
// Get image path from media store
String[] filePathColumn = { android.provider.MediaStore.MediaColumns.DATA };
Cursor cursor = this.getContentResolver().query(imageUri, filePathColumn,
null, null, null);
if (cursor == null || !cursor.moveToFirst()) {
// (code to show error message goes here)
return;
}
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String imagePath = cursor.getString(columnIndex);
cursor.close();
if (imagePath == null) {
// error happens here
}
}
When I select images from particular albums like "Posts", "Profile Photos" (see screenshot) I'm unable to get the image path in onActivityResult(). Images from other albums can be selected with no problems.
I've tried adding intent.putExtra("return-data", true) but data.getExtras() returns null in onActivityResult().
There is similar question here, but no one answered it.
Please help!
hops this will helps you ....
ACTIVITYRESULT_CHOOSEPICTURE is the int you use when calling startActivity(intent, requestCode);
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == ACTIVITYRESULT_CHOOSEPICTURE) {
BitmapFactory.Options options = new BitmapFactory.Options();
final InputStream ist = ontext.getContentResolver().openInputStream(intent.getData());
final Bitmap bitmap = BitmapFactory.decodeStream(ist, null, options);
ist.close();
}
}
if above code doesn't work than just refer this link... it will surly shows the way
http://dimitar.me/how-to-get-picasa-images-using-the-image-picker-on-android-devices-running-any-os-version/
try this:
String selectedImagePath = imageUri.getEncodedPath();
it works for me using gallery image picker
maybe this:
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), data.getData());

Categories

Resources