To get the file path I use:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
Intent.ACTION_GET_CONTENT doesn't allow to select folder.
But how to get the folder/directory path?
API level 19.
Are there ways to do this without third-party libraries?
To get the file I use
That code has little to do with files. Content != file.
But how to get the folder/directory?
Build your own UI for this. Or, use a third-party directory chooser library.
Are there ways to do this without third-party libraries?
There is no platform-defined Intent for choosing a filesystem directory. ACTION_OPEN_DOCUMENT_TREE will let the user choose a document tree on Android 5.1+, but a document tree is not necessarily a filesystem directory, just as ACTION_GET_CONTENT and ACTION_OPEN_DOCUMENT do not necessarily involve files.
First of all create constant in your activity class:
private static final int PICKFILE_REQUEST_CODE = 100;
When you need to pick a folder use intent like this:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
And after user selected folder you will get result in
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICKFILE_REQUEST_CODE) {
String folderPath = data.getDataString();
//TODO
return;
}
super.onActivityResult(requestCode, resultCode, data);
for picking any folder u can use this
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent,PICKFILE_RESULT_CODE);
after getting the file show its details as
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String FilePath = data.getData().getPath();
String FileName = data.getData().getLastPathSegment();
int lastPos = FilePath.length() - FileName.length();
String Folder = FilePath.substring(0, lastPos);
textFile.setText("Full Path: \n" + FilePath + "\n");
textFolder.setText("Folder: \n" + Folder + "\n");
textFileName.setText("File Name: \n" + FileName + "\n"); }
here is an example how you can do that hope this makes fully clear:
http://android-er.blogspot.com/2011/04/more-for-pick-file-using.html
Related
I am trying to fetch a file this way:
final Intent chooseFileIntent = new Intent(Intent.ACTION_GET_CONTENT);
String[] mimetypes = {"application/pdf"};
chooseFileIntent.setType("*/*");
chooseFileIntent.addCategory(Intent.CATEGORY_OPENABLE);
if (chooseFileIntent.resolveActivity(activity
.getApplicationContext().getPackageManager()) != null) {
chooseFileIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
activity.startActivityForResult(chooseFileIntent, Uploader.PDF);
}
Then in onActivityResult :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
According to many threads I'm supposed to fetch the file name from the intent with data.getData().getPath(), the file name I'm expecting is my_file.pdf, but instead I'm getting this :
/document/acc=1;doc=28
So what to do? Thanks for your help.
I am trying to fetch a file
Not with that code. That code is asking the user to pick a piece of content. This may or may not be a file.
According to many threads I'm supposed to fetch the file name from the intent with data.getData().getPath()
That was never correct, though it tended to work on older versions of Android.
So what to do?
Well, that depends.
If you wish to only accept files, integrate a file chooser library instead of using ACTION_GET_CONTENT. (UPDATE 2019-04-06: since Android Q is banning most filesystem access, this solution is no longer practical)
If you are willing to allow the user to pick a piece of content using ACTION_GET_CONTENT, please understand that it does not have to be a file and it does not have to have something that resembles a filename. The closest that you will get:
If getScheme() of the Uri returns file, your original algorithm will work
If getScheme() of the Uri returns content, use DocumentFile.fromSingleUri() to create a DocumentFile, then call getName() on that DocumentFile — this should return a "display name" which should be recognizable to the user
To get the real name and to avoid getting a name that looks like "image: 4431" or even just a number, you can write code as recommended by CommonsWare.
The following is an example of a code that selects a single pdf file, prints its name and path to the log, and then sends the file by email using its uri.
private static final int FILEPICKER_RESULT_CODE = 1;
private static final int SEND_EMAIL_RESULT_CODE = 2;
private Uri fileUri;
private void chooseFile() {
Intent fileChooser = new Intent(Intent.ACTION_GET_CONTENT);
fileChooser.setType("application/pdf");
startActivityForResult(Intent.createChooser(fileChooser, "Choose one pdf file"), FILEPICKER_RESULT_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == FILEPICKER_RESULT_CODE) {
if (resultCode == RESULT_OK) {
fileUri = data != null ? data.getData() : null;
if (fileUri != null) {
DocumentFile d = DocumentFile.fromSingleUri(this, fileUri);
if (d != null) {
Log.d("TAG", "file name: " + d.getName());
Log.d("TAG", "file path: " + d.getUri().getPath());
sendEmail(fileUri);
}
}
}
}
}
private void sendEmail(Uri path) {
String email = "example#gmail.com";
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("application/octet-stream");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "PDF file");
String[] to = { email };
intent.putExtra(Intent.EXTRA_EMAIL, to);
intent.putExtra(Intent.EXTRA_TEXT, "This is the pdf file...");
intent.putExtra(Intent.EXTRA_STREAM, path);
startActivityForResult(Intent.createChooser(intent, "Send mail..."), SEND_EMAIL_RESULT_CODE);
}
hope it helps.
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 need to create a App where I open Android Gallery from my Android App and I should be able to view only .jpeg images in gallery, other images like .png or any other format should be removed .
Can someone please suggest how to do this ?
look this example for txt you can do the same with jpg files, just list the files and select only that you want to use. Android: File list in ListView.
NOTE: The folder for gallery is Enviroment.DIRECTORY_DCIM
Here is the code for that:
Intent intent = new Intent();
intent.setType("image/jpeg");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Main.this.startActivity(intent);
but not sure if all android galery apps would honor the type extra
Here you go...
// Directory path here
String path = "PATH/TO/GALLERY";
File folder = new File(path);
File[] directoryListing = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
files = listOfFiles[i].getName();
if (files.endsWith(".JPG") || files.endsWith(".JPEG")) {
// Handle your file here
}
}
}
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/jpg");
startActivityForResult(photoPickerIntent, 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case 1:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
InputStream imageStream = getContentResolver().openInputStream(selectedImage);
Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
}
}
}
I use following code to select file but i can select one file only. How can i select more then one file.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, CHOOSE_FILE_RESULT_CODE);
.....
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// User has picked an image.
Uri uri = data.getData();
//File URI..
}
thank u
You can create a custom gallery of your own.More info can be had from here.
Android custom image gallery