1

Im trying to share an image, it seems to work only when the SD card is mounted or when the phone doesn't have a SD card slot. But when I dismount the SD card, it wouldn't share and it gives me two errors.

  • FATAL EXCEPTION: main java.lang.NullPointerException: uriString

  • Failed to insert image java.io.FileNotFoundException: No such file or directory

And for some reason it also saves the image that is being shared, can't seem to figured out why.

            private Button button;

        public void onCreate {

         init();
         setupView();
          }

        public void setupView(){
        button.setOnClickListener(this);
         }



         public void init() {
        button = (Button) findViewById(R.id.button);
          }




          @Override
           public void onClick(View v) {
           int id = v.getId();
           switch (id) {
           case R.id.button: {
              startShare();
               break;
             }




           public void startShare() {
        Bitmap b =BitmapFactory.decodeResource(getResources(),R.drawable.m1);
             Intent share = new Intent(Intent.ACTION_SEND);
            share.setType("image/*");
          ByteArrayOutputStream bytes = new ByteArrayOutputStream();
          b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
          String path = MediaStore.Images.Media.insertImage(getContentResolver(),
            b, "Title", null);
        Uri imageUri =  Uri.parse(path);
        share.putExtra(Intent.EXTRA_STREAM, imageUri);
        startActivity(Intent.createChooser(share, "Share"));
         }
sakura
  • 11
  • 2

1 Answers1

0

try this code for share drawable image:

 Uri imageUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
                            getResources().getResourcePackageName(R.drawable.ic_launcher) + '/' +
                            getResources().getResourceTypeName(R.drawable.ic_launcher) + '/' +
                            getResources().getResourceEntryName(R.drawable.ic_launcher));

                    Intent share = new Intent(Intent.ACTION_SEND);
                    share.setType("image/*");
                    share.putExtra(Intent.EXTRA_STREAM, imageUri);
                    startActivity(Intent.createChooser(share, "Share"));
sneha desai
  • 886
  • 1
  • 11
  • 19
  • I tried that code but it doesn't work it says "You can't add this picture to your message". – sakura Sep 15 '16 at 17:53