android file chooser,java - Android 4.4 WebView file chooser not opening? - Stack Overflow

I was working on this issue too, and the problem and here is my solution

private class MyWebChromeClient extends WebChromeClient {

/**

* This is the method used by Android 5.0+ to upload files towards a web form in a Webview

*

* @param webView

* @param filePathCallback

* @param fileChooserParams

* @return

*/

@Override

public boolean onShowFileChooser(

WebView webView, ValueCallback filePathCallback,

WebChromeClient.FileChooserParams fileChooserParams) {

if (mFilePathCallback != null) {

mFilePathCallback.onReceiveValue(null);

}

mFilePathCallback = filePathCallback;

Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);

contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);

contentSelectionIntent.setType("image/*");

Intent[] intentArray = getCameraIntent();

Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);

chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);

chooserIntent.putExtra(Intent.EXTRA_TITLE, "Seleccionar Fuente");

chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);

startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE);

return true;

}

@Override

public void onProgressChanged(WebView view, int newProgress) {

mProgressBar.setVisibility(View.VISIBLE);

WebActivity.this.setValue(newProgress);

super.onProgressChanged(view, newProgress);

}

@Override

public boolean onJsAlert(WebView view, String url, String message, JsResult result) {

Log.d("LogTag", message);

result.confirm();

return true;

}

/**

* Despite that there is not a Override annotation, this method overrides the open file

* chooser function present in Android 3.0+

*

* @param uploadMsg

* @author Tito_Leiva

*/

public void openFileChooser(ValueCallback uploadMsg) {

mUploadMessage = uploadMsg;

Intent i = getChooserIntent(getCameraIntent(), getGalleryIntent("image/*"));

i.addCategory(Intent.CATEGORY_OPENABLE);

WebActivity.this.startActivityForResult(Intent.createChooser(i, "Selecciona la imagen"), FILECHOOSER_RESULTCODE);

}

public void openFileChooser(ValueCallback uploadMsg, String acceptType) {

mUploadMessage = uploadMsg;

Intent i = getChooserIntent(getCameraIntent(), getGalleryIntent("*/*"));

i.addCategory(Intent.CATEGORY_OPENABLE);

WebActivity.this.startActivityForResult(

Intent.createChooser(i, "Selecciona la imagen"),

FILECHOOSER_RESULTCODE);

}

/**

* Despite that there is not a Override annotation, this method overrides the open file

* chooser function present in Android 4.1+

*

* @param uploadMsg

* @author Tito_Leiva

*/

public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) {

mUploadMessage = uploadMsg;

Intent i = getChooserIntent(getCameraIntent(), getGalleryIntent("image/*"));

WebActivity.this.startActivityForResult(Intent.createChooser(i, "Selecciona la imagen"), FILECHOOSER_RESULTCODE);

}

private Intent[] getCameraIntent() {

// Determine Uri of camera image to save.

Intent takePictureIntent = new Intent(WebActivity.this, CameraActivity.class);

if (takePictureIntent.resolveActivity(getPackageManager()) != null) {

// Create the File where the photo should go

File photoFile = null;

try {

photoFile = createImageFile();

takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);

} catch (IOException ex) {

// Error occurred while creating the File

Log.e(TAG, "Unable to create Image File", ex);

}

// Continue only if the File was successfully created

if (photoFile != null) {

mCameraPhotoPath = "file:" + photoFile.getAbsolutePath();

takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,

Uri.fromFile(photoFile));

} else {

takePictureIntent = null;

}

}

Intent[] intentArray;

if (takePictureIntent != null) {

intentArray = new Intent[]{takePictureIntent};

} else {

intentArray = new Intent[0];

}

return intentArray;

}

private Intent getGalleryIntent(String type) {

// Filesystem.

final Intent galleryIntent = new Intent();

galleryIntent.setType(type);

galleryIntent.addCategory(Intent.CATEGORY_OPENABLE);

galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

return galleryIntent;

}

private Intent getChooserIntent(Intent[] cameraIntents, Intent galleryIntent) {

// Chooser of filesystem options.

final Intent chooserIntent = Intent.createChooser(galleryIntent, "Seleccionar Fuente");

// Add the camera options.

chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents);

return chooserIntent;

}

}

One problem that I solved is the uri delivered for the onActivityResult() method does not have extension. To solve this I use this method

public static Uri savePicture(Context context, Bitmap bitmap, int maxSize) {

int cropWidth = bitmap.getWidth();

int cropHeight = bitmap.getHeight();

if (cropWidth > maxSize) {

cropHeight = cropHeight * maxSize / cropWidth;

cropWidth = maxSize;

}

if (cropHeight > maxSize) {

cropWidth = cropWidth * maxSize / cropHeight;

cropHeight = maxSize;

}

bitmap = ThumbnailUtils.extractThumbnail(bitmap, cropWidth, cropHeight, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);

File mediaStorageDir = new File(

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),

context.getString(R.string.app_name)

);

if (!mediaStorageDir.exists()) {

if (!mediaStorageDir.mkdirs()) {

return null;

}

}

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

File mediaFile = new File(

mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg"

);

// Saving the bitmap

try {

ByteArrayOutputStream out = new ByteArrayOutputStream();

bitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);

FileOutputStream stream = new FileOutputStream(mediaFile);

stream.write(out.toByteArray());

stream.close();

} catch (IOException exception) {

exception.printStackTrace();

}

// Mediascanner need to scan for the image saved

Intent mediaScannerIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

Uri fileContentUri = Uri.fromFile(mediaFile);

mediaScannerIntent.setData(fileContentUri);

context.sendBroadcast(mediaScannerIntent);

return fileContentUri;

}

Finally, onActivityResult() method is

@Override

protected void onActivityResult(int requestCode, int resultCode,

Intent intent) {

if (resultCode == RESULT_OK) {

// This is for Android 4.4.4- (JellyBean & KitKat)

if (requestCode == FILECHOOSER_RESULTCODE) {

if (null == mUploadMessage) {

super.onActivityResult(requestCode, resultCode, intent);

return;

}

final boolean isCamera;

if (intent == null) {

isCamera = true;

} else {

final String action = intent.getAction();

if (action == null) {

isCamera = false;

} else {

isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

}

}

Uri selectedImageUri;

if (isCamera) {

selectedImageUri = mOutputFileUri;

mUploadMessage.onReceiveValue(selectedImageUri);

mUploadMessage = null;

return;

} else {

try {

Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), intent.getData());

selectedImageUri = intent == null ? null : ImageUtility.savePicture(this, bitmap, 1400);

mUploadMessage.onReceiveValue(selectedImageUri);

mUploadMessage = null;

return;

} catch (IOException e) {

e.printStackTrace();

}

}

// And this is for Android 5.0+ (Lollipop)

} else if (requestCode == INPUT_FILE_REQUEST_CODE) {

Uri[] results = null;

// Check that the response is a good one

if (resultCode == Activity.RESULT_OK) {

if (intent == null) {

// If there is not data, then we may have taken a photo

if (mCameraPhotoPath != null) {

results = new Uri[]{Uri.parse(mCameraPhotoPath)};

}

} else {

Bitmap bitmap = null;

try {

bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), intent.getData());

} catch (IOException e) {

e.printStackTrace();

}

Uri dataUri = ImageUtility.savePicture(this, bitmap, 1400);

if (dataUri != null) {

results = new Uri[]{dataUri};

}

}

}

mFilePathCallback.onReceiveValue(results);

mFilePathCallback = null;

return;

}

} else {

super.onActivityResult(requestCode, resultCode, intent);

return;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值