onActivityResult方法未被调用

时间:2015-06-23 03:03:01

标签: android

相机应用程序打开,捕获图像后,相机应用程序不会关闭。此外,我已创建默认目录以保存图像文件,但默认情况下会保存到相机文件夹中。

 private void takePicture(){
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (Util.SystemFeatures.hasCamera(getApplication())) {

        if (imageFile != null) {
            AppLog.showAppFlow("imagefile is not null");
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
            startActivityForResult(intent,CAMERA_ACTIVITY_REQUEST_CODE);
            Toast.makeText(this,"opening camera",Toast.LENGTH_LONG).show();
        } else {
            AppLog.showAppFlow("image file is null");
        }

    }

}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if(requestCode == CAMERA_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK){
        AppLog.showAppFlow("Image successfully captured");
        Toast.makeText(getApplicationContext(),"Image saved in: "+imageFile.getAbsolutePath(),Toast.LENGTH_LONG).show();
        startNextActivity();
    }
}

private void startNextActivity(){

    Intent i = new Intent(this,confirmImage.class);
    startActivity(i);

}

2 个答案:

答案 0 :(得分:-1)

要获得调用onActivityResult,您需要启动活动内容您的onActivityResult代码为结果

使用此

private void startNextActivity(){

    Intent i = new Intent(this,confirmImage.class);
    startActivityForResult(i);

}

答案 1 :(得分:-1)

请尝试此代码。 这将在SD卡中创建一个文件夹 MyCapturedImages 并将图像存储在其中。

public static final int REQUEST_IMAGE_CAPTURE = 1;
private Bitmap userImageBit;


//on click
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    if (takePictureIntent.resolveActivity(getPackageManager()) != null) 
                    {

                        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
                    }

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {


        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) 
        {
            Bundle extras = data.getExtras();
            userImageBit = (Bitmap) extras.get("data");

//you can use this bitmap userImageBit 

String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/MyCapturedImages");    
        if(myDir.isDirectory())
        {
            myDir.mkdirs();
        }
        myDir.mkdirs();
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = "MyImage-"+ n +".png";
        File file = new File (myDir, fname);
        if (file.exists ()) file.delete (); 
        try 
        {
            FileOutputStream out = new FileOutputStream(file);
            userImageBit .compress(Bitmap.CompressFormat.PNG, 80, out);
            out.flush();
            out.close();
            System.out.println("on activity of Activity");


            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        }
}