get ImageView's image and send it to an activity with Intent

前端 未结 7 1492
孤街浪徒
孤街浪徒 2020-12-06 03:22

I have a grid of many products in my app. when the user selects one of the item in the grid, I am starting a new activity as DIALOG box and display the item\'s name,quantity

相关标签:
7条回答
  • 2020-12-06 04:01

    Use Bundle like

    imageView.buildDrawingCache();
    Bitmap image= imageView.getDrawingCache();
    
     Bundle extras = new Bundle();
    extras.putParcelable("imagebitmap", image);
    intent.putExtras(extras);
    startActivity(intent);
    
    
    Bundle extras = getIntent().getExtras();
    Bitmap bmp = (Bitmap) extras.getParcelable("imagebitmap");
    
    image.setImageBitmap(bmp );
    
    0 讨论(0)
  • 2020-12-06 04:01

    When passing around Bitmap data between Activities, I generally prefer to store it in an extended Application class, which you have access to from all your Activities. You can obviously do it as the others have said, by passing it on the Intent, but if you need it in more than a couple of Activities, storing it in the Application gives you more flexibility.

    0 讨论(0)
  • 2020-12-06 04:07

    i had the same problem and solve it with passing the position of selected item via intent. as i think and read from professionals in stack its the best solution. just do that in FirstActivity.java that Contains GridView

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
        Intent intent = new Intent(getActivity(),DetailActivity.class);
        intent.putExtra("id",position);
         startActivity(intent);    } });
    

    and then in your destination Activity you must get the position from extras and extract it from your database or where your images array are cast:

    Intent intent = getActivity().getIntent();
                int position = intent.getIntExtra("id",0);
                ImageAdapter imageAdapter = new ImageAdapter(getActivity());
                ImageView imageView = (ImageView)rootView.findViewById(R.id.movie_detail_image);
                imageView.setImageResource((int)imageAdapter.getItem(position));
    

    you must edit your image adapter getItem method to return selected image id.

    0 讨论(0)
  • 2020-12-06 04:09

    first you'll have to save your image first and then you can use this method to send the file name of your saved image

        void Send() { 
                if (file != null) {
                Intent intent = new Intent(this, Draw.class);
                intent.putExtra(PICTURE_FILE_ID, file);
                startActivity(intent);
            } else if (file == null) {
                Toast tst = Toast.makeText(getApplication(),
                        "Please Click Save First", Toast.LENGTH_SHORT);
                tst.setGravity(Gravity.CENTER, 0, 0);
                tst.show();
            }
    }
    

    the other activity

    use mBitmapFile = (File) getIntent().getSerializableExtra( YourClassName.PICTURE_FILE_ID);

    0 讨论(0)
  • 2020-12-06 04:10

    You can convert your drawable to bitmap then into byte array and pass this byte array with intent.

    Drawable d;  
    Bitmap b= ((BitmapDrawable)d).getBitmap();
    
    int bytes = b.getByteCount();
    ByteBuffer buffer = ByteBuffer.allocate(bytes);  
    b.copyPixelsToBuffer(buffer);  
    byte[] array = buffer.array();
    

    user this code :

    calling activity...

    Intent i = new Intent(this, NextActivity.class);
    Bitmap b; // your bitmap
    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    b.compress(Bitmap.CompressFormat.PNG, 50, bs);
    i.putExtra("byteArray", bs.toByteArray());
    startActivity(i);
    

    ...and in your receiving activity

    if(getIntent().hasExtra("byteArray")) {
        ImageView previewThumbnail = new ImageView(this);
        Bitmap b = BitmapFactory.decodeByteArray(
            getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length); 
    }
    
    0 讨论(0)
  • 2020-12-06 04:15

    You can put Parcable (bitmap for example) or serializable objects in a bundle and retrieve the object from the Bundle in the other activity.

    how do you pass images (bitmaps) between android activities using bundles?

    Although I would not recommend it because you pass a lot of data between activities. I would recommend you to pass the image reference id to the next activity and retrieve the image there.

    How to pass the selectedListItem's object to another activity?

    EDIT:

    intent.putExtra("imageId", imageId);
    

    in the other activity:

    int imageRef = getIntent().getIntExtra("imageId", defaultValue);
    

    http://developer.android.com/reference/android/content/Intent.html#getIntExtra%28java.lang.String,%20int%29

    0 讨论(0)
提交回复
热议问题