片段在Camera Intent后关闭

时间:2014-10-15 23:55:57

标签: android android-asynctask fragment android-camera

嗨我有一个活动和2个片段A和B.当app开始片段A当我转到片段B并按下相机显示的按钮并且在拍摄照片并保存而不是再次显示片段B时显示回到片段A.我不知道为什么! 这是我的片段B. 这就是我开始片段B的方式:

 Fragment f = UploadDataFragment.newInstance(rawResult.getText());
    if (f != null) {
        getActivity().getSupportFragmentManager().beginTransaction()
                .replace(R.id.frame_container, f).addToBackStack(null).commit();
    }

我的相机按钮:

     picButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            String file = "img.jpg";
            File newfile =  new File(Environment.getExternalStorageDirectory(), file);
            Uri outputFileUri = Uri.fromFile(newfile);
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            cameraIntent.putExtra("return-data", true);
            startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
        }
    });

这是我的activityResult:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
   super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == TAKE_PHOTO_CODE && resultCode== Activity.RESULT_OK) {
        Bitmap bmp = BitmapFactory.decodeFile("/sdcard/img.jpg");
        img.setImageBitmap(bmp);
        //POST Image to Server
        mUploadPhotoRequest.execute();
    }
   }

我不知道它是否重要但这是我的AsyncTask类:

/**
 * Async task class to upload photo by making HTTP call
 * */
private class UploadPhotoRequest extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Uploading Video...");
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();
        // Making a request to url and getting response
        User user = new User(getActivity().getApplicationContext());
        ArrayList<NameValuePair> list=new ArrayList<NameValuePair>();
        list.add(new     BasicNameValuePair("file",getRealPathFromURI(getActivity().getApplicationContext(),videoUri)));
        String jsonStr = sh.makeServiceCall(Constants.addPhotoUrl + "api_key=" + user.getApiKey()+"&barcode=" + barcode, ServiceHandler.POST,list);
        Log.e("Tag","VIDEO RESPONSE "+ jsonStr);
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (pDialog.isShowing())
            pDialog.dismiss();
    }

}

0 个答案:

没有答案