I use the following code in my Android app to open the gallery and select a video file.
Now I want to modify this code to open the file manager, and select a file of specific type ( for example *.xyz). How can I do this !?
public View.OnClickListener btnChoosePhotoPressed = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent_file = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.INTERNAL_CONTENT_URI);
final int ACTIVITY_SELECT_IMAGE = 1234;
startActivityForResult(intent_file, ACTIVITY_SELECT_IMAGE);
}
};
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1234:
if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Video.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
final String filePath = cursor.getString(columnIndex);
cursor.close();
// Now I use filePath to access the file.
}
}
};
I searched for similar questions and this answer was helpful: Open Default File Manager in android?
It helps to open the file manager. However, how can I handle the second part of my code about cursor
, filePathColumn
, and filepath
!? I need the filePath
to access the file.
Any help is highly appreciated.
EDIT 1 :
I changed my code to the following:
public View.OnClickListener btnChoosePhotoPressed = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent_file = new Intent();
final int ACTIVITY_SELECT_IMAGE = 1234;
intent_file.setAction(Intent.ACTION_GET_CONTENT);
intent_file.setType("video/*");
startActivityForResult(Intent.createChooser(intent_file, "Select Picture"),ACTIVITY_SELECT_IMAGE);
}
};
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1234:
if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
final String filePath = selectedImage.getPath();
}
}
};
This time, it opens the file manager to select the file. But unfortunately, the filePath
is somehow incorrect. Because later in my code, I use the following methods to send the file via sftp, which fails, and no file is transferred.
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
channelSftp2.put(fis, file.getName());
fis.close();
So, any suggestions?