简体   繁体   English

片段中的startActivityForResult

[英]startActivityForResult in fragment

I have two problem: 我有两个问题:

First: i wonder know the diffirent in 2 method cuz when i use method 1 my app run ok, but i change method 2 it cant run.. Method 1(Take photo simple) : 首先:我想知道2方法cuz中的diffirent当我使用方法1我的应用程序运行正常,但我改变方法2它不能运行..方法1(照片简单):

    private void takePhoto() {

     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    Log.d(TAG, "Take photo .......");

    startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}

And Method 2: 方法2:

  public void dispathTakepictureIntent() {

  Context context = getActivity();

    PackageManager packageManager = context.getPackageManager();

    if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)                    ==false)
                 {
        Toast.makeText(getActivity()
       , "This device does not have a camera.", Toast.LENGTH_SHORT).show();
        return;
    }
       else 
        {
        Intent takePictureIntent = new                   Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) 
               {  
            File photoFile = null;
            try {
                photoFile = createCurrentPhotoPath();

                } 
                catch (IOException ex) 
                   {

                Toast.makeText(getActivity(), "null photo Path", Toast.LENGTH_LONG).show();
            }

            if (photoFile != null) {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(photoFile));

                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);

            }
        }
    }

}

---onActivityResult of camera fragment: ---相机片段的onActivityResult:

 @Override

    public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_TAKE_PHOTO && resultCode == Activity.RESULT_OK) {

            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            if (imageBitmap != null)
                Img_show.setImageBitmap(imageBitmap);
            else
                Toast.makeText(getActivity(), "null photo bitmap", Toast.LENGTH_LONG).show();

        } else
            Toast.makeText(getActivity(), "Cancel !", Toast.LENGTH_LONG).show();


    }

Main problem :Now i have MainFragment extend FragmentActivity ( have fragment camera and map) . 主要问题:现在我有MainFragment扩展FragmentActivity(有片段相机和地图)。 but in fragment camera when i 但在片段相机的时候我

startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);

then in method onActivityResult,the resultCode alway ==0 although it must ==1; 然后在方法onActivityResult中,resultCode总是== 0虽然它必须== 1; . who can show my issue ??? 谁能证明我的问题??? where is my wrong?? 我哪里错了?

Note in mainFragment i just call : 请注意在mainFragment中我只是调用:

@Override

    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

    }

thanks 谢谢

Try like this... 试试这样......

     private static final int REQUEST_CAMERA = 2;

    if (isDeviceSupportCamera()) {
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        startActivityForResult(intent, REQUEST_CAMERA);
                    } else {
                        showCustomToast(" No camera on this device !!");
                    }
     private boolean isDeviceSupportCamera() {
            if (getActivity().getApplicationContext().getPackageManager().hasSystemFeature(
                    PackageManager.FEATURE_CAMERA)) {
                // this device has a camera
                return true;
            } else {
                // no camera on this device
                return false;
            }
        }
     @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == REQUEST_CAMERA && resultCode == getActivity().RESULT_OK && null != data) {
                Bitmap photo = (Bitmap) data.getExtras().get("data");
                profileImage.setImageBitmap(photo);

                // CALL THIS METHOD TO GET THE URI FROM THE BITMAP
                Uri tempUri = getImageUri(getActivity().getApplicationContext(), photo);

                // CALL THIS METHOD TO GET THE ACTUAL PATH
                File finalFile = new File(getRealPathFromURI(tempUri));
                decodeFile(finalFile.toString());
            }
        }
 public Uri getImageUri(Context inContext, Bitmap inImage) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
        return Uri.parse(path);
    }

    public String getRealPathFromURI(Uri uri) {
        Cursor cursor = getActivity().getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        return cursor.getString(idx);
    }


    // decode image
    public void decodeFile(String filePath) {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, o);
        // The new size we want to scale to
        final int REQUIRED_SIZE = 1024;
        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        bitmap = BitmapFactory.decodeFile(filePath, o2);       
        profileImage.setImageBitmap(bitmap);
    }

It just a fully tested complete example for you which can surely help you to take picture from camera....and works like a charm..... 它只是一个经过充分测试的完整示例,可以帮助您从相机拍照....并且像一个魅力......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM