public class MainActivity extends AppCompatActivity {
private Button btnStart;
/** ギャラリー表示用リクエストコード */
private static final int GALLARY_INTENT_CALLED = 302;
/** アクティビティコール用のリクエストコード */
private static final int ACTIVITY_CALLED = 201;
private final ActivityResultLauncher<Intent> activityResultLauncher
= registerForActivityResult(
new MyResultContract(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
Intent intent=result.getData();
int code=intent.getIntExtra("request_code",0);
// --------------------------
// code(呼び出し先判別のコード)により処理を分岐する
// ---------------------------
switch (code){
case GALLARY_INTENT_CALLED:
// 処理 ①
break;
case ACTIVITY_CALLED:
// 処理 ②
break;
default:
break;
}
Toast.makeText(MainActivity.this, "onActivityResult requestCode = " +code, Toast.LENGTH_SHORT).show();
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart=findViewById(R.id.startBtn);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Intent.ACTION_OPEN_DOCUMENT);
//端末標準のギャラリーから取得
intent.addCategory(Intent.CATEGORY_OPENABLE);
// マイムタイプ
intent.setType("image/jpeg");
// ギャラリー起動
Intent chooserIntent=Intent.createChooser(intent,getString(MainActivity.this,R.string.gallery_select)); // ギャラリーのタイトルが反映あされていませんが、原因は、後で調査します。
chooserIntent.putExtra("recCode",GALLARY_INTENT_CALLED);
activityResultLauncher.launch(chooserIntent);
}
});
}
public String getString(Context context, int resId) {
return context.getResources().getString(resId);
}
/**
* code(呼び出し元判別コード)を保持し、呼び出し元へ返却するクラス
*/
class MyResultContract extends ActivityResultContract<Intent,ActivityResult> {
private int callRequestCode;
@NonNull
@Override
public Intent createIntent(@NonNull Context context, Intent input) {
Intent intent=input;
int code=intent.getIntExtra("request_code",0);
callRequestCode=code;
return intent;
}
@Override
public ActivityResult parseResult(int resultCode, @Nullable Intent intent) {// intentは選択した画像のパスが入っている。
if(resultCode==Activity.RESULT_OK){
// 呼び出し元のリクエストコード
Intent resIntent=intent;
resIntent.putExtra("request_code",callRequestCode);
ActivityResult activityResult=new ActivityResult(RESULT_OK,resIntent);
return activityResult;
}else{
return null;
}
}
}
}
strings.xml
----------------------------------------------
<resources>
<string name="app_name">StartActivityResultTest</string>
<string name="gallery_select">写真を選択</string>
</resources>
activity_main.xml
----------------------------------
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ActivityResultLauncher TEST"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.125" />
<Button
android:id="@+id/startBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Call Chooser"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499" />
</androidx.constraintlayout.widget.ConstraintLayout>
続きを隠す<<