액티비티에서 (이미지 클릭 ->intent로 기기 내의 이미지 선택 -> onActivityResult로 해당 이미지를 액티비티내에 이미지 뷰에 적용) 다음 단계들을 통해 이미지가 나타나도록 했습니다.
그런데 이 액티비티를 프래그먼트로 옮기게 되자 문제가 발생했습니다. 찾아보니 프래그먼트가 들어있는 액티비티로 result가 반환되기 때문이라고도 하는데 이를 해결하는 방법을 모르겠네요 ㅠㅠ
1) 액티비티
///////////////////
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
startActivityForResult(intent, REQUEST_CODE);
}
---> 이미지를 클릭해 액티비티로 이동합니다.
이런
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
if(uri!=null){
Glide.with(getApplicationContext()).load(uri).into(useriamge_view);
imgpath = getRealPathFromURI(uri);
new AlertDialog.Builder(this).setMessage(uri.toString()+"\n"+imgpath).create().show();
//Upload_img(imgpath);
}
}
} else if (resultCode == RESULT_CANCELED) {
}
}
private String getRealPathFromURI(Uri contentURI) {
String thePath = "no-path-found";
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(contentURI, filePathColumn, null, null, null);
if(cursor.moveToFirst()){
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
thePath = cursor.getString(columnIndex);
}
cursor.close();
return thePath;
}
--->onActivityResult에 해당하는 코드로 액티비티에서는 정상적으로 작동했습니다.
이를 프래그먼트로 그대로 옮기니 빨간 글자 부분은 에러가 나네요
어떻게 하면 프래그먼트에서 실행한 값을 액티비티가 아니라 프래그먼트로 다시 넘겨 받을 수 있을까요?