代码之家  ›  专栏  ›  技术社区  ›  Pushan Gupta

当子活动调用另一个活动时,StartActivityForResults不工作

  •  0
  • Pushan Gupta  · 技术社区  · 8 年前

    我有3个活动,比如A、B和C。

    A呼叫B。

    当b不调用c时,它返回到a。但是当b调用c时,它不返回到a,应用程序停止。

    现在真正的问题是,从活动A中,我想调用图像选择器并裁剪图像。这就是活动B,它收割并调用C来拾取图像。

    活动A:

        iv_profile_pic.setOnClickListener(new  View.OnClickListener() {//iv_profile_pic is an ImageView
            @Override
            public void onClick(View view) {
                Intent i=new Intent(MainActivity.this,profile_pic_chooser.class);
                i.setFlags(0);
                MainActivity.this.startActivityForResult(i, 999);
    
                Toast.makeText(getApplicationContext(),"Reached",Toast.LENGTH_SHORT).show();
    
            }
        });
    

    @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data)
        {
            if (requestCode == 999) {
                Bitmap image=data.getParcelableExtra("picture");
    
                iv_profile_pic.setImageBitmap(image);
    
    
            }
        }
    

    活动B:

    它有两个按钮。装载和收割。单击时加载调用ImageChooseIntent并选择在B中打开的要裁剪的guidLine图像。

    单击“裁剪”后,应返回到裁剪的图像。

    当然,如果在不调用LOAD的情况下调用CROP,它将返回到WITH NULL。 但是,如果先单击加载,然后调用裁剪,应用程序就会停止。

     public void onLoadImageClick(View view) {
    
            startActivityForResult(getPickImageChooserIntent(), 200);
     }
    

    public void onCropImageClick(View view) {
            Bitmap cropped =  mCropImageView.getCroppedImage(500, 500);
            if (cropped != null) {
                mCropImageView.setImageBitmap(cropped);
                iv.setImageBitmap(cropped);
    
                Intent returnIntent = new Intent();
                returnIntent.putExtra("picture", cropped);
                setResult(Activity.RESULT_OK, returnIntent);
                finish();
            }
        }
    

    @Override
        protected void onActivityResult(int  requestCode, int resultCode, Intent data) {
            if (resultCode == Activity.RESULT_OK) {
                Uri imageUri =  getPickImageResultUri(data);
    
                // For API >= 23 we need to check specifically that we have permissions to read external storage,
                // but we don't know if we need to for the URI so the simplest is to try open the stream and see if we get error.
                boolean requirePermissions = false;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
                        checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED &&
                        isUriRequiresPermissions(imageUri)) {
    
                    // request permissions and handle the result in onRequestPermissionsResult()
                    requirePermissions = true;
                    mCropImageUri = imageUri;
                    requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
                }
    
                if (!requirePermissions) {
                    mCropImageView.setImageUriAsync(imageUri);
                }
            }
        }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Pushan Gupta    8 年前

    我有个解决办法。我面临的最可能的问题是:

    我正在使用一个外部库来裁剪图像。这个图书馆做了两件事。

    首先,使用ImageChooser意图选择图像。

    第二,剪掉那个图像。

    库剪切图像后,它没有将剪切图像保存到本地/外部存储中。但我想把它传回父目录。

    所以我的解决方法是,

    将位图保存在存储中

    将URI传递给父级

    从子级提取该URI

    从该URI生成位图

    _窋应用于图像视图

    所以活动B有:

    public void onCropImageClick(View view) {
            Bitmap cropped =  mCropImageView.getCroppedImage(500, 500);
            if (cropped != null) {
                mCropImageView.setImageBitmap(cropped);
                iv.setImageBitmap(cropped);
                File externalStorageDirectory = Environment.getExternalStorageDirectory();
                externalStorageDirectory= new File(externalStorageDirectory , "FOLDERNAME");
    
                if(!createDirIfNotExists(externalStorageDirectory)){
                    Toast.makeText(this,"Failed creating Directory!",Toast.LENGTH_SHORT).show();
                }else{
    
                    File filename=new File(externalStorageDirectory, String.valueOf(Calendar.getInstance().getTimeInMillis())+".PNG");
                    FileOutputStream out = null;
                    try {
                        out = new FileOutputStream(filename);
                        cropped.compress(Bitmap.CompressFormat.PNG, 100, out); // cropped is your Bitmap instance
                        // PNG is a lossless format, the compression factor (100) is ignored
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            if (out != null) {
                                out.close();
                                Intent returnIntent = new Intent();
                                returnIntent.putExtra("picture", Uri.fromFile(filename));
                                setResult(RESULT_OK, returnIntent);
                                finish();
    
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
    
                }
    
            }
            //Toast.makeText(this,mCropImageUri.toString(),Toast.LENGTH_SHORT).show();
    
    
        }
    

    活动A:

    @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data)
        {
    
            if (requestCode == 999 && resultCode==RESULT_OK) {
    
               Uri path=data.getParcelableExtra("picture");
                Bitmap bitmap=null;
                try {
                    bitmap= MediaStore.Images.Media.getBitmap(this.getContentResolver(), path);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Toast.makeText(this,path.toString(),Toast.LENGTH_SHORT).show();
    
                if (bitmap!=null){
    
                    iv_profile_pic.setImageBitmap(bitmap);
    
                }
    }
    

    也许我的问题陈述是错误的,但解决方法是有效的。任何编辑/建议

    100%欢迎。万一像我这样的人被卡住了,这可能会有所帮助!