自定义图像共享对话框到其他应用程序

时间:2016-09-01 10:43:43

标签: android android-intent

我有一个应用程序,可以共享和接收来自其他应用程序的图像。我这里有两个问题

  • 在分享来自其他地方的图片时,请从我的应用程序显示的图库中显示,但使用其整个包名称,即com.test.myapp而不是仅使用应用名称

这是它的清单

    <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>
  • 其次,Ion从我的应用程序共享图像我不想在共享对话框上显示我自己的应用程序,但当我删除我的应用程序时,我得到一些不需要的系统应用程序以及一些带有默认android图标的应用程序。

这是共享代码

    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);

提前致谢

2 个答案:

答案 0 :(得分:1)

还是不确定第一个问题,但找到了第二个问题的解决方案。

以下是使用您想要的任何应用创建自己的共享对话框的代码

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();

希望能帮助其他寻找类似事物的人。

答案 1 :(得分:0)

public static Intent shareImage(Context context, String pathToImage) {
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    else
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
    shareIntent.setType("image/*");
    // For a file in shared storage.  For data in private storage, use a ContentProvider.
    Uri uri = Uri.fromFile(context.getFileStreamPath(pathToImage));
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    return shareIntent;
}

refer this http://android-developers.blogspot.com/2012/02/share-with-intents.html