startActivityForResult와 onActivityResult를 각각 호출하는 두 개의 버튼이 있습니다. 첫 번째는 연락처 선택 도구이고 두 번째는 그림 선택 도구입니다. 첫 번째는 제대로 작동하고 필요한 연락처 번호를 내 eddittext에 반환합니다. 두 번째는 시작해야하며 내 갤러리에서 사진을 선택할 수는 있지만 사진을 내 imagview로 돌려 보내지는 않습니다. 나는 그것의 첫번째 것에서 자료를 돌려 보내려고 노력하고 두 번째와 첫 번째를 구별하는 방법을 이해할 수 없다고 생각한다. 나는 안드로이드 입문, 아직 배우기, 실수하기, 그리고 내가 어디서 어떻게 잘못되었는지에 대한 도움을 주시면 감사하겠습니다. 문제의 코드는 다음과 같습니다.2 startActivityForResult 및 2 onActivityResult, 두 번째 하나가 올바르게 시작하지만 첫 번째 것들 데이터를 반환하려고?
Button.OnClickListener buttonClickListener3 = new Button.OnClickListener() {
public void onClick(View contact) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
}};
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor cur = managedQuery(contactData, null, null, null, null);
ContentResolver contect_resolver = getContentResolver();
if (cur.moveToFirst()) {
String id = cur.getString(cur.getColumnIndexOrThrow(BaseColumns._ID));
String name = "";
String no = "";
Cursor phoneCur = contect_resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
if (phoneCur.moveToFirst()) {
name = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
no = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
Log.e("Phone no & name :***: ", name + " : " + no);
txt.append(name + " : " + no + "/n");
id = null;
name = null;
no = null;
phoneCur = null;
}
contect_resolver = null;
cur = null;
}
}
Button.OnClickListener buttonClickListener4 = new Button.OnClickListener() {
public void onClick(View ChoosePictureButton) {
Intent choosePictureIntent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(choosePictureIntent, 2);
}};
public void onActivityResult2(int requestCode2, int resultCode2, Intent intent) {
super.onActivityResult(requestCode2, resultCode2, intent);
if (resultCode2 == RESULT_OK) {
Uri imageFileUri = intent.getData();
try {
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpFactoryOptions);
bmpFactoryOptions.inSampleSize = 2;
bmpFactoryOptions.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
imageFileUri), null, bmpFactoryOptions);
ChosenImageView.setImageBitmap(bmp);
} catch (FileNotFoundException e) {
Log.v("ERROR", e.toString());
}
}
}
Hmm could not는 switch 문을 제대로 작동시키지 못했다.하지만 (reqCode == 1) {do stuff}와 if (reqCode == 2) {do stuff}를 시도했지만 여전히 그것을 얻을 수는 없다. 선택한 사진을 반환합니다. – user1409172
예 if 문도 작동합니다. –
왜 갤러리에서 사진을 반환하지 않는 이유는 무엇입니까? – user1409172