M
M
micsma2021-09-13 07:00:30
JavaScript
micsma, 2021-09-13 07:00:30

android studio webview how to upload a file?

Question: Why doesn't the selection menu appear when I press the button?
ps I understand that it may be necessary to connect something else in MainActivity.java.
but on the PC version everything is fine ..


MainActivity.java code

package com.example.hello;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {

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

        WebView web = (WebView)findViewById(R.id.webView);
        web.getSettings().setJavaScriptEnabled(true);
        web.setWebChromeClient(new WebChromeClient());
        web.loadUrl("file:///android_asset/www/index.html");
        web.getSettings().setDomStorageEnabled(true);

        web.getSettings().setDomStorageEnabled(true);
        web.getSettings().setDatabaseEnabled(true);
        web.getSettings().setDatabasePath("/data/data/" + web.getContext().getPackageName() + "/databases/");

    }
}


activity_main.xml code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>


index.html code
<input type="file" id="img" accept="image/*" onchange="fileupload(this)">


app.js code
function fileupload(input) {
  let file = input.files[0];
  let reader = new FileReader();
  reader.readAsDataURL(file);

  reader.onload = function () {
    localStorage.setItem('img', reader.result);
  }
}


Personally, this is my first time dealing with WebView and in general with Andoid Studio, thanks in advance

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question