質問編集履歴
4
コードとエラーコードを更新しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -19,32 +19,39 @@
|
|
19
19
|
|
20
20
|
以下エラーログです。
|
21
21
|
```ここに言語を入力
|
22
|
-
|
23
|
-
08-
|
24
|
-
08-
|
25
|
-
08-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
at android.
|
30
|
-
at
|
31
|
-
at android.
|
32
|
-
at android.
|
33
|
-
at android.
|
34
|
-
at android.
|
35
|
-
at android.
|
36
|
-
at
|
37
|
-
at android.
|
38
|
-
at android.
|
39
|
-
|
40
|
-
at
|
41
|
-
at
|
42
|
-
at
|
43
|
-
at android.
|
44
|
-
at android.app.
|
45
|
-
at
|
46
|
-
at
|
47
|
-
at
|
22
|
+
08-26 17:04:36.424 6846-6846パッケージ名 I/Process: Sending signal. PID: 6846 SIG: 9
|
23
|
+
08-26 17:04:37.671 6940-6940パッケージ名 W/System: ClassLoader referenced unknown path: /data/appパッケージ名-1/lib/arm64
|
24
|
+
08-26 17:04:37.773 6940-6940パッケージ名 W/System: ClassLoader referenced unknown path: /data/appパッケージ名-1/lib/arm64
|
25
|
+
08-26 17:04:37.827 6940-6940パッケージ名 D/AndroidRuntime: Shutting down VM
|
26
|
+
08-26 17:04:37.827 6940-6940パッケージ名 E/AndroidRuntime: FATAL EXCEPTION: main
|
27
|
+
Process: パッケージ名, PID: 6940
|
28
|
+
java.lang.RuntimeException: Unable to start activity ComponentInfo{パッケージ名パッケージ名.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
|
29
|
+
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2455)
|
30
|
+
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2541)
|
31
|
+
at android.app.ActivityThread.access$900(ActivityThread.java:169)
|
32
|
+
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1382)
|
33
|
+
at android.os.Handler.dispatchMessage(Handler.java:102)
|
34
|
+
at android.os.Looper.loop(Looper.java:148)
|
35
|
+
at android.app.ActivityThread.main(ActivityThread.java:5554)
|
36
|
+
at java.lang.reflect.Method.invoke(Native Method)
|
37
|
+
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746)
|
38
|
+
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
|
39
|
+
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
|
40
|
+
at パッケージ名.MainActivity.requestPermissions(MainActivity.java:123)
|
41
|
+
at パッケージ名.MainActivity.checkPermission(MainActivity.java:112)
|
42
|
+
at パッケージ名.MainActivity.onCreate(MainActivity.java:50)
|
43
|
+
at android.app.Activity.performCreate(Activity.java:6285)
|
44
|
+
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
|
45
|
+
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2408)
|
46
|
+
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2541)
|
47
|
+
at android.app.ActivityThread.access$900(ActivityThread.java:169)
|
48
|
+
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1382)
|
49
|
+
at android.os.Handler.dispatchMessage(Handler.java:102)
|
50
|
+
at android.os.Looper.loop(Looper.java:148)
|
51
|
+
at android.app.ActivityThread.main(ActivityThread.java:5554)
|
52
|
+
at java.lang.reflect.Method.invoke(Native Method)
|
53
|
+
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746)
|
54
|
+
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
|
48
55
|
|
49
56
|
```
|
50
57
|
|
@@ -77,6 +84,10 @@
|
|
77
84
|
|
78
85
|
|
79
86
|
super.onCreate( savedInstanceState );
|
87
|
+
File dirs = new File( "storage" );
|
88
|
+
if (!dirs.exists()) {
|
89
|
+
dirs.mkdir();
|
90
|
+
}
|
80
91
|
|
81
92
|
|
82
93
|
}
|
@@ -98,10 +109,7 @@
|
|
98
109
|
Log.v( TAG, "Holder" + holder );
|
99
110
|
|
100
111
|
|
101
|
-
|
112
|
+
|
102
|
-
if (!dirs.exists()) {
|
103
|
-
dirs.mkdir();
|
104
|
-
}
|
105
113
|
}
|
106
114
|
|
107
115
|
|
@@ -141,11 +149,25 @@
|
|
141
149
|
|
142
150
|
|
143
151
|
private void requestPermissions(boolean camP, boolean recP, boolean strP) {
|
152
|
+
boolean pList[]= new boolean[2];
|
153
|
+
pList[0] = camP;
|
154
|
+
pList[1] = recP;
|
155
|
+
pList[2] = strP;
|
144
156
|
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
157
|
+
for (int i=0;i<pList.length;i++){
|
158
|
+
if (pList[i]){
|
159
|
+
switch (i){
|
160
|
+
case 0:
|
161
|
+
ActivityCompat.requestPermissions( MainActivity.this, new String[]{Manifest.permission.CAMERA}, REQUEST_PERMISSION );
|
162
|
+
break;
|
163
|
+
case 1:
|
164
|
+
ActivityCompat.requestPermissions( MainActivity.this, new String[]{Manifest.permission.RECORD_AUDIO}, REQUEST_PERMISSION );
|
165
|
+
break;
|
166
|
+
case 2:
|
167
|
+
ActivityCompat.requestPermissions( MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION );
|
168
|
+
break;
|
169
|
+
}
|
170
|
+
}
|
149
171
|
}
|
150
172
|
|
151
173
|
|
@@ -166,6 +188,8 @@
|
|
166
188
|
}
|
167
189
|
}
|
168
190
|
|
191
|
+
|
192
|
+
|
169
193
|
以下文字数の関係で省略。
|
170
194
|
|
171
195
|
|
3
8/25現在のコードに更新しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -71,18 +71,16 @@
|
|
71
71
|
|
72
72
|
@Override
|
73
73
|
protected void onCreate(Bundle savedInstanceState) {
|
74
|
-
if (Build.VERSION.SDK_INT >= 23) {
|
75
|
-
checkPermission();
|
76
|
-
Log.v( "Check","permission" );
|
77
74
|
|
78
|
-
|
75
|
+
checkPermission();
|
76
|
+
Log.v( "Check", "permission" );
|
79
77
|
|
80
78
|
|
79
|
+
super.onCreate( savedInstanceState );
|
81
80
|
|
82
|
-
super.onCreate( savedInstanceState );
|
83
81
|
|
84
|
-
|
82
|
+
}
|
85
|
-
|
83
|
+
private void prepareCamera(){
|
86
84
|
setContentView( R.layout.activity_main );
|
87
85
|
btn1 = (Button) findViewById( R.id.button1 );
|
88
86
|
btn1.setOnClickListener( this );
|
@@ -106,9 +104,9 @@
|
|
106
104
|
}
|
107
105
|
}
|
108
106
|
|
109
|
-
}
|
110
107
|
|
111
108
|
|
109
|
+
|
112
110
|
private boolean checkPermission() {
|
113
111
|
boolean camP;
|
114
112
|
|
@@ -138,6 +136,7 @@
|
|
138
136
|
requestPermissions(camP,recP,strP);
|
139
137
|
Log.v( "request","permission" );
|
140
138
|
return camP && recP && strP;
|
139
|
+
|
141
140
|
}
|
142
141
|
|
143
142
|
|
@@ -160,13 +159,13 @@
|
|
160
159
|
if (requestCode == REQUEST_PERMISSION) {
|
161
160
|
// 使用が許可された
|
162
161
|
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
162
|
+
prepareCamera();
|
163
163
|
return;
|
164
164
|
|
165
165
|
}
|
166
166
|
}
|
167
167
|
}
|
168
168
|
|
169
|
-
|
170
169
|
以下文字数の関係で省略。
|
171
170
|
|
172
171
|
|
2
8/25現在のコードに変更しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -51,6 +51,7 @@
|
|
51
51
|
|
52
52
|
###該当のソースコード
|
53
53
|
```ここに言語を入力
|
54
|
+
|
54
55
|
public class MainActivity extends Activity implements View.OnClickListener {
|
55
56
|
private SurfaceView mySurfaceView;
|
56
57
|
private Camera camera;
|
@@ -64,96 +65,103 @@
|
|
64
65
|
private Button btn1;
|
65
66
|
private Button btn2;
|
66
67
|
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
|
67
72
|
@Override
|
68
73
|
protected void onCreate(Bundle savedInstanceState) {
|
69
|
-
if(Build.VERSION.SDK_INT >= 23){
|
74
|
+
if (Build.VERSION.SDK_INT >= 23) {
|
70
75
|
checkPermission();
|
71
|
-
|
76
|
+
Log.v( "Check","permission" );
|
72
77
|
|
73
|
-
|
78
|
+
}else {
|
74
|
-
setContentView( R.layout.activity_main );
|
75
79
|
|
76
80
|
|
77
81
|
|
82
|
+
super.onCreate( savedInstanceState );
|
78
83
|
|
79
84
|
|
80
|
-
btn1 = (Button) findViewById( R.id.button1 );
|
81
|
-
btn1.setOnClickListener( this );
|
82
85
|
|
86
|
+
setContentView( R.layout.activity_main );
|
83
|
-
|
87
|
+
btn1 = (Button) findViewById( R.id.button1 );
|
84
|
-
|
88
|
+
btn1.setOnClickListener( this );
|
85
89
|
|
86
|
-
|
90
|
+
btn2 = (Button) findViewById( R.id.rebtn );
|
91
|
+
btn2.setOnClickListener( this );
|
87
92
|
|
88
|
-
|
93
|
+
mySurfaceView = (SurfaceView) findViewById( R.id.surface_view );
|
89
94
|
|
90
|
-
holder.addCallback( callback );
|
91
|
-
Log.v("Holder","ok");
|
92
|
-
holder.setType( SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS );
|
93
|
-
|
95
|
+
holder = mySurfaceView.getHolder();
|
94
96
|
|
97
|
+
holder.addCallback( callback );
|
98
|
+
Log.v( "Holder", "ok" );
|
99
|
+
holder.setType( SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS );
|
100
|
+
Log.v( TAG, "Holder" + holder );
|
95
101
|
|
102
|
+
|
96
|
-
|
103
|
+
File dirs = new File( "storage" );
|
97
|
-
|
104
|
+
if (!dirs.exists()) {
|
98
|
-
|
105
|
+
dirs.mkdir();
|
106
|
+
}
|
99
107
|
}
|
100
108
|
|
101
109
|
}
|
102
110
|
|
103
|
-
private void checkPermission() {
|
104
|
-
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED){
|
105
|
-
requestCamPermission();
|
106
|
-
}
|
107
111
|
|
108
|
-
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)!= PackageManager.PERMISSION_GRANTED){
|
109
|
-
|
112
|
+
private boolean checkPermission() {
|
110
|
-
|
113
|
+
boolean camP;
|
111
114
|
|
112
|
-
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
|
113
|
-
|
115
|
+
boolean recP;
|
114
|
-
}
|
115
116
|
|
116
|
-
|
117
|
+
boolean strP;
|
117
118
|
|
118
119
|
|
119
|
-
private void requestCamPermission() {
|
120
|
-
if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.CAMERA)){
|
121
|
-
|
120
|
+
if (ActivityCompat.checkSelfPermission( this, Manifest.permission.CAMERA ) != PackageManager.PERMISSION_GRANTED) {
|
121
|
+
camP=false;
|
122
122
|
}else {
|
123
|
-
|
123
|
+
camP=true;
|
124
124
|
}
|
125
125
|
|
126
|
+
if (ActivityCompat.checkSelfPermission( this, Manifest.permission.RECORD_AUDIO ) != PackageManager.PERMISSION_GRANTED) {
|
127
|
+
recP=false;
|
128
|
+
}else {
|
129
|
+
recP=true;
|
126
|
-
|
130
|
+
}
|
131
|
+
if (ActivityCompat.checkSelfPermission( this, Manifest.permission.WRITE_EXTERNAL_STORAGE ) != PackageManager.PERMISSION_GRANTED) {
|
127
132
|
|
128
|
-
|
133
|
+
strP=false;
|
129
|
-
if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.RECORD_AUDIO)){
|
130
|
-
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.RECORD_AUDIO},REQUEST_PERMISSION);
|
131
134
|
}else {
|
132
|
-
|
135
|
+
strP=true;
|
133
136
|
}
|
137
|
+
|
138
|
+
requestPermissions(camP,recP,strP);
|
139
|
+
Log.v( "request","permission" );
|
140
|
+
return camP && recP && strP;
|
134
141
|
}
|
135
142
|
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_PERMISSION);
|
143
|
+
|
144
|
+
private void requestPermissions(boolean camP, boolean recP, boolean strP) {
|
145
|
+
|
146
|
+
if (ActivityCompat.shouldShowRequestPermissionRationale( this, Manifest.permission.CAMERA )) {
|
147
|
+
ActivityCompat.requestPermissions( MainActivity.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION );
|
148
|
+
} else {
|
149
|
+
ActivityCompat.requestPermissions( this, new String[]{Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION );
|
141
150
|
}
|
142
151
|
|
152
|
+
|
143
153
|
}
|
144
154
|
|
155
|
+
|
156
|
+
|
157
|
+
|
145
158
|
@Override
|
146
159
|
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
147
160
|
if (requestCode == REQUEST_PERMISSION) {
|
148
161
|
// 使用が許可された
|
149
162
|
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
150
|
-
Camera.open();
|
151
163
|
return;
|
152
164
|
|
153
|
-
} else {
|
154
|
-
// それでも拒否された時の対応
|
155
|
-
Toast toast = Toast.makeText(this, "これ以上なにもできません", Toast.LENGTH_SHORT);
|
156
|
-
toast.show();
|
157
165
|
}
|
158
166
|
}
|
159
167
|
}
|
1
エラーログを追記しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -17,6 +17,38 @@
|
|
17
17
|
現在実機にアプリをインストールして起動すると、パーミッション許可を求めるダイアログが表示される前にアプリがRuntimeExceptionにより終了してしまっています。アプリが終了しました〜という旨のメッセージの後ろに許可を求めるダイアログ表示されており、アプリが終了しました〜のメッセージをOKを選択してアプリを完全に停止した後にパーミッション許可を押す形になってしまっています。しかも、このダイアログボックスが一度にすべて表示されません(参考にしたサイトでは1/3のように一度のダイアログ表示でよいようになっていた)なので、アプリ起動→終了→ダイアログ表示の動作を3回繰り返さなければならない状態にあります。(全てのパーミッションを許可すればアプリの起動に問題はありません)
|
18
18
|
どうかどのようにコーディングしていけば良いかご教授お願いします。
|
19
19
|
|
20
|
+
以下エラーログです。
|
21
|
+
```ここに言語を入力
|
22
|
+
08-24 11:13:46.255 19824-19979/パッケージ名 I/OpenGLRenderer: Initialized EGL, version 1.4
|
23
|
+
08-24 11:13:46.272 19824-19824/パッケージ名 W/CameraBase: An error occurred while connecting to camera: 0
|
24
|
+
08-24 11:13:46.275 19824-19824/パッケージ名 D/AndroidRuntime: Shutting down VM
|
25
|
+
08-24 11:13:46.275 19824-19824/パッケージ名 E/AndroidRuntime: FATAL EXCEPTION: main
|
26
|
+
Process: パッケージ名, PID: 19824
|
27
|
+
java.lang.RuntimeException: Fail to connect to camera service
|
28
|
+
at android.hardware.Camera.<init>(Camera.java:702)
|
29
|
+
at android.hardware.Camera.open(Camera.java:489)
|
30
|
+
at パッケージ名.MainActivity$1.surfaceCreated(MainActivity.java:166)
|
31
|
+
at android.view.SurfaceView.updateWindow(SurfaceView.java:597)
|
32
|
+
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:179)
|
33
|
+
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
|
34
|
+
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2073)
|
35
|
+
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1125)
|
36
|
+
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6058)
|
37
|
+
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
|
38
|
+
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
|
39
|
+
at android.view.Choreographer.doFrame(Choreographer.java:606)
|
40
|
+
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
|
41
|
+
at android.os.Handler.handleCallback(Handler.java:739)
|
42
|
+
at android.os.Handler.dispatchMessage(Handler.java:95)
|
43
|
+
at android.os.Looper.loop(Looper.java:148)
|
44
|
+
at android.app.ActivityThread.main(ActivityThread.java:5554)
|
45
|
+
at java.lang.reflect.Method.invoke(Native Method)
|
46
|
+
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746)
|
47
|
+
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
|
48
|
+
|
49
|
+
```
|
50
|
+
|
51
|
+
|
20
52
|
###該当のソースコード
|
21
53
|
```ここに言語を入力
|
22
54
|
public class MainActivity extends Activity implements View.OnClickListener {
|