12

I'm looking for a solution for some days for my problem but I can't find a good one. So I wanted to ask here again before I start developing a native app for Android since I can't solve this problem.

As the title says, I'm trying to upload an image by using webview to a html content which NORMALLY is choosing an image, showing it on the page and allowing user to crop it. I made whole app and had tested it on normal browser of my phone so I didn't know that this woulnd't work. That's why it's damn frustrating.

Those solutions which I've found on internet are not working for my Android and I read that it's nor possible anymore.

Does anyone know if this is possible or not? It would be a great help if I could get more informtions about this (even if it's possible or not?) Thanks in Adavnce...

yadbo
  • 407
  • 1
  • 5
  • 16
  • Do you follow the new permission model being followed since API 23? – Sibidharan Apr 04 '16 at 19:12
  • what do you mean? @Sibidharan – yadbo Apr 04 '16 at 19:17
  • While you are selecting file using WebView, your app must get permission from the system that it should read the `EXTERNAL_STORAGE`. The permission model from API 23 is a RunTime OnDemand model. Did you manifest that before you choose image from gallery using your `WebView`? – Sibidharan Apr 04 '16 at 19:19
  • It doesn't affect.... Still skips and nothing happens. But what do you think about the advanced webview plugin? – yadbo Apr 04 '16 at 19:26
  • Whatever the view may be. When they are inside your app, you need to manifest permission for them to read the storage. Make sure you set these permissions `` `` `` – Sibidharan Apr 04 '16 at 19:32
  • Please share your code and manifest file. I will help. – Sibidharan Apr 04 '16 at 19:33
  • I'm trying to make a working code but nothign works... For example this second one https://stackoverflow.com/questions/5907369/file-upload-in-webview... "openFileChooser" is undefined method for me. But the other view which I've mentioned seems to work. I think it's not possible anymore to do it without any other plugin. – yadbo Apr 04 '16 at 19:53
  • Check this answer http://stackoverflow.com/a/29545290/3073612. Compile using API 21+ and it should work! – Sibidharan Apr 04 '16 at 21:15
  • Wow it's working! (And yeah I've added the permissions which you've listed). So Android added this feature later by using those permissions? And what about API 21+? What about the devices below 21? Will this work on the most devices by using 21+? @Sibidharan – yadbo Apr 04 '16 at 21:47
  • It is just the compile SDK version.. It will work on all devices. Do not worry :) I will add the same as the answer.. Upvote and mark as accepted please as a way of thank :) – Sibidharan Apr 04 '16 at 22:06
  • 1
    Thanks for your help :) You saved my WebApp @Sibidharan – yadbo Apr 04 '16 at 22:18
  • Hello again... some user can't use this app anymore. It's not compatible with their devices anymore. @Sibidharan Is there a trick? What to do? – yadbo Apr 10 '16 at 15:51
  • What is the version of Android they are using !? Is it greater than your minimumSdkVersion?? – Sibidharan Apr 10 '16 at 15:52
  • They use 4.1.2 @Sibidharan an my minSDK is 21 as u recommended. – yadbo Apr 10 '16 at 16:00
  • Please change the Minimum SDK to 16 or whatever min you want.. I said the complieSDK as 21. Not the minimum – Sibidharan Apr 10 '16 at 16:11
  • Oh my fault then.. thanks again @Sibidharan :) – yadbo Apr 10 '16 at 16:23
  • It's me again... this is not working for Samsung S3 :-/ @Sibidharan sorry – yadbo Apr 11 '16 at 19:15
  • Can you please come over hangouts? sibi.nandhu@gmail.com @yadbo – Sibidharan Apr 11 '16 at 19:15

2 Answers2

16

Try adding these permissions.

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Android 6.0 Marshmallow introduces a new model for handling permissions, which streamlines the process for users when they install and upgrade apps. Provided you're using version 8.1 or later of Google Play services, you can configure your app to target the Android 6.0 Marshmallow SDK and use the new permissions model.

If your app supports the new permissions model, the user does not have to grant any permissions when they install or upgrade the app. Instead, the app must request permissions when it needs them at runtime, and the system shows a dialog to the user asking for the permission.

To learn more, see the documentation for Android 6.0 Marshmallow and the changes you must make to your app for the new permissions model.

Google has added WebChromeClient.onShowFileChooser. They even provide a way to automatically generate the file chooser intent so that it uses the input accept mime types.

Implement it in this way (from an answer by weiyin):

public class MyWebChromeClient extends WebChromeClient {
        // reference to activity instance. May be unnecessary if your web chrome client is member class.
    private MyActivity myActivity;

    public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
        // make sure there is no existing message
        if (myActivity.uploadMessage != null) {
            myActivity.uploadMessage.onReceiveValue(null);
            myActivity.uploadMessage = null;
        }

        myActivity.uploadMessage = filePathCallback;

        Intent intent = fileChooserParams.createIntent();
        try {
            myActivity.startActivityForResult(intent, MyActivity.REQUEST_SELECT_FILE);
        } catch (ActivityNotFoundException e) {
            myActivity.uploadMessage = null;
            Toast.makeText(myActivity, "Cannot open file chooser", Toast.LENGTH_LONG).show();
            return false;
        }

        return true;
    }
}


public class MyActivity extends ... {
    public static final int REQUEST_SELECT_FILE = 100;
    public ValueCallback<Uri[]> uploadMessage;

    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if (requestCode == REQUEST_SELECT_FILE) {
                if (uploadMessage == null) return;
                uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data));
                uploadMessage = null;
            }
        }
    }
}

Make sure to compile the app with API 21+. And this will work on all the platforms as you mention on your gradle.

CoolMind
  • 26,736
  • 15
  • 188
  • 224
Sibidharan
  • 2,717
  • 2
  • 26
  • 54
3

No answer is addressing the issue here.. some expert android people are giving solution for building image upload into android code.. that is not this question asking for.. I have same issue bothering me.. What we are asking is the HTML/PHP page which is loaded into webview app is not triggering gallery or camera app as it triggers 'upload file from PC' on a desktop computer/laptop in Mozilla or chrome.. It is like web from in iframe kind a thing.. but inside android app's web view.. I think I have made it clear.. We are not android programers we are web developers.

<form method="post" action="" enctype="multipart/form-data" name="form1">
<!-- this following input is not working in webview -->
<input size="25" name="file" type="file">
<input name="submit" type="submit" value="submit">
</form> 
Panjab Web
  • 35
  • 6
  • are you finded the solution sir ??? i have the same problem , i didn't find any solution – akira Aug 22 '18 at 02:35
  • @PanjabWeb I think you didn't understood the concern here. It's not the web code which has the issue, it's android webview's webclient class'es issue that it don't let the webpage's iFrame to open Android device's gallery or hardware camera to click or pick image from it (Android Security Patches). So, your android developer need to integrate the code to make it happen for your project/product. – Harpreet May 20 '19 at 12:16