質問編集履歴
1
文章の修正
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
createImageFileの疑問
|
body
CHANGED
@@ -1,126 +1,36 @@
|
|
1
1
|
###実現したいこと
|
2
|
-
|
2
|
+
WebViewアプリでファイルアップロードボタンを押したら、実機へのアルバムにアクセスすることができ、画像を選択することが出来ています。しかし、画像を選択後に画面に戻ると、画像の読み込みが出来ていません。
|
3
|
-
|
3
|
+
そこで、createImageFileが抜けているからと考え、付け加えました。しかし、読み込みが実現出来ていません。
|
4
|
-
上記2点の問題を抱えておりますが、どちらかでもよろしいので、何かお分かりであればお助けください。
|
5
4
|
|
6
|
-
###
|
5
|
+
###疑問
|
6
|
+
・このような現状の改善するためにcreateImageFileメソッドを取り入れましたが認識は合ってますか?
|
7
|
+
・エラーがないのですが、読み込めません。書き方に謝りがありますか?
|
7
|
-
|
8
|
+
・createImageFileが灰色になり、private method file createImageFile is never usedと警告があります。なぜでしょう。
|
8
|
-
選択した画像がちゃんとロード出来ていない事かと思いますが、どこの部分を変更したり付け加えれば良いか把握出来ずにおります> <。
|
9
|
-
|
10
9
|
###コード
|
11
10
|
```java
|
12
|
-
|
11
|
+
import java.io.File;
|
13
|
-
|
12
|
+
import java.io.IOException;
|
14
|
-
WebView webView;
|
15
|
-
|
13
|
+
import java.util.Date;
|
16
|
-
|
14
|
+
import java.text.SimpleDateFormat;
|
17
|
-
|
15
|
+
import android.os.Environment;
|
18
16
|
|
19
|
-
|
17
|
+
//上、onCreateメソッド
|
20
|
-
|
18
|
+
private File createImageFile() throws IOException {
|
21
|
-
|
19
|
+
// Create an image file name
|
20
|
+
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
|
21
|
+
String imageFileName = "JPEG_" + timeStamp + "_";
|
22
|
+
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
|
23
|
+
File image = File.createTempFile(
|
24
|
+
imageFileName, /* prefix */
|
25
|
+
".jpg", /* suffix */
|
22
|
-
|
26
|
+
storageDir /* directory */
|
27
|
+
);
|
23
28
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
}
|
28
|
-
assert webView != null;
|
29
|
-
|
30
|
-
WebSettings webSettings = webView.getSettings();
|
31
|
-
webSettings.setJavaScriptEnabled(true);
|
32
|
-
webSettings.setAllowFileAccess(true);
|
33
|
-
webView.getSettings().setLoadWithOverviewMode(true);
|
34
|
-
webView.getSettings().setUseWideViewPort(true);
|
35
|
-
webView.getSettings().setSupportZoom(true);
|
36
|
-
webView.getSettings().setBuiltInZoomControls(true);
|
37
|
-
if (Build.VERSION.SDK_INT >= 21) {
|
38
|
-
webSettings.setMixedContentMode(0);
|
39
|
-
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
|
40
|
-
} else if (Build.VERSION.SDK_INT >= 19) {
|
41
|
-
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
|
42
|
-
} else if (Build.VERSION.SDK_INT < 19) {
|
43
|
-
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
|
44
|
-
}
|
45
|
-
webView.setWebViewClient(new Callback());
|
46
|
-
//webView.loadUrl("https://infeeds.com/");
|
47
|
-
webView.loadUrl("https://google.com");
|
48
|
-
|
49
|
-
webView.setWebChromeClient(new WebChromeClient() {
|
50
|
-
|
51
|
-
//For Android 3.0+
|
52
|
-
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
|
53
|
-
|
54
|
-
mUM = uploadMsg;
|
55
|
-
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
|
56
|
-
i.addCategory(Intent.CATEGORY_OPENABLE);
|
57
|
-
i.setType("*/*");
|
58
|
-
MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), FCR);
|
59
|
-
}
|
60
|
-
|
61
|
-
// For Android 3.0+, above method not supported in some android 3+ versions, in such case we use this
|
62
|
-
public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
|
63
|
-
|
64
|
-
mUM = uploadMsg;
|
65
|
-
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
|
66
|
-
i.addCategory(Intent.CATEGORY_OPENABLE);
|
67
|
-
i.setType("*/*");
|
68
|
-
MainActivity.this.startActivityForResult(
|
69
|
-
Intent.createChooser(i, "File Browser"),
|
70
|
-
FCR);
|
71
|
-
}
|
72
|
-
|
73
|
-
//For Android 4.1+
|
74
|
-
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
|
75
|
-
|
76
|
-
mUM = uploadMsg;
|
77
|
-
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
|
78
|
-
i.addCategory(Intent.CATEGORY_OPENABLE);
|
79
|
-
i.setType("*/*");
|
80
|
-
MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), MainActivity.FCR);
|
81
|
-
}
|
82
|
-
|
83
|
-
//For Android 5.0+
|
84
|
-
public boolean onShowFileChooser(
|
85
|
-
WebView webView, ValueCallback<Uri[]> filePathCallback,
|
86
|
-
WebChromeClient.FileChooserParams fileChooserParams) {
|
87
|
-
|
88
|
-
if (mUMA != null) {
|
89
|
-
mUMA.onReceiveValue(null);
|
90
|
-
}
|
91
|
-
|
92
|
-
mUMA = filePathCallback;
|
93
|
-
// Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
|
94
|
-
|
95
|
-
|
96
|
-
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
|
97
|
-
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
|
98
|
-
contentSelectionIntent.setType("*/*");
|
99
|
-
// Intent[] intentArray;
|
100
|
-
//
|
101
|
-
// if (takePictureIntent != null) {
|
102
|
-
// intentArray = new Intent[]{takePictureIntent};
|
103
|
-
// } else {
|
104
|
-
// intentArray = new Intent[0];
|
105
|
-
// }
|
106
|
-
|
107
|
-
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
|
108
|
-
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
|
109
|
-
// chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
|
110
|
-
// chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
|
111
|
-
startActivityForResult(chooserIntent, FCR);
|
112
|
-
|
113
|
-
return true;
|
114
|
-
}
|
115
|
-
|
116
|
-
});
|
29
|
+
// Save a file: path for use with ACTION_VIEW intents
|
30
|
+
currentPhotoPath = image.getAbsolutePath();
|
31
|
+
return image;
|
117
32
|
}
|
118
|
-
|
33
|
+
//下、onActivityResultメソッド
|
119
|
-
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
|
120
|
-
Toast.makeText(getApplicationContext(), "Failed loading app!", Toast.LENGTH_SHORT).show();
|
121
|
-
}
|
122
|
-
}
|
123
|
-
}
|
124
34
|
|
125
35
|
```
|
126
36
|
|