-1

I can't figure out what could be the reason. The photo is attached only 1 time, that is, I press the attach button, the photo is attached, I press it again - the application crashes ... Maybe someone can also tell me what code will help attach several images at once, now this is a maximum of one.

There is also an option to upload an image from the camera (should be). The camera opens, takes a photo and seems to attach, but nothing actually happens. And after pressing the button to attach a photo again, the application crashes, but this is more likely to the first part of the question.

public class MainActivity extends AppCompatActivity {

    private MainActivity activity;
    private WebView webView;
    private UploadSupport uploadSupport;
    private ValueCallback<Uri[]> mUMA;
    private Uri uri;
    private String mainPage = "*";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        uploadSupport = new UploadSupport(MainActivity.this);

        webView = findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setAllowContentAccess(true);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().getLoadWithOverviewMode();

        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(
                    WebView view,
                    WebResourceRequest request
                ){

                return super.shouldOverrideUrlLoading(view, request);
            }
            @Override
            public void onPageFinished(WebView view, String url) {
            }
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
            }
        });
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
                uploadSupport.showFileChooser(filePathCallback);
                if (mUMA != null){
                    mUMA.onReceiveValue(null);
                }
                mUMA = filePathCallback;

                return true;
            }

            @Override
            public void onPermissionRequest(final PermissionRequest request) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    request.grant(request.getResources());
                }
            }
        });
        webView.loadUrl(mainPage);
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode==UploadSupport.uploadCode){
            if (grantResults.length>0&&grantResults[0]== PackageManager.PERMISSION_GRANTED){
                uploadSupport.showFileChooser(mUMA);
            }
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==UploadSupport.FCR){
            if (uploadSupport.mUMA!=null){
                uploadSupport.mUMA.onReceiveValue(parseResult(resultCode, data));
            }
        }
    }
}

I use an additional class UploadSupport for file upload.

public class UploadSupport {
    public ValueCallback<Uri> mUM;
    public ValueCallback<Uri[]> mUMA;
    public String mCM;
    public static int FCR = 863;
    public static int uploadCode = 9438162;
    private Activity activity;

    public UploadSupport(Activity activity){
        this.activity = activity;
    }
    public void openFileChooser(ValueCallback<Uri> uploadMsg) {
        mUM = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("*/*");
        activity.startActivityForResult(Intent.createChooser(i, "File Chooser"), FCR);
    }
    public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
        mUM = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("*/*");
        activity.startActivityForResult(
                Intent.createChooser(i, "File Browser"),
                FCR);
    }
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
        mUM = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("*/*");
        activity.startActivityForResult(Intent.createChooser(i, "File Chooser"), FCR);
    }
    private File createImageFile() throws IOException {
        @SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "img_" + timeStamp + "_";
        File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        return File.createTempFile(imageFileName, ".jpg", storageDir);
    }
    public void showFileChooser(ValueCallback<Uri[]> filePathCallback){
        if (Build.VERSION.SDK_INT>=23){
            if (ContextCompat.checkSelfPermission(activity,Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    ==PackageManager.PERMISSION_GRANTED){
                manageFileChooser(filePathCallback);
            }else {
                activity.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},uploadCode);
            }
        }else {
            manageFileChooser(filePathCallback);
        }
    }
    private void manageFileChooser(ValueCallback<Uri[]> filePathCallback){
        if (mUMA != null) {
            mUMA.onReceiveValue(null);
        }
        mUMA = filePathCallback;
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) {
            File photoFile = null;
            try {
                photoFile = createImageFile();
                takePictureIntent.putExtra("PhotoPath", mCM);
            } catch (IOException exc) {
                Log.e("TAG", "Image file creation failed", exc);
            }
            if (photoFile != null) {
                mCM = "file:" + photoFile.getAbsolutePath();
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
            } else {
                takePictureIntent = null;
            }
        }
        Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
        contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
        contentSelectionIntent.setType("*/*");
        Intent[] intentArray;
        if (takePictureIntent != null) {
            intentArray = new Intent[]{takePictureIntent};
        } else {
            intentArray = new Intent[0];
        }
        Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
        chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
        chooserIntent.putExtra(Intent.EXTRA_TITLE, "Choose an Action");
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
        activity.startActivityForResult(chooserIntent,FCR);
    }
}

There is 2 errors in Logcat, after crash

A/chromium: [FATAL:jni_android.cc(306)] Please include Java exception stack in crash report
A/libc: Fatal signal 5 (SIGTRAP), code -6 (SI_TKILL) in tid 19063 (iesel.ru.java12), pid 19063

I would be very grateful if you tell me what is wrong or at least point me in the right direction.

TenjiTenno
  • 11
  • 3
  • Does this answer your question? [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – a_local_nobody May 23 '22 at 12:17
  • Add crash Stacktrace with question . – ADM May 23 '22 at 12:25

0 Answers0