Home » Android » First time click on context item not work

First time click on context item not work

Posted by: admin November 1, 2017 Leave a comment


My problem is that When I open my app (my app show a pictures list), with a long clik on item list a context menu opens and I can choose to share the image, but if i click once not work and it only happens for the first time I open the application.

This is my code:

@RequiresApi(api = Build.VERSION_CODES.N)
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.author_images:
            return true;
        case R.id.share:
            Share share = new Share(item, position, mvc, getContext(), getActivity());
            return true;
            return super.onContextItemSelected(item);

And this is the Share class:

public class Share extends AsyncTask<Void, Void, Bitmap> {
private final MenuItem menuItem;
private final int imagePos;
private final MVC mvc;
private final Context context;
private final Activity activity;

public Share(MenuItem menuItem, int imagePos, MVC mvc, Context context, Activity activity){
    this.menuItem = menuItem;
    this.imagePos = imagePos;
    this.mvc = mvc;
    this.context = context;
    this.activity = activity;

protected Bitmap doInBackground(Void... params) {
    return downloadBitmap(mvc.model.getPictureInfos()[imagePos].getUrl());

@RequiresApi(api = Build.VERSION_CODES.N)
protected void onPostExecute(Bitmap bitmap) {
    ShareActionProvider shareActionProvider = (ShareActionProvider) menuItem.getActionProvider();
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, getUriFromBitmap(bitmap, context, activity));

private Bitmap downloadBitmap(String url) {
    HttpURLConnection urlConnection = null;
    try {
        URL uri = new URL(url);
        urlConnection = (HttpURLConnection) uri.openConnection();

        int statusCode = urlConnection.getResponseCode();
        //if (statusCode != HttpStatus.SC_OK) {
          //  return null;

        InputStream inputStream = urlConnection.getInputStream();
        if (inputStream != null) {

            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            return bitmap;
    } catch (Exception e) {
        Log.d("URLCONNECTIONERROR", e.toString());
        if (urlConnection != null) {
        Log.w("ImageDownloader", "Error downloading image from " + url);
    } finally {
        if (urlConnection != null) {

    return null;

@RequiresApi(api = Build.VERSION_CODES.N)
private static Uri getUriFromBitmap(Bitmap bitmap, Context context, Activity activity) {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File storage = Environment.getExternalStoragePublicDirectory("Android/data/com.flickrclient.mattia.flickrclient/files");
    File newFile = new File(storage, timeStamp + ".jpg");

    //verifico che l'app abbia il permesso WRITE_EXTERNAL_STORAGE per le versioni di android superiori alla 6.0
    if (Build.VERSION.SDK_INT >= 23) {
        int permissionCheck = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);

    FileOutputStream out = null;
    try {
        out = new FileOutputStream(newFile);
    } catch (FileNotFoundException e) {

    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
    Uri uri = FileProvider.getUriForFile(context , "com.flickrclient.mattia.flickclient", newFile);

    return uri;

can anyone tell me where I am wrong?