0
Good morning, I’m a beginner in the programming of Android apps and I have a question mark about how to enable filepicker to select multiple files. My code works, but only selects one file at a time.
Follows the code:
Basectivity.java
@Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (reqCode == WebEngine.KEY_FILE_PICKER) {
String picturePath = FilePicker.getPickedFilePath(this, data);
if (webEngine != null) {
webEngine.uploadFile(data, picturePath);
}
}
}
}
Filepicker.java
package com.teste.final.utility;
FilePicker.java
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.os.Parcelable;
import android.provider.MediaStore;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
/**
* Created by Ashiq on 6/1/16.
*/
public class FilePicker {
private static String pictureTempPath = "";
private static final String CHOOSER_MSG = "Escolha uma opção";
private static final String IMAGE_PREFIX = "IMG_";
private static final String IMAGE_EXTENSION = ".jpg";
/**
* Start picker intent
*
* @param context
* @return
*/
public static Intent getPickFileIntent(Context context) {
Intent chooserIntent = null;
List<Intent> intentList = new ArrayList<Intent>();
addIntentsToList(context, intentList, getGalleryIntent());
addIntentsToList(context, intentList, getCameraIntent(context));
addIntentsToList(context, intentList, getFileIntent());
if (intentList.size() > 0) {
chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1), CHOOSER_MSG);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[]{}));
}
return chooserIntent;
}
/**
* Start camera picker intent
*
* @param context
* @return
*/
public static Intent getCameraPickerIntent(Context context) {
Intent chooserIntent = null;
List<Intent> intentList = new ArrayList<Intent>();
addIntentsToList(context, intentList, getCameraIntent(context));
if (intentList.size() > 0) {
chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1), CHOOSER_MSG);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[]{}));
}
return chooserIntent;
}
/**
* Start gallery picker intent
*
* @param context
* @return
*/
public static Intent getGalleryPickerIntent(Context context) {
Intent chooserIntent = null;
List<Intent> intentList = new ArrayList<Intent>();
addIntentsToList(context, intentList, getGalleryIntent());
if (intentList.size() > 0) {
chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1), CHOOSER_MSG);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[]{}));
}
return chooserIntent;
}
/**
* Get captured image path
*
* @param context
* @param data
* @return
*/
public static String getPickedFilePath(Context context, Intent data) {
String filePath = null;
boolean isCameraIntent = (data == null || data.getData() == null);
if (isCameraIntent) {
filePath = pictureTempPath;
} else {
filePath = getImagePath(context, data.getData());
/*if (filePath == null) {
filePath = data.getData().getPath();
}*/
}
return filePath;
}
private static String getImagePath(Context context, Uri imageUri) {
String path = null;
try {
String[] filePathColumn = {MediaStore.Images.Media.DATA};
// Get DB Cursor to get Image link
Cursor cursor = context.getContentResolver().query(imageUri, filePathColumn, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
// Get image path
path = cursor.getString(columnIndex);
cursor.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return path;
}
/**
* Creates a gallery intent
*
* @return
*/
private static Intent getGalleryIntent() {
// Define gallery intent
return new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
}
private static Intent getFileIntent() {
// Define file intent
Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.setType("*/*");
return chooseFile;
}
/**
* Creates a camera intent
*
* @return
*/
private static Intent getCameraIntent(Context ctx) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (cameraIntent.resolveActivity(ctx.getPackageManager()) != null) {
cameraIntent.putExtra("return-data", true);
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
String timeStamp = new SimpleDateFormat(DateUtils.DateTimeFormat.YYMMDD_HHMMSS.toString(), Locale.US).format(new Date());
String imageFileName = IMAGE_PREFIX + timeStamp;
File photoFile = new File(path, imageFileName + IMAGE_EXTENSION);
pictureTempPath = photoFile.getAbsolutePath();
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
}
return cameraIntent;
}
/**
* Make a list of choices to display to user
*
* @param context
* @param list
* @param intent
* @return
*/
private static void addIntentsToList(Context context, List<Intent> list, Intent intent) {
List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(intent, 0);
for (ResolveInfo resolveInfo : resInfo) {
String packageName = resolveInfo.activityInfo.packageName;
Intent targetedIntent = new Intent(intent);
targetedIntent.setPackage(packageName);
list.add(targetedIntent);
}
}
}
Your question is not clear. You want to select multiple files at the same time or you want to open one chooser (window) that allows choosing an application, among several, to select a file?
– ramaral
Take a look here: https://temandei.com, if you access this site through your mobile browser you will notice that the browser allows you to select multiple files at once. I’m not able to do this on the webview. I was only able to select 1 file at a time. I have this code with Filepicker selecting 1 file at a time, I choose the app and open the file. But it’s not what I want, I want to simply select several files at once like in the example I gave above.
– gelsonalves