您知道如何在一项活动中处理两个onActivityResult()
吗?
我需要使用相机并在一项活动中搜索我的照片。
public class MainActivity extends AppCompatActivity {
public static final int REQUEST_CAPTURE = 1;
Button button_Vyber_Fotku, button_Fotak;
ImageView imageView_VyberFotku, imageView_Fotak;
private static final int PICK_IMAGE = 100;
Uri imageUri_vybrana;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView_VyberFotku = (ImageView) findViewById(R.id.imageView_VyberFotku);
button_Vyber_Fotku = (Button) findViewById(R.id.button_Vyber_Fotku);
imageView_Fotak = (ImageView) findViewById(R.id.imageView_Fotak);
button_Fotak = (Button) findViewById(R.id.button_fotak);
if (!hasCamera())
{
button_Fotak.setEnabled(false);
}
}
public void Vyber_fotku_clicked(View v)
{
openGallery();
}
private void openGallery()
{
Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
imageUri_vybrana = data.getData();
imageView_VyberFotku.setImageURI(imageUri_vybrana);
}
}
public boolean hasCamera()
{
return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
}
public void PouzijFotakClicked(View v)
{
Intent vyfot = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(vyfot , REQUEST_CAPTURE);
}
@Override
protected void onActivityResult(int requestCode1, int resultCode1, Intent data1)
{ if (requestCode1 == REQUEST_CAPTURE && resultCode1 == RESULT_OK)
{
Bundle extras = data1.getExtras();
Bitmap photo = (Bitmap) extras.get("data1");
imageView_Fotak.setImageBitmap(photo);
}
}
}
答案 0 :(得分:2)
而不是onActivityResults
使用单一方法的两种不同方法,并根据其请求代码区分它们。
@Override
protected void onActivityResult(int requestCode1, int resultCode1, Intent data1){
if (requestCode1 == REQUEST_CAPTURE && resultCode1 == RESULT_OK){
Bundle extras = data1.getExtras();
Bitmap photo = (Bitmap) extras.get("data1");
imageView_Fotak.setImageBitmap(photo);
}
else if (resultCode1 == RESULT_OK && requestCode1 == PICK_IMAGE){
imageUri_vybrana = data1.getData();
imageView_VyberFotku.setImageURI(imageUri_vybrana);
}
}
注意:单个覆盖方法不能有两个声明。
答案 1 :(得分:0)
检查一下 它对我有用
private static final int CAMERA_ = 999;
private static final int GALLERY_ = 888;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// if the result is capturing Image
if (requestCode == CAMERA_) {
if (resultCode == RESULT_OK) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
UtilsClass.mBitmap = BitmapFactory.decodeFile(fileUri.getPath(),
options);
imageView.setImageBitmap(UtilsClass.mBitmap);
} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
}else if (requestCode == GALLERY_) {
if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
c.close();
UtilsClass.mBitmap = (BitmapFactory.decodeFile(picturePath));
imageView.setImageBitmap(UtilsClass.mBitmap);
} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
}
答案 2 :(得分:0)
// define two variable camera and pick_image of int type pass value of request code of desired out put in activity onResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
//first one to pick image
//do somthing
}else if(resultCode == RESULT_OK && requestCode == Camera){
//use to take image from camera response
}
}