Search code examples
androidandroid-intent

Custom Image Sharing Dialog to other apps


I have an app which shares and receive images from other apps. I have two problems here

  • On sharing Image from some other place let say from gallery my app is showing but with its whole package name i.e. com.test.myapp instead of only with the app name

here is the manifest of it

    <activity
        android:name=".newdesign.SplashActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEND" />

            <category android:name="android.intent.category.DEFAULT" />

            <data android:mimeType="image/*" />
        </intent-filter>
    </activity>
  • Secondly, Ion sharing the image from my app I don't want to display my own app on the share dialog, but when I remove my app I get some unwanted system apps along with some app with default android icons.

here is the sharing code

    List<Intent> targetedShareIntents = new ArrayList<Intent>();
        Intent share = new Intent(android.content.Intent.ACTION_SEND);
        share.setType("image/*");
        List<ResolveInfo> resInfo = ExportOrSaveActivity.this.getPackageManager().queryIntentActivities(createShareIntent(), 0);
        if (!resInfo.isEmpty()) {
            for (ResolveInfo info : resInfo) {
                Intent targetedShare = createShareIntent();
                if (!info.activityInfo.packageName.equalsIgnoreCase("com.test.myapp1") && !info.activityInfo.packageName.equalsIgnoreCase("com.test.myapp2")) {
                    targetedShare.setPackage(info.activityInfo.packageName);
                    targetedShareIntents.add(targetedShare);
                }
            }
            Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0),
                    "Select app to share");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
                    targetedShareIntents.toArray(new Parcelable[] {}));
            startActivity(chooserIntent);

Thanks in advance


Solution

  • Still Not Sure about the first issue, but found a solution to the second one .

    Here is the code to create you own share dialog with whichever apps you want

    final  List<Item> items=new ArrayList<>();
    
            Intent audioIntent = new Intent(android.content.Intent.ACTION_SEND);
            audioIntent.setDataAndType(Uri.parse("file://" + MainActivity.sSavedImagePath), "image/*");
            List<ResolveInfo> audio = ExportOrSaveActivity.this.getPackageManager().queryIntentActivities(audioIntent, 0);
            for (ResolveInfo info : audio){
                String label = info.loadLabel(ExportOrSaveActivity.this.getPackageManager()).toString();
                Drawable icon = info.loadIcon(ExportOrSaveActivity.this.getPackageManager());
                String packageName = info.activityInfo.packageName;
                String name = info.activityInfo.name;
                Item ita=new Item(label , packageName ,icon );
                if (!info.activityInfo.packageName.equalsIgnoreCase("com.package.toskip") && !info.activityInfo.packageName.equalsIgnoreCase("com.package.toskiptoo")) {
                    items.add(ita);
                }
    
            }
    
    
            ListAdapter adapter = new ArrayAdapter<Item>(
                    this,
                    android.R.layout.select_dialog_item,
                    android.R.id.text1,
                    items){
                public View getView(int position, View convertView, ViewGroup parent) {
                    //Use super class to create the View
                    View v = super.getView(position, convertView, parent);
                    TextView tv = (TextView)v.findViewById(android.R.id.text1);
    
                    //Put the image on the TextView
                    tv.setCompoundDrawablesWithIntrinsicBounds(items.get(position).icon, null, null, null);
    
                    //Add margin between image and text (support various screen densities)
                    int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
                    tv.setCompoundDrawablePadding(dp5);
                    tv.setTextColor(R.color.black);
    
                    return v;
                }
            };
    
    
            new AlertDialog.Builder(ExportOrSaveActivity.this,android.R.style.Theme_Holo_Light)
                    .setTitle("Share Image")
                    .setAdapter(adapter, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int item) {
                            Intent share = new Intent(Intent.ACTION_SEND);
                            share.setPackage(items.get(item).packagename);
                            share.setType("image/*");
                            share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + MainActivity.sSavedImagePath));
                            startActivity(share);
    
                        }
                    }).show();
    

    Hope it help other who are looking for some thing similar.