小编典典

无法将图片传递给其他班级

java

我正在尝试将图像传递给另一个类intent,但 它仅适用于captured image,不适用于image selected from gallery

这是开始使用相机功能的地方。在 ImageFitScreen.java中 ,它具有一个ok用于返回到上一个活动的按钮。

ImageFitScreen.java

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

                }

            }

        });

        builder.setOnKeyListener(new Dialog.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode,
                                 KeyEvent event) {
                // TODO Auto-generated method stub
                if (keyCode == KeyEvent.KEYCODE_BACK){
                    dialog.dismiss();
               finish();
            }

            return true;
        }
        });
        builder.show();



    }



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

上一个活动

     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 displayed
                }


            }
            else if (requestCode==CAMERA_REQUEST_CODE)
            {

            }
        }

   b.setOnClickListener(new View.OnClickListener() {  // save button
            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();
            }
        });

    }

由于图像可以显示在中,因此可以返回到上一个活动 viewImage。在上一个活动中,它也有一个返回活动A的按钮。

活动A

   c = (TextView) claims.findViewById(R.id.textView49);
     c.setOnClickListener(new View.OnClickListener() {
                                     @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();
                                             Intent intent = new Intent(getActivity(), EditClaims.class);
                                             intent.putExtra("name", name);
                                             intent.putExtra("result", result);
                                             intent.putExtra("description", description);

                                        byte[]data=getBitmapAsByte(getActivity(), Global.img);
                                             intent.putExtra("data",data);
                                             startActivity(intent);

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

      public static byte[]getBitmapAsByte(final Context context,Bitmap bitmap) {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 50, outputStream);
            return outputStream.toByteArray();

        }

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        int button = data.getIntExtra("k1", 0);

        if (button == 1) {
            switch (requestCode) {
                case 0:
                    result = data.getStringExtra("text");
                    name = data.getStringExtra("a");
                    description=data.getStringExtra("c");
                    if (Global.img != null) {
                        v.setImageBitmap(Global.img);
                    }

                         as=Long.parseLong(result);
                        c.setText("            " + name + "------" + "RM " + result);
                        break;
  }
        }
       else if(requestCode==CAMERA_REQUEST_CODE)
        {

        }

活动B (活动)

 if(getIntent().hasExtra("data")) {
           // ImageView previewThumbnail = new ImageView(this);
            Bitmap b = BitmapFactory.decodeByteArray(
                    getIntent().getByteArrayExtra("data"),0,getIntent().getByteArrayExtra("data").length);
            viewImage.setImageBitmap(b);
        }

Global.java

public class Global {

    static Bitmap img;
}

当它intent进入活动B时,我的应用程序将自动退出。但是如果图像为,它将起作用captured image。是否因为尺寸太大?我对此没有想法,有人可以帮我吗?非常感谢!


阅读 243

收藏
2020-12-03

共1个答案

小编典典

您不应该将位图放入捆绑包。最好的方法是将图像保存在存储中,并在活动之间传递路径uri。

2020-12-03