Android: startActivityForResult deprecated
С очередным обновлением Android Studio опять прилетели новые deprecated. На этот раз для startActivityForResult. У меня сия конструкция используется для обработки выбора файла в компоненте WebView. Соответственно изменим:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() { @Override public void onActivityResult(ActivityResult result) { Intent data = result.getData(); Uri uri = data.getData(); if (result.getResultCode() == REQUEST_SELECT_FILE) { if (uploadMessage == null) return; uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(result.getResultCode(), data)); uploadMessage = null; }; if (result.getResultCode() == FILECHOOSER_RESULTCODE) { if (null == mUploadMessage) return; Uri result2 = data == null || result.getResultCode() != MainActivity.RESULT_OK ? null : data.getData(); mUploadMessage.onReceiveValue(result2); mUploadMessage = null; } } }); // For Lollipop 5.0+ Devices public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) { if (uploadMessage != null) { uploadMessage.onReceiveValue(null); uploadMessage = null; } uploadMessage = filePathCallback; Intent intent = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { intent = fileChooserParams.createIntent(); } try { someActivityResultLauncher.launch(intent); // startActivityForResult(intent, REQUEST_SELECT_FILE); } catch (ActivityNotFoundException e) { uploadMessage = null; return false; } return true; } 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); someActivityResultLauncher.launch(Intent.createChooser(i, "File Chooser")); } }); |