I'm making an app that picks a image from the gallery and returns the result. I need to get the file path from that selection and store it for later use. I have looked at similar questions and apparently the file path of the image is stored in the column
MediaStore.Images.Media.DATA
How would anyone know that this is the file path? I've looked at the android documentation on MediaStore and it does not tell you where the file path column is. Any help would be appreciated.
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECT_PHOTO);
use this to get an image from gallery, it will return URI of the image which could be store store for later use
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_CANCELED) {
if (requestCode == SELECT_PHOTO) {
Uri path = data.getData();
}
}
}
Related
I'm a junior in android development and I faced a problem with getting Uri for a long time. My aim is get an Image Uri and show the image after few days. I use this method and it works when I restart my app, but in doesn't work when I try to upload image using the same Uri after a day. What i should do to get long Uri?
Intent galleryIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, 1);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == RESULT_OK){
Uri uri = data.getData();
save(uri.toString());
}
}
because of this URL refers to a local image and I think will not work if you removed this image or renamed it,
you can transform it to base64 and save it in shared preferences or local DB.
I am developing a file uploading android application. My objective is to upload the user selected file from file manager to a remote server. But when a google drive file is selected , file uploading fails because of empty path . Can somebody help me ?
My code is :
private void showFileChooser() {
Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(intent, "Choose File to Upload.."), PICK_FILE_REQUEST);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == PICK_FILE_REQUEST) {
if (data == null) {
//no data present
return;
}
Uri selectedFileUri = data.getData();
selectedFilePath = FilePath.getPath(mActivity, selectedFileUri);
if (selectedFilePath != null && !selectedFilePath.equals("")) {
callUploadDocumentAPI();
} else {
Toast.makeText(mActivity, StringConstants.CANT_UPLOAD_TO_SERVER, Toast.LENGTH_SHORT).show();
}
}
}
But when a google drive file is selected , file uploading fails because of empty path .
FilePath.getPath(mActivity, selectedFileUri) cannot work. A Uri is not a file.
Use a ContentResolver and openInputStream() to get an InputStream on the content identified by the Uri. Either use that InputStream directly, or use it to make your own copy of the content in some file that you control, then use that copy.
I'm using file helper class from this post https://stackoverflow.com/a/20559175/2281821 but it doesn't working properly in each case. I'm using Intent chooseIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT); to start file picker. I have Android Nougat and I'm receiving from onActivityResult uri: content://com.android.externalstorage.documents/document/home%3Aimage.png
What does this home: means? How can I get access to this file? I was trying with Environment.getExternalStorageDirectory() and System.getenv("EXTERNAL_STORAGE") but I can't get acess.
Following code can be used to get document path:
Intent galleryIntent = new Intent();
galleryIntent .setType("image/*");
galleryIntent .setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(galleryIntent , getString(R.string.app_name)),REQUEST_CODE);
OnActivityResult():
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CODE) {
Uri uri = data.getData();
}
}
}
You can use "uri" variable value to get bitmap. If you want to open document other image also then remove following line:
galleryIntent .setType("image/*");
You get access to a content scheme by opening for instance an InputStream on the uri.
InputStream is = getContentResolver().openInputStream(uri);
BitmapFactory.decodeFromStream(is) will happily read from your stream.
i want to capture an image and save it in folder, but when i capture it, my image is not save in folder that i want, but just save in gallery, and the name of my image is not like what i want.
my image also not display in my activity, is there anyone know, why this is happen?
this is my code :
private void dialogKamera() {
String sdCard= Environment.getExternalStorageDirectory()+"/android/data/spaj/spaj_foto.png";
File file=new File(sdCard);
if(file.exists())
file.delete();
Uri outputFileUri = Uri.fromFile(file);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, 0);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0 && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
image_spaj.setImageBitmap(photo);
}
i don't know where is my fault, i hope somebody can help me to solve my problem, thank you
You should make sure the output directory exists.
EDIT: removed spurious comment :-)
To ensure the directory exists:
File dir = file.getParentFile();
dir.mkdirs();
To display the image, here are several good resources:
How can I display image in Android Application,
Displaying simple bitmap on Android has a list of other resources,
And the tutorial Displaying Bitmaps in Your UI.
First make sure the directory exists, and then
try this:
Change path to
String sdCard= Environment.getExternalStorageDirectory()+"/Android/data/spaj/spaj_foto.png"
'Android' instead of 'android'.
I am making an android album app where I can create an album and add photos and delete photos from the album. Adding a photo is a bit tricky where I need a photo filename with a file path. This was very easy using JFileChooser in java but this is android and I have no clue on getting the filename and file path. Is there any thing in the android api where I can get the same functionality as the JFileChooser.
I am looking for a solution to this problem either using a file chooser of some sort or an entire to new approach. Any help is appreciated..
Or is there any other approach I can implement to add a photo...
You may use Intent.ACTION_PICK to invoke an image picker. This intent may be caught by the default gallery app, or some other app installed on the device.
private static final int REQUEST_PICKER = 1;
private void invokePicker() {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Complete action using"), REQUEST_PICKER);
}
Then receive the result on onActivityResult.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK)
return;
if (requestCode == PICK_FROM_FILE) {
// Get Uri of the file selected,
Uri theImageUri = data.getData();
// Or if you want a Bitmap,
Bitmap theBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), theImageUri);
}
}
Edited:
Though in this way you don't need a real file path, you can get it from MediaStore if you need.