0

Hi I have a gallery in my app which shows the image in an image view when you click on the thumbnail. I am setting up a Long Click Listener for an alert dialog that has 2 buttons one to set as wallpaper and one to share. I have the share intent and the get drawing cache somewhat working. It works in the emulator but not on my phone. I have used many of the examples on this site to get this going but its not working at all. it keeps force closing the app on my phone. any help is appreciated.

                alertDialog.setButton2("Share",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {
                            imgView.setDrawingCacheEnabled(true);
                            Bitmap b = imgView.getDrawingCache();
                            File sdCard = Environment.getExternalStorageDirectory();
                            File file = new File(sdCard, "image.jpg");
                            FileOutputStream fos;
                            try {
                                fos = new FileOutputStream(file);
                                b.compress(CompressFormat.JPEG, 95,fos);
                               } catch (FileNotFoundException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                               }

                            Log.d("Save Example", "Image saved.");

                            Intent intent = new Intent(Intent.ACTION_SEND);
                            intent.setType("image/jpeg");
                            intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/image.jpg"));

                            startActivity(Intent.createChooser(intent, "Share Picture Via..")); 
                        }   

            });

            alertDialog.show();
            return true;

Update: it seems to be a problem with

b.compress(CompressFormat.JPEG, 95, fos);

it keeps saying null pointer.

Turns out the null pointer is actually a read_only error..So its not actually writing the file i get a ioexception read-only file system. Why is it doing this?

Dirk Jäckel
  • 2,979
  • 3
  • 29
  • 47
Pestilence
  • 11
  • 1
  • 7

1 Answers1

0
imgView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); imgView.layout(0, 0, imgView.getMeasuredWidth(), imgView.getMeasuredHeight());

See if this helps before

 Bitmap b = imgView.getDrawingCache();

ALso see if this helps Android drawing cache

Community
  • 1
  • 1
Vrashabh Irde
  • 14,129
  • 6
  • 51
  • 103