我正在尝试构建一个仅打开webview并转到我网站上的论坛页面的应用程序,但是我不知道为什么我不能上传照片。我不认为权限不是问题,因为我将应用程序设置为在打开时请求权限(尽管您必须打开应用程序两次才能同时获得相机提示和读/写权限提示),当我单击“上传照片”时,什么都没有发生。该网站可以在普通的Web浏览器应用程序中正常运行。该网站是用Wix建立的。
我已经阅读了类似问题的一百种答案,或者没有一项工作,它们已经过时,或者我什么都不懂。我读过一些东西,说这与Java警报或使用WebChromeClient有关,但我最大的问题是我真的不知道真正的问题是试图找到答案或如何实际编写解决方案。
我将所有代码都放在这里,但我不仅想要解决该问题的答案,还想知道为什么它不起作用,以及如何找出解决方法,例如将来这些都是我自己的。
活动主要XML
<?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"
android:background="#000000"
tools:context=".MainActivity">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000" />
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
MainActivity JAVA
package inventory.csdc.com.csdcinventory;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
public class MainActivity extends AppCompatActivity {
private AdView mAdView;
WebView webview;
private static final String TAG = "MainActivity";
private static final String [] CAMERA_PERMISSIONS = {
Manifest.permission.CAMERA};
private static final String [] STORAGE_PERMISSIONS ={
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
verifyPermissions();
webview = findViewById(R.id.webview);
webview.getSettings() .setJavaScriptEnabled(true);
webview.getSettings() .setJavaScriptCanOpenWindowsAutomatically(true);
webview.getSettings() .setRenderPriority(WebSettings.RenderPriority.HIGH);
webview.getSettings() .setAppCacheEnabled(true);
webview.loadUrl("https://funforums.wixsite.com/csdc");
webview.setWebViewClient(new WebViewClient());
webview.setWebChromeClient(new WebChromeClient());
webview.getSettings() .setUseWideViewPort(true);
MobileAds.initialize(this, "MY ADMOB ID");
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
webview.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
private void verifyPermissions(){
Log.d(TAG, "verifyPermissions: Checking Permissions");
int permissionCamera = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA);
int permissionExternalMemory = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionExternalMemory != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
MainActivity.this,
STORAGE_PERMISSIONS,
1
);
}
if (permissionCamera != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(
MainActivity.this,
CAMERA_PERMISSIONS,
1
);
}
}
}
AndroidManifest XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="inventory.csdc.com.csdcinventory">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_dance2"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="[MY ADMOB ID]" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
构建gradle(应用程序)
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "inventory.csdc.com.csdcinventory"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.android.gms:play-services-ads:17.0.0'
implementation 'com.android.support:customtabs:27.1.1'
}
构建gradle(CSDCInventory2)
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven {
url "https://maven.google.com"
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}