Я загружаю фотографии через веб-просмотр, я изменяю размер загруженной фотографии. Я изменяю размер фотографии, которую выбрал при изменении размера. Но «Вызвано: java .lang.NullPointerException: попытка вызвать метод интерфейса 'void android .webkit.ValueCallback.onReceiveValue (java .lang.Object)' для ссылки на нулевой объект»
Я получаю сообщение об ошибке. Строка, в которой я получил ошибку: «this.mUploadMessage.onReceiveValue (Uri.fromFile (resizedFile));»
Я вызываю функцию showAttachmentDialog (), чтобы выбрать фотографию.
Что такое причина, по которой у меня возникла эта проблема? Открывается окно выбора файлов, я выбираю фотографию, но после выбора фотографии получаю эту ошибку.
private ValueCallback<Uri> mUploadMessage;
public ValueCallback<Uri[]> uploadMessage;
private final static int FILECHOOSER_RESULTCODE = 1;
private void showAttachmentDialog(ValueCallback<Uri[]> uploadMsg) {
this.uploadMessage = uploadMsg;
File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "DemetDavetiyeDD");
// Create the storage directory if it does not exist
if (! imageStorageDir.exists()){
imageStorageDir.mkdirs();
}
File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
this.imageUri= Uri.fromFile(file);
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getActivity().getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for(ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
cameraIntents.add(intent);
}
// mUploadMessage = uploadMsg;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
Intent chooserIntent = Intent.createChooser(intent,"Fotoğraf Seç");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
this.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
}
webViewAdim1.setWebChromeClient(new WebChromeClient()
{
// For 3.0+ Devices (Start)
// onActivityResult attached before constructor
protected void openFileChooser(ValueCallback uploadMsg, String acceptType)
{
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE);
}
// For Lollipop 5.0+ Devices
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
{
showAttachmentDialog(filePathCallback);
return true;
}
//For Android 4.1 only
protected void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)
{
mUploadMessage = uploadMsg;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "File Browser"), FILECHOOSER_RESULTCODE);
}
protected void openFileChooser(ValueCallback<Uri> uploadMsg)
{
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
String newPath;
if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == this.uploadMessage) {
return;
}
Uri result;
if (resultCode != RESULT_OK) {
result = null;
} else {
result = intent == null ? this.imageUri : intent.getData();
}
if(result==this.imageUri){
newPath=file.getAbsolutePath();
}
else{
newPath=getRealPathFromURI(result);
}
Bitmap bMap= BitmapFactory.decodeFile(newPath);
Bitmap out = Bitmap.createScaledBitmap(bMap, 150, 150, false);
File resizedFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "resize.png");
OutputStream fOut=null;
try {
fOut = new BufferedOutputStream(new FileOutputStream(resizedFile));
out.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
bMap.recycle();
out.recycle();
} catch (Exception e) { // TODO
}
this.mUploadMessage.onReceiveValue(Uri.fromFile(resizedFile));
this.mUploadMessage = null;
//resizedFile.delete();
}
}