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

質問編集履歴

1

文章の修正

2019/10/15 15:30

投稿

tomtom1
tomtom1

スコア168

title CHANGED
@@ -1,1 +1,1 @@
1
- Android ファイルアップロード方法
1
+ createImageFile疑問
body CHANGED
@@ -1,126 +1,36 @@
1
1
  ###実現したいこと
2
- WebViewでファイルアップロードを出来ずにいます。(ファイルの選択までは出来ます)
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
- public class MainActivity extends AppCompatActivity {
11
+ import java.io.File;
13
- private final static int FCR = 1;
12
+ import java.io.IOException;
14
- WebView webView;
15
- private String mCM;
13
+ import java.util.Date;
16
- private ValueCallback<Uri> mUM;
14
+ import java.text.SimpleDateFormat;
17
- private ValueCallback<Uri[]> mUMA;
15
+ import android.os.Environment;
18
16
 
19
- @Override
17
+ //上、onCreateメソッド
20
- protected void onCreate(Bundle savedInstanceState) {
18
+ private File createImageFile() throws IOException {
21
- super.onCreate(savedInstanceState);
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
- setContentView(R.layout.activity_main);
26
+ storageDir /* directory */
27
+ );
23
28
 
24
- webView = findViewById(R.id.webview);
25
- 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)) {
26
- ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);
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
- public class Callback extends WebViewClient {
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