I cannot return the previous activity with using ActivityForResult

独自空忆成欢 提交于 2020-01-03 04:32:26

问题


I have these codes. Camera opened, the picture is taken, but when I click on "ok" button, nothing happened. The only way to get back to previous activity is clicking the "x" button which is not useful for me :). What is the problem? (onActivityResult method is not finished yet.) (I used to use this algorithm with Android.provider.MediaStore.ACTION_IMAGE_CAPTURE intent. there was everything OK. I have no idea why I have problem now.)

public void onClick(View v) {

            Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE");
            db.open();
            Cursor cr = db.getAllRecords();
            int count = cr.getCount();

            db.close();

            File cameraFolder;

            if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
                cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),"TSFC");
            else
                cameraFolder= ShowMessagesPage.this.getCacheDir();
            if(!cameraFolder.exists())
                cameraFolder.mkdirs();

            File photo = new File(Environment.getExternalStorageDirectory(), "TSFC/" + (count + 1) + ".jpg");
            getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));

            initialURI = Uri.fromFile(photo);

            startActivityForResult(getCameraImage, CAMERA_RESULT);
        }});}

protected void onActivityResult(int requestCode, int resultCode,
          Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);

        if (resultCode == RESULT_OK) {
            Bundle extras = intent.getExtras();
            photo = (Uri) extras.get("data");


        }
      }

回答1:


Ok, Its well known Bug,

Just Add this code line, photo.createNewFile();

Something like,

File photo = new File(Environment.getExternalStorageDirectory(), "TSFC/" + (count + 1) + ".jpg");
photo.createNewFile();
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));

Also don't forget to,

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

And let me know your progress.




回答2:


Use onActivityResult(..) Method to get back Result Like this Way.

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

    if (requestCode == CAMERA_RESULT) {
        // Make sure the request was successful

            if (resultCode == RESULT_OK) {
            Bundle extras = intent.getExtras();
            photo = (Uri) extras.get("data");

        }
    }
}


来源:https://stackoverflow.com/questions/14295906/i-cannot-return-the-previous-activity-with-using-activityforresult

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!