3

I am trying to attach image from my gridview to gmail or facebook,but whenever i tried to attach my app got crash,and i am getting following error with nullpointer exception,following is my code with gridview image selection,can any one help?

public class Free_Cover_Activity extends AppCompatActivity
{
 GridView grid;
 int[] imageId = {
  R.drawable.discovercover,
  R.drawable.burpresswordfree,
  R.drawable.flyfree,
  R.drawable.cantmovefree,
  R.drawable.cantmovewordfree,
  R.drawable.chalkthisfree,
  R.drawable.fivehundredmetersfree,
  R.drawable.freeexercise,
  R.drawable.gym_smilie,
  R.drawable.hundredcalrairesfree,
  R.drawable.injuryfree,
  R.drawable.jumpropefree,
  R.drawable.nicesnathcfree,
  R.drawable.personglrecordfree,
  R.drawable.posefree,
  R.drawable.pushupfree,
  R.drawable.shoulder,
  R.drawable.timewordfree,
  R.drawable.unbrokernfree,
  R.drawable.weightbeltfree
 };

 private Bitmap mBitmap;
 private Intent email;

 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);

  setContentView(R.layout.free_cover_gridview);
  android.support.v7.app.ActionBar abar =  getSupportActionBar();
  ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#D64365"));
  abar.setBackgroundDrawable(colorDrawable);
  abar.hide();

  CustomGridFreeCover adapter = 
               new CustomGridFreeCover(Free_Cover_Activity.this, imageId);
  grid=(GridView)findViewById(R.id.freecover_grid);
  grid.setAdapter(adapter);
  grid.setOnItemClickListener(new AdapterView.OnItemClickListener()
  {
   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id)
   {
    try
    {
     Bitmap largeIcon = 
         BitmapFactory.decodeResource(getResources(), R.drawable.discovercover);

     /*
     replace "R.drawable.bubble_green" with the image resource 
     you want to share from drawable
     */
     ByteArrayOutputStream bytes = new ByteArrayOutputStream();
     largeIcon.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

     //you can create a new file name "test.jpg" in sdcard folder.
     File f = 
        new File(Environment.getExternalStorageDirectory() + File.separator + "test.jpg");
     f.createNewFile();

     //write the bytes in file
     FileOutputStream fo = new FileOutputStream(f);
     fo.write(bytes.toByteArray());

     //remember close de FileOutput
     fo.close();
    } catch (IOException e) {
     //TODO Auto-generated catch block
     e.printStackTrace();
    }

    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("image/jpeg");
    //set your subject
    shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Hi"); 
    //set your message
    shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "How are you"); 

    String imagePath = Environment.getExternalStorageDirectory() 
           + File.separator + "test.jpg";
    File imageFileToShare = new File(imagePath);
    Uri uri = Uri.fromFile(imageFileToShare);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(shareIntent, "Share Image"));
   }
  });
 }

 public class CustomGridFreeCover extends BaseAdapter
 {
  private Context mContext;
  //private final String[] web;
  private final int[] Imageid;

  public CustomGridFreeCover(Context c,int[] Imageid )
  {
   mContext = c;
   this.Imageid = Imageid;
   //this.web = web;
  }

  @Override
  public int getCount()
  {
   //TODO Auto-generated method stub
   return Imageid.length;
  }

  @Override
  public Object getItem(int position) {
   //TODO Auto-generated method stub
   return null;
  }

  @Override
  public long getItemId(int position) {
   //TODO Auto-generated method stub
   return 0;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent)
  {
   //TODO Auto-generated method stub
   View grid;
   LayoutInflater inflater = (LayoutInflater) mContext
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null)
    {
     grid = new View(mContext);
     grid = inflater.inflate(R.layout.free_cover_griditem, null);
     //TextView textView = (TextView) grid.findViewById(R.id.grid_text);
     ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image_freecover);
     //textView.setText(web[position]);
     imageView.setImageResource(Imageid[position]);
    } else {
     grid = (View) convertView;
    }
   return grid;
  }
 }
}
Reporter
  • 3,897
  • 5
  • 33
  • 47
Aditya
  • 1,508
  • 1
  • 19
  • 37

4 Answers4

2

Here is the working code which you need:

Firstly save image from Drawable to SD Card here is the code:

try{

Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.bubble_green);

            //replace "R.drawable.bubble_green" with the image resource you want to share from drawable 

            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            largeIcon.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

            // you can create a new file name "test.jpg" in sdcard folder.
            File f = new File(Environment.getExternalStorageDirectory() + File.separator + "test.jpg");

            f.createNewFile();

            // write the bytes in file
            FileOutputStream fo = new FileOutputStream(f);
            fo.write(bytes.toByteArray());

            // remember close de FileOutput
            fo.close();
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Then get the saved image from SD card and attach in the email intent like this:

        Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
        shareIntent.setType("image/jpeg");

        shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Hi"); //set your subject
        shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "How are you"); //set your message

        String imagePath = Environment.getExternalStorageDirectory() + File.separator + "test.jpg";

        File imageFileToShare = new File(imagePath);

        Uri uri = Uri.fromFile(imageFileToShare);

        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);

        startActivity(Intent.createChooser(shareIntent, "Share Image"));
Anand Singh
  • 5,672
  • 2
  • 23
  • 33
  • f.createNewFile();..red line unhandled exception – Aditya Aug 06 '15 at 08:01
  • the problem is image is attach but its not viewing and not able to send..i am testing in device samsung edge.lollipop version – Aditya Aug 06 '15 at 08:40
  • @Roman i tried this code and it is working perfectly in Nexus 9 (Lollipop). Are you setting `` permission in your manifest? – Anand Singh Aug 06 '15 at 08:44
  • @Roman Ok, check your directory whether you can see "test.jpg" or not? Try to run same code in other device, check whether it is working there? – Anand Singh Aug 06 '15 at 08:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/85280/discussion-between-roman-and-anand-singh). – Aditya Aug 06 '15 at 08:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/85377/discussion-between-roman-and-anand-singh). – Aditya Aug 07 '15 at 05:56
  • can you tell http://stackoverflow.com/questions/31870132/how-to-change-color-of-hamburger-icon-in-material-design-navigation-drawer – Aditya Aug 07 '15 at 05:56
1

This is what worked for me perfectly.

            Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
            shareIntent.setType("image/jpeg");

            shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getResources().getString(R.string.share_subject));
            shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, getResources().getString(R.string.share_message));

            shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://" + C.PROJECT_PATH + "/drawable/" + R.drawable.image_to_share);

            startActivity(Intent.createChooser(shareIntent, "Share Image"));

But I will always recommend to decode a Bitmap from this resource, save it as a File in the storage and then use that file. A more reliable way this directly using from the resources.

Aritra Roy
  • 15,355
  • 10
  • 73
  • 107
1

roman thing is you are passing a null bitmap object

 Bitmap icon = mBitmap;

you need to assign bitmap first then the code will start work

for more info you can have a look on to below links :-

https://github.com/codepath/android_guides/wiki/Sharing-Content-with-Intents

http://developer.android.com/training/sharing/send.html

kuljeet singh
  • 2,792
  • 2
  • 12
  • 15
  • with that link extension is not sending to gmail or fb – Aditya Aug 06 '15 at 07:04
  • 1
    hello roman you need to pass your drawable or image source as a **URI** first and then you can send that URI by using below intent :- Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage); shareIntent.setType("image/jpeg"); startActivity(Intent.createChooser(shareIntent,"Share Image")); please note **uriToImage** is object which will store your image resource as uri – kuljeet singh Aug 06 '15 at 07:15
  • 1
    and please see my edited answer I have updated the link you can find a information https://github.com/codepath/android_guides/wiki/Sharing-Content-with-Intents – kuljeet singh Aug 06 '15 at 07:17
  • icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);..as per error line it shows issuie here – Aditya Aug 06 '15 at 07:24
  • 1
    the thing is object "icon" is null means there is no image bitmap in it . so you need to assign the bitmap or image as a bitmap to object "icon" – kuljeet singh Aug 06 '15 at 07:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/85273/discussion-between-kuljeet-singh-and-roman). – kuljeet singh Aug 06 '15 at 07:27
  • need help http://stackoverflow.com/questions/31870132/how-to-change-color-of-hamburger-icon-in-material-design-navigation-drawer – Aditya Aug 07 '15 at 07:44
1

First Convert your drwable to image and store it on SDCard

Store Drawable to SD card


Then

private void shareImage() {
        Intent share = new Intent(Intent.ACTION_SEND);

        // setType("image/png"); OR for jpeg: setType("image/jpeg");
        share.setType("image/*");

        // Make sure you put example png image named yourImg.png in your
        // directory

        String imagePath = Environment.getExternalStorageDirectory()
                + "/yourImg.png";

        File imageFileToShare = new File(imagePath);

        Uri uri = Uri.fromFile(imageFileToShare);
        share.putExtra(Intent.EXTRA_STREAM, uri);

        startActivity(Intent.createChooser(share, "Share Image!"));
    }
Community
  • 1
  • 1
N J
  • 27,217
  • 13
  • 76
  • 96