Name of an image from gallery in android - android

I am using yhe following lines of code for getting the image path from the gallery.
Now, how can i also pick the name of that image from the gallery?
public void selectImage() {
new AlertDialog.Builder(getActivity()).setTitle("Select Picture")
.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
}
}
}
}).show();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == getActivity().RESULT_OK && null != data) {
Uri selectedImageUri = data.getData();
String selectedImagePath = getRealPathFromURIForGallery(selectedImageUri);
}
}
public String getRealPathFromURIForGallery(Uri uri) {
if (uri == null) {
return null;
}
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
return uri.getPath();
}

Related

how to select image from Device and it's path in android Studio

I need to select image from device and it's full path
But I get null value.. this is my Code
any suggestions
private void openPicture() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
String selectedImagePath = getPath(selectedImageUri);
Toast.makeText(this, ""+selectedImagePath, Toast.LENGTH_SHORT).show();
}
}
}
public String getPath(Uri uri) {
if( uri == null ) {
return null;
}
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();
String path = cursor.getString(column_index);
cursor.close();
return path;
}
return uri.getPath();
}
it's return Null value. I need selected image Path.

Image path null in Android

I have code like this, the problem is when i want to save it to sqlite, the error message shown that i got null in image path.
Here is the main activity
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
System.out.println("Data Image : "+selectedImageUri);
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
image1.setVisibility(View.VISIBLE);
image1.setImageURI(selectedImageUri);
}
}
}
private String getRealPathFromURI(Uri contentURI) {
String result;
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null) { // Source is Dropbox or other similar local file path
result = contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(idx);
cursor.close();
}
return result;
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
I use this method to take photos from camera.
private void capturePhoto() {
// save this Uri in a field variable.
uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new ContentValues());
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, CAMERA_REQUEST);
}
then onActivityResult will look like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK){
// use the field Uri, it now points to the path to the image taken from the camera.
}
}
In manifest file set permission
Check permission granted in onActivityResult method and after its granted READ_EXTERNAL_STORAGE
If it has Permission then call doCreatePath() and set the same in onRequestPermissionsResult on permission granted.
void doCreatePath()
{
try {
Uri originalUri = filePath;
String pathsegment[] = originalUri.getLastPathSegment().split(":");
String id = pathsegment[0];
final String[] imageColumns = {MediaStore.Images.Media.DATA};
final String imageOrderBy = null;
Uri uri = getUri();
Cursor imageCursor = getActivity().getContentResolver().query(uri, imageColumns,
null, null, null);
if (imageCursor.moveToFirst()) {
int column_index_data = imageCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
realPath = imageCursor.getString(column_index_data);
System.out.print("value" + realPath);
}
} catch (Exception e) {
e.printStackTrace();
}
}
It works!.

Get PicturePath selected from gallery to store it in dataBase

I used this code to upload image from gallery .it works perfectly..but the problem is that i wanna get the Image's path to store it in wamp dataBase ..`
public class Image extends Activity {
ImageView contact;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
contact = (ImageView) findViewById(R.id.candidat);
contact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Contact Image"), 1);
}
});
}
public void onActivityResult(int reqCode, int resCode, Intent data) {
if (resCode == RESULT_OK) {
if (reqCode == 1)
contact.setImageURI(data.getData());
}
}
}
Thank you for help
You can get the exact path using this method -
public String getPathFromURI(Uri contentURI) {
String result = null;
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
int _id = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(_id);
cursor.close();
}
return result;
}
And use it like -
public void onActivityResult(int reqCode, int resCode, Intent data) {
if (resCode == RESULT_OK) {
if (reqCode == 1)
contact.setImageURI(data.getData());
String path = getPathFromURI(data.getData());
}
}
put this code:
public static final int IMAGEM = 1;
In the click put:
startActivityForResult(Intent.createChooser(new Intent(Intent.ACTION_PICK).setType("image/*"), "Select a image"), IMAGE);
In your activity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
try {
if (resultCode == RESULT_OK && requestCode == IMAGE){
String pathImg = getRealPathFromURI(intent.getData());
}
}catch (Exception e){
e.printStackTrace();
}
}
public String getRealPathFromURI(Uri uri) {
//this method work for any api
Cursor cursor = null;
try {
Uri newUri = handleImageUri(uri);
String[] proj = { MediaStore.Images.Media.DATA };
cursor = getContentResolver().query(newUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} catch (Exception e){
return null;
} finally {
if (cursor != null) {
cursor.close();
}
}
}

Android: Getting path from mediaStore after selecting from android gallery app

I tried to return real photo path stored in my android phone. It works on my 4.4.2 phone but when using 5.0.2 phone, the returned path is null
This is my code getting the real photo path from the intent.getData as a Uri(
content://com.android.providers.media.documents/document/image%3A3061),
String[] proj = { MediaStore.Images.Media.DATA };
CursorLoader loader = new CursorLoader(GlobalApplication.getContext(), photoUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String result = cursor.getString(column_index);
cursor.close();
return result;
And this is the code I start the gallery application:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),MobileConstant.newInstance().REQUEST_CODE_GALLERY);
I have it working by using this:
Cursor cursor = GlobalApplication.getContext().getContentResolver().query(photoUri, null, null, null, null);
cursor.moveToFirst();
String document_id = cursor.getString(0);
document_id = document_id.substring(document_id.lastIndexOf(":")+ 1);
cursor.close();
cursor = GlobalApplication.getContext().getContentResolver().query(
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
return path;
//To get picture from gallery or camrea
if (Build.VERSION.SDK_INT < 19) {
Intent intent = new Intent();
intent.setType("image/jpeg");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), 200);
} else {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/jpeg");
startActivityForResult(intent, 300);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//To get result
if (requestCode == 200) {
Uri selectedImage = data.getData();
Log.d("Uri", selectedImage.toString());
startCropImage(selectedImage);
}
else if (requestCode == 300) {
Uri originalUri = data.getData();
final int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
//noinspection ResourceType
getContentResolver().takePersistableUriPermission(originalUri, takeFlags);
Log.d("Uri", originalUri.toString());
startCropImage(originalUri);
}
}
Intent for picking image from gallery
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent, reqCode);
In onActivityResult method
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
try {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == reqCode) {
if (data != null) {
String realPath=getRealPathFromURI(data.getData());
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String getRealPathFromURI(Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = getActivity().getContentResolver().query(contentUri, proj,
null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}

Retrieve the path of Image from gallery into Edittext

I want to Achieve the following in my project:
open the Gallery on clicking an Edittext.
select an Image or File in the gallery and get its path into my edittext.
Can anyone tell me how to do this?
On your onActivityResult
Uri uri = intent.getData();
And then grap that uri path this way
MyEditText.setText(uri.getPath());
try this,
For open gallary,
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);
use this method to get image path,
#SuppressWarnings("unchecked")
#Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case PICK_IMAGE:
String imagepath = (getAbsolutePath(data.getData()) + "|").split("\\|");
break;
}
}
}
public String getAbsolutePath(Uri uri) {
String[] projection = { MediaColumns.DATA };
#SuppressWarnings("deprecation")
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}

Categories

Resources