質問編集履歴

1

文章の修正

2019/10/15 15:30

投稿

tomtom1
tomtom1

スコア168

test CHANGED
@@ -1 +1 @@
1
- Android ファイルアップロード方法
1
+ createImageFile疑問
test CHANGED
@@ -1,248 +1,68 @@
1
1
  ###実現したいこと
2
2
 
3
- WebViewでファイルアップロードを出来ずにいます。(ファイルの選択までは出来ます)
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
- public class MainActivity extends AppCompatActivity {
21
+ import java.io.File;
24
22
 
25
- private final static int FCR = 1;
23
+ import java.io.IOException;
26
24
 
27
- WebView webView;
25
+ import java.util.Date;
28
26
 
29
- private String mCM;
27
+ import java.text.SimpleDateFormat;
30
28
 
31
- private ValueCallback<Uri> mUM;
29
+ import android.os.Environment;
32
-
33
- private ValueCallback<Uri[]> mUMA;
34
30
 
35
31
 
36
32
 
37
- @Override
33
+ //上、onCreateメソッド
38
34
 
39
- protected void onCreate(Bundle savedInstanceState) {
35
+ private File createImageFile() throws IOException {
40
36
 
41
- super.onCreate(savedInstanceState);
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
- setContentView(R.layout.activity_main);
51
+ storageDir /* directory */
52
+
53
+ );
44
54
 
45
55
 
46
56
 
47
- webView = findViewById(R.id.webview);
57
+ // Save a file: path for use with ACTION_VIEW intents
48
58
 
49
- 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)) {
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
- return true;
61
+ return image;
226
-
227
- }
228
-
229
-
230
-
231
- });
232
62
 
233
63
  }
234
64
 
235
- public class Callback extends WebViewClient {
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