1
@Override
        public void onClick(View v) {
            try {
                //Write fileObject tag = imageView.getTag();                  

                //Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.c);

            Bitmap bmp= BitmapFactory.decodeResource(getResources(),v.getResources().getResourceName((Integer)v.getTag()));


                String filename = "bitmap.png";
                FileOutputStream stream = getApplicationContext().openFileOutput(filename, Context.MODE_PRIVATE);
                bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);

                //Cleanup
                stream.close();
                bmp.recycle();

i want to add the image id from viewPager instead of the R.drawable.img1, any1 please any help? i have edited my question, instaed of getting image from drawable i need to get ithe current image on viewPager. how do i do it?

sapamlucy
  • 308
  • 1
  • 3
  • 15

2 Answers2

1

i have found the solution to this

Bitmap bmp=getBitmapFromView(viewPager.getChildAt(1));

i got my solution with this, thanks for all your feedbacks

sapamlucy
  • 308
  • 1
  • 3
  • 15
  • sorry for the late reply, getbitmap from view is the function u use to get the decoded image from ur viewPager as it was required in my application search it on google u will get better explanations – sapamlucy May 12 '15 at 07:15
0

View Pager must be having an adapter. In getView method, set position as tag to the imageviews. Then for each positions , access the imageviews as per requirement.

Aparupa Ghoshal
  • 309
  • 2
  • 10
  • i use only one image view so how i tell my image view to get that current id and pass it on for sharing, i mean it what i wanna do, share the current image on fb , i just cant seem to be able to get the current image id – sapamlucy Oct 27 '14 at 06:52
  • Have you set an adapter to the view pager? – Aparupa Ghoshal Oct 27 '14 at 06:54
  • yes i have set the adapter and m loading images from drawable into a viewPager – sapamlucy Oct 27 '14 at 06:56
  • In getView method you have to set the id of the imageView. Then set tag to the imageview as :- imageView.setTag(position) , where position is the current position of the adapter. Then , perform getTag to get the position and fetch the drawable/image bitmap as per requirement. – Aparupa Ghoshal Oct 27 '14 at 06:59
  • i have set the tag for position and i use this tag for other functions in my code, but how do i convert the current image shown on the viewPager to bitmap, all images are initially in drawable the user sees one image and shares it, so i need to convert it to bitmap, but decodeResources() takes only resourceId so how do i get the id??? – sapamlucy Oct 27 '14 at 07:04
  • that is i want the id not the position, please help Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.c); instead of r.drawable.blahblah, i want to put the current image id – sapamlucy Oct 27 '14 at 07:05
  • First create a temporary imageView as :- ImageView tempImg = (ImageView)view.findViewWithTag(position); and then, Bitmap bitmap = ((BitmapDrawable)tempImg.getDrawable()).getBitmap(); – Aparupa Ghoshal Oct 27 '14 at 07:06
  • m getting a null pointer exception it seems i cant get the id – sapamlucy Oct 27 '14 at 07:17
  • You are setting the tag-position only for imageview or for some other widgets also? – Aparupa Ghoshal Oct 27 '14 at 07:19
  • Also , you need to access it as view.findViewByTag , where view = the layout inflated in the adapter. – Aparupa Ghoshal Oct 27 '14 at 07:20
  • no under public Object instantiateItem(ViewGroup container, final int position) i have included imageView.setTag("myview" + position); – sapamlucy Oct 27 '14 at 07:21
  • While fetching , please check if you are using "myview" + position as your tag? Otherwise it would not work. Also I would suggest you to Log the position , to check whether you are obtaining the correct position or not. – Aparupa Ghoshal Oct 27 '14 at 07:24
  • APrupa i have acessed it correctly as u said from my button click Listener and somehow i get an npe when i click on this share button – sapamlucy Oct 27 '14 at 07:25
  • this share button is present with the same layout ie same parent view where image is present? Or image is in inflated view , and button is in some other view? – Aparupa Ghoshal Oct 27 '14 at 07:27
  • that is while fetching the image in the button click event ryt? n there is an issue wen i use position in my button click event position variable cannot be resolved, i put my buttons in OnCreateBundle() and the imageAdapter is in a different class – sapamlucy Oct 27 '14 at 07:28
  • yes all the buttons are in the same layout as the viewPager where the images are to shown – sapamlucy Oct 27 '14 at 07:31
  • Yes, I guessed this only !! That's why they don't have any relation with each other and getting npe !! When you are clicking the button to fetch the bitmap , you have to get the position , otherwise it's difficult... also you must have the access of the imageview to get the tags and access it. – Aparupa Ghoshal Oct 27 '14 at 07:31
  • so we are back to square one, how do i get this position m going crazy – sapamlucy Oct 27 '14 at 07:32