Min menu

Pages

SAMSUNG

Enable Upload Button in Web View Android App - Android Studio


 Enable Upload Button in WebView Android App - Android Studio WebView


Watch This Video


Source Code


AndroidManifest.xml


<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Variables


private static String file_type = "*/*";
private String cam_file_data = null;
private ValueCallback<Uri> file_data;
private ValueCallback<Uri[]> file_path;
private final static int file_req_code = 1;

Inside onCreate Class in WebChromeClient


public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {

    if(file_permission() && Build.VERSION.SDK_INT >= 21) {
        file_path = filePathCallback;
        Intent takePictureIntent = null;
        Intent takeVideoIntent = null;

        boolean includeVideo = false;
        boolean includePhoto = false;

        paramCheck:
        for (String acceptTypes : fileChooserParams.getAcceptTypes()) {
            String[] splitTypes = acceptTypes.split(", ?+");
            for (String acceptType : splitTypes) {
                switch (acceptType) {
                    case "*/*":
                        includePhoto = true;
                        includeVideo = true;
                        break paramCheck;
                    case "image/*":
                        includePhoto = true;
                        break;
                    case "video/*":
                        includeVideo = true;
                        break;
                }
            }
        }

        if (fileChooserParams.getAcceptTypes().length == 0) {
            includePhoto = true;
            includeVideo = true;
        }

        if (includePhoto) {
            takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null) {
                File photoFile = null;
                try {
                    photoFile = create_image();
                    takePictureIntent.putExtra("PhotoPath", cam_file_data);
                } catch (IOException ex) {
                    Log.e(TAG, "Image file creation failed", ex);
                }
                if (photoFile != null) {
                    cam_file_data = "file:" + photoFile.getAbsolutePath();
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                } else {
                    cam_file_data = null;
                    takePictureIntent = null;
                }
            }
        }

        if (includeVideo) {
            takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
            if (takeVideoIntent.resolveActivity(MainActivity.this.getPackageManager()) != null) {
                File videoFile = null;
                try {
                    videoFile = create_video();
                } catch (IOException ex) {
                    Log.e(TAG, "Video file creation failed", ex);
                }
                if (videoFile != null) {
                    cam_file_data = "file:" + videoFile.getAbsolutePath();
                    takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(videoFile));
                } else {
                    cam_file_data = null;
                    takeVideoIntent = null;
                }
            }
        }

        Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
        contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
        contentSelectionIntent.setType(file_type);


        Intent[] intentArray;
        if (takePictureIntent != null && takeVideoIntent != null) {
            intentArray = new Intent[]{takePictureIntent, takeVideoIntent};
        } else if (takePictureIntent != null) {
            intentArray = new Intent[]{takePictureIntent};
        } else if (takeVideoIntent != null) {
            intentArray = new Intent[]{takeVideoIntent};
        } else {
            intentArray = new Intent[0];
        }

        Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
        chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
        chooserIntent.putExtra(Intent.EXTRA_TITLE, "File chooser");
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
        startActivityForResult(chooserIntent, file_req_code);
        return true;
    } else {
        return false;
    }
}

Outside onCreate Class


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
    super.onActivityResult(requestCode, resultCode, intent);
    if(Build.VERSION.SDK_INT >= 21){
        Uri[] results = null;

        if (resultCode == Activity.RESULT_CANCELED) {
            file_path.onReceiveValue(null);
            return;
        }

        if(resultCode== Activity.RESULT_OK){
            if(null == file_path){
                return;
            }
            ClipData clipData;
            String stringData;

            try {
                clipData = intent.getClipData();
                stringData = intent.getDataString();
            }catch (Exception e){
                clipData = null;
                stringData = null;
            }
            if (clipData == null && stringData == null && cam_file_data != null) {
                results = new Uri[]{Uri.parse(cam_file_data)};
            }else{
                if (clipData != null) {
                    final int numSelectedFiles = clipData.getItemCount();
                    results = new Uri[numSelectedFiles];
                    for (int i = 0; i < clipData.getItemCount(); i++) {
                        results[i] = clipData.getItemAt(i).getUri();
                    }
                } else {
                    try {
                        Bitmap cam_photo = (Bitmap) intent.getExtras().get("data");
                        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                        cam_photo.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                        stringData = MediaStore.Images.Media.insertImage(this.getContentResolver(), cam_photo, null, null);
                    }catch (Exception ignored){}

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

        file_path.onReceiveValue(results);
        file_path = null;
    }else{
        if(requestCode == file_req_code){
            if(null == file_data) return;
            Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
            file_data.onReceiveValue(result);
            file_data = null;
        }
    }
}

@Override
public void onConfigurationChanged(Configuration newConfig){
    super.onConfigurationChanged(newConfig);
}

public boolean file_permission(){
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && (checkSelfPermission(WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED || checkSelfPermission(CAMERA) == PackageManager.PERMISSION_DENIED)) {
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{WRITE_EXTERNAL_STORAGE, CAMERA}, 1);
        return true;
    }else{
        return false;
    }
}

private File create_image() throws IOException{
    @SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "img_"+timeStamp+"_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    return File.createTempFile(imageFileName,".jpg",storageDir);
}

private File create_video() throws IOException {
    @SuppressLint("SimpleDateFormat")
    String file_name = new SimpleDateFormat("yyyy_mm_ss").format(new Date());
    String new_name = "file_"+file_name+"_";
    File sd_directory = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    return File.createTempFile(new_name, ".3gp", sd_directory);
}

《《《 Links 》》》


To Subscribe to our YouTube Channel


Source Code + Project



reaction:
Muhammad Al-Aloul
Muhammad Al-Aloul
Software Engineer and Graphic Designer

Comments