【问题标题】:How to allow a WebView app to upload a picture from the Gallery of the device on Android Studio?如何允许 WebView 应用从 Android Studio 设备的图库中上传图片?
【发布时间】:2015-12-13 01:07:40
【问题描述】:

我在 WebView 中的第一个应用程序中。我的应用程序中有一个页面,允许用户从设备的照片库中上传图片以更改头像。

但是,当我测试应用程序时,该按钮不执行任何操作。我点击,点击,点击,双击!什么都没有发生...

我看过很多帖子,我也尝试了很多,似乎没有任何效果,有人说在 webview 中不可能,其他人说它不再是棒棒糖等相同的方法......换句话说:我迷路了!

这是我的 MainActivity.java 的代码:

package com.example.app;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;



public class MainActivity extends Activity {

    private WebView mWebView;

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

        mWebView = (WebView) findViewById(R.id.activity_main_webview);

        // Force links and redirects to open in the WebView instead of in a browser
        mWebView.setWebViewClient(new WebViewClient());

        // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        // Use remote resource
        mWebView.loadUrl("http://urlofmywebsite");

        // Stop local links and redirects from opening in browser instead of WebView
        //mWebView.setWebViewClient(new MyAppWebViewClient());

        // Use local resource
        // mWebView.loadUrl("file:///android_asset/www/index.html");

    }

    // Prevent the back-button from closing the app
    @Override
    public void onBackPressed() {
        if(mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

【问题讨论】:

    标签: android webview upload


    【解决方案1】:

    你想先从你的服务器获取关于你想发送的图片的链接,然后简单地将 URL 加载到 webview 或者你希望图片在本地发送。

    【讨论】:

    • 嗯,我的网站是一个 Wordpress。所以我不知道该怎么办。只有一个页面,用户可以为他的个人资料图片上传图片(Buddypress 插件)。所以通常,他们点击“发送图片”按钮并打开画廊,他们选择他们的图片和basta......就像浏览器一样,它工作正常。只是在 webview 上,它是禁用的......所以告诉我最好的!这是我正在使用的 webview 的代码:github.com/slymax/webview
    【解决方案2】:

    您需要将本地文件更改为 bytearray 并将其转换为 Base64 字符串,并且需要使用该内容访问您的服务器 URL 使用一些关键字来识别此内容,然后需要将其更改为服务器中的它以获取该内容.如果您需要代码帮助,请告知。另一种选择是使用一些服务将内容发送到服务器 URL 链接并将其发送到您的服务器 URL,即 http URL。

    【讨论】:

    • 您好,感谢您的快速答复。然而,我是一个完全的菜鸟,我所理解的只是我深陷其中。所以是的,如果你能给我放什么和放在哪里,那将不胜感激!
    猜你喜欢
    • 1970-01-01
    • 2017-12-13
    • 2017-06-13
    • 1970-01-01
    • 2011-06-05
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    相关资源
    最近更新 更多