質問編集履歴
1
文章の修正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
createImageFileの疑問
|
test
CHANGED
@@ -1,248 +1,68 @@
|
|
1
1
|
###実現したいこと
|
2
2
|
|
3
|
-
|
3
|
+
WebViewアプリでファイルアップロードボタンを押したら、実機へのアルバムにアクセスすることができ、画像を選択することが出来ています。しかし、画像を選択後に画面に戻ると、画像の読み込みが出来ていません。
|
4
4
|
|
5
|
-
|
5
|
+
そこで、createImageFileが抜けているからと考え、付け加えました。しかし、読み込みが実現出来ていません。
|
6
|
-
|
7
|
-
上記2点の問題を抱えておりますが、どちらかでもよろしいので、何かお分かりであればお助けください。
|
8
6
|
|
9
7
|
|
10
8
|
|
11
|
-
###
|
9
|
+
###疑問
|
12
10
|
|
13
|
-
現状
|
11
|
+
・このような現状の改善するためにcreateImageFileメソッドを取り入れましたが認識は合ってますか?
|
14
12
|
|
15
|
-
|
13
|
+
・エラーがないのですが、読み込めません。書き方に謝りがありますか?
|
16
14
|
|
17
|
-
|
15
|
+
・createImageFileが灰色になり、private method file createImageFile is never usedと警告があります。なぜでしょう。
|
18
16
|
|
19
17
|
###コード
|
20
18
|
|
21
19
|
```java
|
22
20
|
|
23
|
-
p
|
21
|
+
import java.io.File;
|
24
22
|
|
25
|
-
|
23
|
+
import java.io.IOException;
|
26
24
|
|
27
|
-
|
25
|
+
import java.util.Date;
|
28
26
|
|
29
|
-
|
27
|
+
import java.text.SimpleDateFormat;
|
30
28
|
|
31
|
-
|
29
|
+
import android.os.Environment;
|
32
|
-
|
33
|
-
private ValueCallback<Uri[]> mUMA;
|
34
30
|
|
35
31
|
|
36
32
|
|
37
|
-
|
33
|
+
//上、onCreateメソッド
|
38
34
|
|
39
|
-
|
35
|
+
private File createImageFile() throws IOException {
|
40
36
|
|
41
|
-
|
37
|
+
// Create an image file name
|
42
38
|
|
39
|
+
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
|
40
|
+
|
41
|
+
String imageFileName = "JPEG_" + timeStamp + "_";
|
42
|
+
|
43
|
+
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
|
44
|
+
|
45
|
+
File image = File.createTempFile(
|
46
|
+
|
47
|
+
imageFileName, /* prefix */
|
48
|
+
|
49
|
+
".jpg", /* suffix */
|
50
|
+
|
43
|
-
s
|
51
|
+
storageDir /* directory */
|
52
|
+
|
53
|
+
);
|
44
54
|
|
45
55
|
|
46
56
|
|
47
|
-
|
57
|
+
// Save a file: path for use with ACTION_VIEW intents
|
48
58
|
|
49
|
-
|
59
|
+
currentPhotoPath = image.getAbsolutePath();
|
50
60
|
|
51
|
-
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);
|
52
|
-
|
53
|
-
}
|
54
|
-
|
55
|
-
assert webView != null;
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
WebSettings webSettings = webView.getSettings();
|
60
|
-
|
61
|
-
webSettings.setJavaScriptEnabled(true);
|
62
|
-
|
63
|
-
webSettings.setAllowFileAccess(true);
|
64
|
-
|
65
|
-
webView.getSettings().setLoadWithOverviewMode(true);
|
66
|
-
|
67
|
-
webView.getSettings().setUseWideViewPort(true);
|
68
|
-
|
69
|
-
webView.getSettings().setSupportZoom(true);
|
70
|
-
|
71
|
-
webView.getSettings().setBuiltInZoomControls(true);
|
72
|
-
|
73
|
-
if (Build.VERSION.SDK_INT >= 21) {
|
74
|
-
|
75
|
-
webSettings.setMixedContentMode(0);
|
76
|
-
|
77
|
-
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
|
78
|
-
|
79
|
-
} else if (Build.VERSION.SDK_INT >= 19) {
|
80
|
-
|
81
|
-
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
|
82
|
-
|
83
|
-
} else if (Build.VERSION.SDK_INT < 19) {
|
84
|
-
|
85
|
-
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
|
86
|
-
|
87
|
-
}
|
88
|
-
|
89
|
-
webView.setWebViewClient(new Callback());
|
90
|
-
|
91
|
-
//webView.loadUrl("https://infeeds.com/");
|
92
|
-
|
93
|
-
webView.loadUrl("https://google.com");
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
webView.setWebChromeClient(new WebChromeClient() {
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
//For Android 3.0+
|
102
|
-
|
103
|
-
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
mUM = uploadMsg;
|
108
|
-
|
109
|
-
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
|
110
|
-
|
111
|
-
i.addCategory(Intent.CATEGORY_OPENABLE);
|
112
|
-
|
113
|
-
i.setType("*/*");
|
114
|
-
|
115
|
-
MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), FCR);
|
116
|
-
|
117
|
-
}
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
// For Android 3.0+, above method not supported in some android 3+ versions, in such case we use this
|
122
|
-
|
123
|
-
public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
mUM = uploadMsg;
|
128
|
-
|
129
|
-
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
|
130
|
-
|
131
|
-
i.addCategory(Intent.CATEGORY_OPENABLE);
|
132
|
-
|
133
|
-
i.setType("*/*");
|
134
|
-
|
135
|
-
MainActivity.this.startActivityForResult(
|
136
|
-
|
137
|
-
Intent.createChooser(i, "File Browser"),
|
138
|
-
|
139
|
-
FCR);
|
140
|
-
|
141
|
-
}
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
//For Android 4.1+
|
146
|
-
|
147
|
-
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
mUM = uploadMsg;
|
152
|
-
|
153
|
-
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
|
154
|
-
|
155
|
-
i.addCategory(Intent.CATEGORY_OPENABLE);
|
156
|
-
|
157
|
-
i.setType("*/*");
|
158
|
-
|
159
|
-
MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), MainActivity.FCR);
|
160
|
-
|
161
|
-
}
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
//For Android 5.0+
|
166
|
-
|
167
|
-
public boolean onShowFileChooser(
|
168
|
-
|
169
|
-
WebView webView, ValueCallback<Uri[]> filePathCallback,
|
170
|
-
|
171
|
-
WebChromeClient.FileChooserParams fileChooserParams) {
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
if (mUMA != null) {
|
176
|
-
|
177
|
-
mUMA.onReceiveValue(null);
|
178
|
-
|
179
|
-
}
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
mUMA = filePathCallback;
|
184
|
-
|
185
|
-
// Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
|
192
|
-
|
193
|
-
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
|
194
|
-
|
195
|
-
contentSelectionIntent.setType("*/*");
|
196
|
-
|
197
|
-
// Intent[] intentArray;
|
198
|
-
|
199
|
-
//
|
200
|
-
|
201
|
-
// if (takePictureIntent != null) {
|
202
|
-
|
203
|
-
// intentArray = new Intent[]{takePictureIntent};
|
204
|
-
|
205
|
-
// } else {
|
206
|
-
|
207
|
-
// intentArray = new Intent[0];
|
208
|
-
|
209
|
-
// }
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
|
214
|
-
|
215
|
-
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
|
216
|
-
|
217
|
-
// chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
|
218
|
-
|
219
|
-
// chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
|
220
|
-
|
221
|
-
startActivityForResult(chooserIntent, FCR);
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
61
|
+
return image;
|
226
|
-
|
227
|
-
}
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
});
|
232
62
|
|
233
63
|
}
|
234
64
|
|
235
|
-
|
65
|
+
//下、onActivityResultメソッド
|
236
|
-
|
237
|
-
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
|
238
|
-
|
239
|
-
Toast.makeText(getApplicationContext(), "Failed loading app!", Toast.LENGTH_SHORT).show();
|
240
|
-
|
241
|
-
}
|
242
|
-
|
243
|
-
}
|
244
|
-
|
245
|
-
}
|
246
66
|
|
247
67
|
|
248
68
|
|