片段中的 startActivityForResult

Posted

技术标签:

【中文标题】片段中的 startActivityForResult【英文标题】:startActivityForResult in fragment 【发布时间】:2016-09-04 21:07:21 【问题描述】:

我有两个问题:

首先:我想知道方法 2 的不同之处,因为当我使用方法 1 时,我的应用程序运行正常,但我更改方法 2 它无法运行.. 方法一(简单拍照):

    private void takePhoto() 

     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

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

    startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);

还有方法二:

  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:

 @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();


    

主要问题:现在我有 MainFragment 扩展 FragmentActivity(有片段相机和地图)。但是当我在片段相机中

startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);

然后在onActivityResult方法中,resultCode总是==0,虽然它必须==1; .谁可以显示我的问题???我哪里错了??

注意 mainFragment 我只是调用:

@Override

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

        super.onActivityResult(requestCode, resultCode, data);

    

谢谢

【问题讨论】:

【参考方案1】:

这样试试……

     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);
    

这只是一个经过全面测试的完整示例,它肯定可以帮助您从相机拍照......并且像魅力一样工作......

【讨论】:

它仍然被拒绝。因为当我调试时,我在 caemra 片段 ==0 上看到了 resultCode; (吐司=取消)。我认为我在 startActivityForResult 和 onActivityResult 的问题。 @AceAkatsuki..okay...现在告诉我您解决此问题的主要关注点,或者您可以使用其他一些运行效率更高的代码 @AceAkatsuki...我已经更新了我的答案,这肯定对你有帮助 你的代码很棒。如您所知,resultCode == getActivity().RESULT_OK 必须返回 =1 。但是当我运行调试 resultCode == getActivity().RESULT_OK =0 时,我拍照后我没有取消它,然后它无法保存,获取图像...等等..这是我的问题。我不知道为什么也不明白。 @AceAkatsuki 因为您在检查相机功能时在 if else 条件下做错了(相反)...

以上是关于片段中的 startActivityForResult的主要内容,如果未能解决你的问题,请参考以下文章

片段中的片段

操作栏标签片段中的片段?

片段对话框中的片段膨胀引发错误“片段未创建视图”

当前一个片段中的某些任务完成时如何通知另一个片段中的适配器

如何处理片段和活动中的后压

从另一个片段中的目录更新片段中的列表视图元素