Inx
Inx

Reputation: 2384

WebView, input of type file, camera and image

I have been googeling around for quite some time now... but I still cant find a real answer to my question. The thing is that I need a way for my user to be able to choose to upload a file from gallery or directly from camera once they press an input-element with the type="file". So if there is any good samples of this out there, then please let me know, ot if you might have a sample that I could take a look at.

Thanks in advance!

Upvotes: 2

Views: 6863

Answers (2)

Sung Hoon Kang
Sung Hoon Kang

Reputation: 11

I was creating a program that uploads file using input tag with android webview. by the way, i'm not an android app programmer.

Please take a look at these references: Android webview, openfilechooser termination and http://www.cnblogs.com/sipher/archive/2012/09/05/2672361.html.

Steps:

  1. Make a menu to with input file request to capture photo - Android webview, openfilechooser termination.

  2. But picture was not sending to server and modified imageUri values Environment.getExternalStoragePublicDirectory as seen here - http://www.cnblogs.com/sipher/archive/2012/09/05/2672361.html.

  3. So I needed to add permission WRITE_EXTERNAL_STORAGE in Manifest.

    webView.setWebChromeClient(new WebChromeClient(){
        @SuppressWarnings("unused")
        public void openFileChooser(ValueCallback<Uri> uploadMsg){
            this.openFileChooser(uploadMsg, "");
        }
    @SuppressWarnings("unused")
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String AcceptType, String capture) {
        this.openFileChooser(uploadMsg, "");
    }
    
    @SuppressWarnings("unused")
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String AcceptType) {
        final List<Intent> cameraIntents = new ArrayList<Intent>();
        final Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File externalDataDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        File cameraDataDir = new File(externalDataDir.getAbsolutePath()+File.separator+"browser-photos");
        cameraDataDir.mkdirs();
        String mCameraFilePath = cameraDataDir.getAbsolutePath()+File.separator+System.currentTimeMillis()+".jpg";
        imageUri = Uri.fromFile(new File(mCameraFilePath));
    
    final PackageManager packageManager = 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 i = new Intent(Intent.ACTION_GET_CONTENT);
    i.addCategory(Intent.CATEGORY_OPENABLE);
    i.setType("image/*");               
    Intent chooserIntent = Intent.createChooser(i,"Image Chooser");
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
    MainActivity.this.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
    }
    });
    webView.getSettings().setDomStorageEnabled(true);
    webView.loadUrl("..............");
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webView.setWebViewClient(new WebViewClientClass());}
    

Now. i'm can upload pictures taken by camera with input tag.

UPDATE: You can look for alternate methods to solve the same issue

Upvotes: 1

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

public void attachFileInput() {

    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
    i.addCategory(Intent.CATEGORY_OPENABLE);
    i.setType("image/*");
    ((Activity) mContext).startActivityForResult(
                Intent.createChooser(i, "Image Choser"), 1);
}

This method will be in your JSInterface. Call it as follows:

$(".file").live('click', function() {
    mySJInterface.attachFileInput();
});

Hope this helps.

Upvotes: 1

Related Questions