teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

追記

2019/10/15 07:28

投稿

tomtom1
tomtom1

スコア168

title CHANGED
File without changes
body CHANGED
@@ -4,7 +4,8 @@
4
4
  アルバムから画像を選択するというところまでたどり着きましたが、選択した画像の読み込みが出来ません。そこで、onActivityResultを追加しましたが、表題のエラーが出てしまいます。(onActivityResult部分を追加するまでは正常に稼働します!)
5
5
  自分見る限り、メソッドも正しく閉じられている気がするのですが、何故このようなエラーが起きるのでしょうか..。
6
6
 
7
- ###コード
7
+ ###コード・エラーまでの経緯
8
+ 現在はこの様なコードになっているます。画像の読み込みが出来ないため、onActivityResultメソッドを取り組みたいと思います。
8
9
  ```java
9
10
  public class MainActivity extends AppCompatActivity {
10
11
  private final static int FCR = 1;
@@ -115,7 +116,18 @@
115
116
 
116
117
  }
117
118
  //↓ここからを追加した際にエラー発生
119
+ //ここにonActivityResultコードを追加すると、、、
120
+ //ここまで
121
+ public class Callback extends WebViewClient {
122
+ public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
123
+ Toast.makeText(getApplicationContext(), "Failed loading app!", Toast.LENGTH_SHORT).show();
124
+ }
125
+ }
126
+ }
127
+ ```
128
+ 付け加えるonActivityResultメソッドは以下になります。
129
+ ```ここに言語を入力
118
- @Override
130
+ @Override
119
131
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
120
132
  if (requestCode == INPUT_FILE_REQUEST_CODE) {
121
133
  ValueCallback mFilePathCallback;
@@ -136,12 +148,13 @@
136
148
  mFilePathCallback = null;
137
149
  }
138
150
  }
139
- //ここまで
140
- public class Callback extends WebViewClient {
141
- public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
142
- Toast.makeText(getApplicationContext(), "Failed loading app!", Toast.LENGTH_SHORT).show();
143
- }
144
- }
145
- }
146
151
  ```
152
+ 上記コードを付け加えると、INPUT_FILE_REQUEST_CODEがエラーになるため、
153
+ Create constant fieldをします。
154
+
155
+ ![イメージ説明](a04c0244619e1926dfb3181dcbfde814.png)
156
+
157
+ そうすると下記の様に加えられますが、この時からビルドすると、表題のエラーが発生します。
158
+ ![イメージ説明](75924cca2ed10a0c6dd20b21ac744dd1.png)
159
+
147
160
  よろしくお願いします。

1

追加

2019/10/15 07:28

投稿

tomtom1
tomtom1

スコア168

title CHANGED
File without changes
body CHANGED
@@ -6,7 +6,116 @@
6
6
 
7
7
  ###コード
8
8
  ```java
9
- @Override
9
+ public class MainActivity extends AppCompatActivity {
10
+ private final static int FCR = 1;
11
+ private static final int INPUT_FILE_REQUEST_CODE = ;
12
+ WebView webView;
13
+ private String mCM;
14
+ private ValueCallback<Uri> mUM;
15
+ private ValueCallback<Uri[]> mUMA;
16
+
17
+ @Override
18
+ protected void onCreate(Bundle savedInstanceState) {
19
+ super.onCreate(savedInstanceState);
20
+ setContentView(R.layout.activity_main);
21
+
22
+ webView = findViewById(R.id.webview);
23
+ if (Build.VERSION.SDK_INT >= 23 && (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)) {
24
+ ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);
25
+ }
26
+ assert webView != null;
27
+
28
+ WebSettings webSettings = webView.getSettings();
29
+ webSettings.setJavaScriptEnabled(true);
30
+ webSettings.setAllowFileAccess(true);
31
+ webView.getSettings().setLoadWithOverviewMode(true);
32
+ webView.getSettings().setUseWideViewPort(true);
33
+ webView.getSettings().setSupportZoom(true);
34
+ webView.getSettings().setBuiltInZoomControls(true);
35
+ if (Build.VERSION.SDK_INT >= 21) {
36
+ webSettings.setMixedContentMode(0);
37
+ webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
38
+ } else if (Build.VERSION.SDK_INT >= 19) {
39
+ webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
40
+ } else if (Build.VERSION.SDK_INT < 19) {
41
+ webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
42
+ }
43
+ webView.setWebViewClient(new Callback());
44
+ //webView.loadUrl("https://infeeds.com/");
45
+ webView.loadUrl("https://google.com");
46
+
47
+ webView.setWebChromeClient(new WebChromeClient() {
48
+
49
+ //For Android 3.0+
50
+ public void openFileChooser(ValueCallback<Uri> uploadMsg) {
51
+
52
+ mUM = uploadMsg;
53
+ Intent i = new Intent(Intent.ACTION_GET_CONTENT);
54
+ i.addCategory(Intent.CATEGORY_OPENABLE);
55
+ i.setType("*/*");
56
+ MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), FCR);
57
+ }
58
+
59
+ // For Android 3.0+, above method not supported in some android 3+ versions, in such case we use this
60
+ public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
61
+
62
+ mUM = uploadMsg;
63
+ Intent i = new Intent(Intent.ACTION_GET_CONTENT);
64
+ i.addCategory(Intent.CATEGORY_OPENABLE);
65
+ i.setType("*/*");
66
+ MainActivity.this.startActivityForResult(
67
+ Intent.createChooser(i, "File Browser"),
68
+ FCR);
69
+ }
70
+
71
+ //For Android 4.1+
72
+ public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
73
+
74
+ mUM = uploadMsg;
75
+ Intent i = new Intent(Intent.ACTION_GET_CONTENT);
76
+ i.addCategory(Intent.CATEGORY_OPENABLE);
77
+ i.setType("*/*");
78
+ MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), MainActivity.FCR);
79
+ }
80
+
81
+ //For Android 5.0+
82
+ public boolean onShowFileChooser(
83
+ WebView webView, ValueCallback<Uri[]> filePathCallback,
84
+ WebChromeClient.FileChooserParams fileChooserParams) {
85
+
86
+ if (mUMA != null) {
87
+ mUMA.onReceiveValue(null);
88
+ }
89
+
90
+ mUMA = filePathCallback;
91
+ Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
92
+
93
+
94
+ Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
95
+ contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
96
+ contentSelectionIntent.setType("*/*");
97
+ Intent[] intentArray;
98
+
99
+ if (takePictureIntent != null) {
100
+ intentArray = new Intent[]{takePictureIntent};
101
+ } else {
102
+ intentArray = new Intent[0];
103
+ }
104
+
105
+ Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
106
+ chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
107
+ chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
108
+ chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
109
+ startActivityForResult(chooserIntent, FCR);
110
+
111
+ return true;
112
+ }
113
+
114
+ });
115
+
116
+ }
117
+ //↓ここからを追加した際にエラー発生
118
+ @Override
10
119
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
11
120
  if (requestCode == INPUT_FILE_REQUEST_CODE) {
12
121
  ValueCallback mFilePathCallback;
@@ -27,5 +136,12 @@
27
136
  mFilePathCallback = null;
28
137
  }
29
138
  }
139
+ //ここまで
140
+ public class Callback extends WebViewClient {
141
+ public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
142
+ Toast.makeText(getApplicationContext(), "Failed loading app!", Toast.LENGTH_SHORT).show();
143
+ }
144
+ }
145
+ }
30
146
  ```
31
147
  よろしくお願いします。