简体   繁体   English

为什么图片可以返回到上一个活动但不打算返回新活动

[英]why Image can return to previous activity but not intent to new activity

Why image can be pass to previous activity but not intent to new activity? 为什么图像可以传递给先前的活动,但不能传递给新的活动?

ImageFitScreen.java ImageFitScreen.java

    b = (ImageView) findViewById(R.id.imageView3);
      public void selectImage() {

            final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };
            AlertDialog.Builder builder = new AlertDialog.Builder(ImageFitScreen.this);
            builder.setTitle("Add Photo!");
            builder.setItems(options, new DialogInterface.OnClickListener() {

                @Override

                public void onClick(DialogInterface dialog, int item) {

                    if (options[item].equals("Take Photo"))

                    {

                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                        File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");


                        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                        //pic = f;
                      //  Toast.makeText(getApplicationContext(), Uri.fromFile(f) +"", Toast.LENGTH_LONG).show();
                        startActivityForResult(intent, 1);


                    } else if (options[item].equals("Choose from Gallery"))

                    {

                        Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                        startActivityForResult(intent, 2);


                    } else if (options[item].equals("Cancel")) {

                        dialog.dismiss();
                        finish();

                    }

                }

            });
       @Override

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

            super.onActivityResult(requestCode, resultCode, data);

            if (resultCode == RESULT_OK) {
                if (requestCode == 1) {
                    //h=0;
                    File f = new File(Environment.getExternalStorageDirectory().toString());
                    for (File temp : f.listFiles()) {
                        if (temp.getName().equals("temp.jpg")) {
                            f = temp;
                            File photo = new File(Environment.getExternalStorageDirectory(), "temp.jpg");
                            //pic = photo;
                            break;
                        }
                    }

                    try {
                        Bitmap bitmap;
                        BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
                        bitmapOptions.inJustDecodeBounds = false;
                        bitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
                        bitmapOptions.inDither = true;
                        bitmapOptions.inSampleSize=8;
                        bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions);
                        Global.img = bitmap;

                        b.setImageBitmap(bitmap);
                        String path = android.os.Environment.getExternalStorageDirectory() + File.separator + "Phoenix" + File.separator + "default";
                        //p = path;
                        f.delete();
                        OutputStream outFile = null;
                        File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
                        try {

                            outFile = new FileOutputStream(file);
                            bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
                            //pic=file;
                            outFile.flush();
                            outFile.close();


                        } catch (FileNotFoundException e) {
                            e.printStackTrace();

                        } catch (IOException e) {
                            e.printStackTrace();

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    } catch (Exception e) {
                        e.printStackTrace();

                    }

                } else if (requestCode == 2) {

                    Uri selectedImage = data.getData();
                    // h=1;
                    //imgui = selectedImage;
                    String[] filePath = {MediaStore.Images.Media.DATA};
                    Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
                    c.moveToFirst();
                    int columnIndex = c.getColumnIndex(filePath[0]);
                    String picturePath = c.getString(columnIndex);
                    c.close();
                    Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
                    Log.w("path of image ******", picturePath + "");
                    b.setImageBitmap(thumbnail);
                }


            }
            else
            {
                finish();
            }


        }

 ok.setOnClickListener(new View.OnClickListener()  //back to Project1.java
        {
            public void onClick(View arg0)
            {
                Intent returnIntent=new Intent();
                text=t.getText().toString();
                b.setDrawingCacheEnabled(true);
                b.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                b.layout(0, 0, b.getMeasuredWidth(), b.getMeasuredHeight());
                b.buildDrawingCache(true);
                returnIntent.putExtra("text", text);
                if (b.getDrawingCache() != null) {
                    Bitmap bitmap = Bitmap.createBitmap(b.getDrawingCache());
                    if (bitmap == null) {
                        Log.e("TAG", "getDrawingCache() == null");
                    }
                    Global.img = bitmap;
                }
                setResult(Activity.RESULT_OK, returnIntent);
                finish();
            }
        });

Project1.java Project1.java

    public void onActivityResult(int requestCode,int resultCode, Intent data)
        {
            if(requestCode==PROJECT_REQUEST_CODE) {
                if(data!=null&&data.hasExtra("text")) {
                    c = data.getStringExtra("text");
                    txt1.setText(c);
                    viewImage.setImageBitmap(Global.img); //image can be returned
                }


            }
            else if (requestCode==CAMERA_REQUEST_CODE)
            {

            }
        }

  b.setOnClickListener(new View.OnClickListener() {  // back to Claims.java
            public void onClick(View arg0) {
                Intent returnIntent = new Intent();
                a = "Project";
                text = txt.getText().toString(); // amount
                returnIntent.putExtra("text", text);
                returnIntent.putExtra("a", a);
                final int k1 = getIntent().getExtras().getInt("k");
                returnIntent.putExtra("k1", k1);
                returnIntent.putExtra("c",c);
                setResult(Activity.RESULT_OK, returnIntent);
                finish();
            }
        });

Claims.java Claims.java

      @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
                   switch (requestCode) {
                    case 0:
                        result = data.getStringExtra("text");
                        name = data.getStringExtra("a");
                        description=data.getStringExtra("c");
                        if (Global.img != null) {
                            v.setImageBitmap(Global.img); //image can shown here
                        }

                        c.setText("            " + name + "------" + "RM " + result);
                        break;
}
}

 c.setOnClickListener(new View.OnClickListener() { //pass image to EditClaims.java
                                 @Override
                                 public void onClick(View v) {
                                     if ((name != null && name.trim().length() > 0) && (result != null && result.trim().length() > 0)) {
                                         Toast.makeText(getActivity().getApplicationContext(), "not null", Toast.LENGTH_LONG).show();
                                         Global.img=null;
                                         Intent intent = new Intent(getActivity(), EditClaims.class);
                                         v.setDrawingCacheEnabled(true);
                                         v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                                                 View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                                         v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
                                         v.buildDrawingCache(true);
                                         intent.putExtra("name", name);
                                         intent.putExtra("result", result);
                                         intent.putExtra("description", description);
                                         if (v.getDrawingCache() != null) {
                                             Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());
                                             if (bitmap == null) {
                                                 Log.e("TAG", "getDrawingCache() == null");
                                             }
                                             Global.img = bitmap;
                                             startActivity(intent);
                                         }

                                         }
                                     else {
                                         Toast.makeText(getActivity().getApplicationContext(), "null", Toast.LENGTH_LONG).show();
                                     }
                                 }
                                     } );

EditClaims.java EditClaims.java

  viewImage.setImageBitmap(Global.img);

Global.java Global.java

public class Global {

    static Bitmap img;
}

But what I get in viewImage is (" " + name + "------" + "RM " + result) which is get from the c.setText . 但是我在viewImage中得到的是(" " + name + "------" + "RM " + result) ,它是从c.setText获得的。 Can someone help me to figure out the problem? 有人可以帮我解决问题吗?

Activity Flow: ImageFitScreen ----->Back to project1 ----->Back to Claims -----> Claims intent to EditClaims 活动流程: ImageFitScreen ----->返回project1 ----->返回Claims -----> ClaimsEditClaims意图

Screen shot of Claims 索赔屏幕截图

Note that the image beside Amount is return from Project1 . 请注意,金额旁边的图像是从Project1返回的。 Since c.setText is not null, it can intent to EditClaims.java . 由于c.setText不为null,因此可以使用EditClaims.java

在此处输入图片说明

It works like charm, but only for captured image . 它像魅力一样工作,但仅适用于captured image

在此处输入图片说明

Now I try with the image from gallery 现在,我尝试使用image from galleryimage from gallery

在此处输入图片说明

When I click c, it shows me this. 当我单击c时,它显示了这一点。 It didn't intent to EditClaims.java 它不是要EditClaims.java

在此处输入图片说明

The problem is with the OnClickListener you're setting on c . 问题出在您在c设置的OnClickListener上。 That listener has a method public void onClick(View v) , in which you're confusing your v variables. 该侦听器有一个方法public void onClick(View v) ,您在其中混淆了v变量。

Judging from the onActivityResult() method, you have a class member ImageView named v . onActivityResult()方法来看,您有一个名为v的类成员ImageView But, within the onClick() method, an unqualified v is going to be the View v passed into the method, not the class member v . 但是,在onClick()方法中,不合格的v将是传递给该方法的View v ,而不是类成员v The View v passed into the onClick() method of an OnClickListener is the View that is being clicked, so this explains why you're seeing the content of c in the next Activity . 传递给OnClickListeneronClick()方法的View v是被单击的View ,因此这解释了为什么您在下一个Activity看到c的内容。

The simplest solution would be to change the parameter variable of the onClick() method to a different name. 最简单的解决方案是将onClick()方法的参数变量更改为其他名称。 For example: 例如:

public void onClick(View view)

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

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