Search code examples
androidandroid-intentdialogshareevernote

Custom share dialog,not working for some apps


I have implemented a custom share dialog,in my android app.I am sharing images,interesting enough it does not work for Evernote application.I can share with Evernote by the default way.But it does not work with this code.I have experienced this only for the Evernote application.

This is my code :

public void doShareCustomDialog() {
    Bitmap curBitmap = ((ImagePagerAdapter) viewPager.getAdapter())
            .getCurrentBitmap();
    final Intent imageIntent = new Intent();
    imageIntent.setAction(Intent.ACTION_SEND);
    imageIntent.setType("image/jpeg");
    imageIntent.putExtra(Intent.EXTRA_STREAM,
            getImageUri(getActivity(), curBitmap));

    final Dialog dialog = new Dialog(getActivity());
    dialog.setTitle("Choose application:");
    dialog.getWindow().setBackgroundDrawable(
            new ColorDrawable(R.color.black_overlay));
    dialog.setCanceledOnTouchOutside(true);
    ListView lv = new ListView(getActivity());
    dialog.setContentView(lv);
    dialog.show();

    PackageManager pm = getActivity().getPackageManager();
    List<ResolveInfo> launchables = pm
            .queryIntentActivities(imageIntent, 0);
    Collections
            .sort(launchables, new ResolveInfo.DisplayNameComparator(pm));

    final AppAdapter adapter = new AppAdapter(pm, launchables);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            ResolveInfo launchable = adapter.getItem(position);
            ActivityInfo activity = launchable.activityInfo;
            ComponentName name = new ComponentName(
                    activity.applicationInfo.packageName, activity.name);
            imageIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            imageIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            imageIntent.setComponent(name);
            startActivity(imageIntent);
        }
    });

}

This is the getUri() method:

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = Images.Media.insertImage(inContext.getContentResolver(),
            inImage, "Title", null);
    latestSharedImageURI = Uri.parse(path);
    return latestSharedImageURI;
}

This code here works perfectly fine,but its without the custom dialog,but with the default one.

public void doShare() {
    Bitmap curBitmap = ((ImagePagerAdapter) viewPager.getAdapter())
            .getCurrentBitmap();
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.setType("image/jpeg");
    intent.putExtra(Intent.EXTRA_STREAM,
            getImageUri(getActivity(), curBitmap));
    setShareIntent(intent);
    startActivity(Intent.createChooser(intent,
            getResources().getText(R.string.share_choose)));
}

Solution

  • I need to mention first that I'm an Evernote employee. I tried your snippet and it can work. It depends on this method:

    imageIntent.putExtra(Intent.EXTRA_STREAM,
            getImageUri(getActivity(), curBitmap));
    

    You may want to post how you generate the Uri. In my test this definitely doesn't work if your file is in your app's internal storage. If the file is stored on the external storage, then your code works if the Uri is correct.

    If you want to share a file from the internal storage you need to implement the FileProvider and generate the Uri with getUriForFile. Then it worked with my test.