共享图标不适用于每台设备

时间:2014-12-03 12:20:20

标签: android device

我在我的应用程序中有共享图标,允许在facebook,whatsapp,twitter等上共享图像。我在三个移动设备上测试它在一个移动设备上工作正常但在其他两个上没有打开任何东西。这是我要求点击共享图标的代码。它在一台设备上工作正常而不在其他两台设备上的原因是什么..是编码问题还是设备问题?我使用的是API 19移动设备

if (lActionBtnTag.startsWith(G.General.TAG_SHARE))
            {

                aView.setEnabled(false);
                actionHandler.shareG(gMoments.get(lPosition), aView);
            }
  public void shareG(final GMoment aGMoment, final View aShareBtn)
    {
        try {

            String lMediaType = aGMoment.getGMediaType();
            if (lMediaType.equals(G.General.MEDIA_TYPE_PHOTO) || lMediaType.equals(G.General.MEDIA_TYPE_MEME))
            {
                _handleImageShare(aGMoment, aShareBtn);

            }
            else if (lMediaType.equals(Guggu.General.MEDIA_TYPE_AUDIO))
            {

            }
            else if (lMediaType.equals(Guggu.General.MEDIA_TYPE_VIDEO))
            {

            }
        } catch (Exception ex) {
            Log.e(G.Tag, "Exception saving image to share: " + aGMoment.getGMediaLoc(), ex);
        }
    }
    private void _handleImageShare(final GMoment aGMoment, final View aShareBtn)
        {

            final String lShareableFileName = "xyz.jpg";
            imageLoader.get(aGMoment.getGMediaLoc(), new ImageLoader.ImageListener()
            {
                @Override
                public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b) {

                    try {
                        FileOutputStream outputStream = mCtxt.getApplicationContext().openFileOutput(lShareableFileName,
                                Context.MODE_WORLD_READABLE);

                        Bitmap lBmp = imageContainer.getBitmap();

                        if (lBmp != null) {
                            ByteArrayOutputStream stream = new ByteArrayOutputStream();
                            lBmp.compress(Bitmap.CompressFormat.JPEG, 50, stream);
                            byte[] imageInByte = stream.toByteArray();
                            stream.close();

                            ByteArrayInputStream in = new ByteArrayInputStream(imageInByte);
                            IOUtils.copy(in, outputStream);
                            outputStream.close();
                            in.close();

                            aShareBtn.setEnabled(true);
                            _launchShareIntents(lShareableFileName, aGMoment.getDescription());
                        }
                    } catch (Exception ex) {
                        // TODO: Log this exception
                        ex.printStackTrace();
                    }
                }

                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    aShareBtn.setEnabled(true);
                    Log.e(Guggu.Tag, volleyError.getMessage());
                }
            });
        }

        private void _launchShareIntents(String aImgPath, String aDesc)
        {

            String lSelectedIntent;
            List<Intent> intentList = new ArrayList<Intent>();

            final PackageManager mgr = mCtxt.getPackageManager();
            Intent sendIntent = new Intent(Intent.ACTION_SEND);
            sendIntent.setType("text/plain");

            List<ResolveInfo> list = mgr.queryIntentActivities(sendIntent, 0);

            for (ResolveInfo info : list) {
                lSelectedIntent = info.activityInfo.packageName;

                if (lSelectedIntent.contains("twitter") || lSelectedIntent.contains("facebook") || lSelectedIntent.contains("whatsapp"))
                {
                    Intent intent = new Intent();
                    intent.setComponent(new ComponentName(lSelectedIntent, info.activityInfo.name));
                    intent.setAction(Intent.ACTION_SEND);
                    intent.setType("image/*");

                    if (lSelectedIntent.contains("twitter") || lSelectedIntent.contains("whatsapp"))
                    {
                        intent.putExtra(Intent.EXTRA_TEXT, aDesc);
                        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mCtxt.getFileStreamPath(aImgPath)));

                    }
                    else if (lSelectedIntent.contains("facebook"))
                    {
                        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mCtxt.getFileStreamPath(aImgPath)));
                    }

                    intentList.add(new LabeledIntent(intent, lSelectedIntent, info.loadLabel(mgr), info.icon));
                }
            }

            if (intentList == null && intentList.size() == 0)
            {
                return;
            }

            Intent  openInChooser = Intent.createChooser(intentList.remove(0), "Share this image");
            openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[]{}));

            mCtxt.startActivity(openInChooser);
        }

Intent.java

public static Intent createChooser(Intent target, CharSequence title) {
        Intent intent = new Intent(ACTION_CHOOSER);
        intent.putExtra(EXTRA_INTENT, target);
        if (title != null) {
            intent.putExtra(EXTRA_TITLE, title);
        }
        // Migrate any clip data and flags from target.
        int permFlags = target.getFlags() & (FLAG_GRANT_READ_URI_PERMISSION
                | FLAG_GRANT_WRITE_URI_PERMISSION | FLAG_GRANT_PERSISTABLE_URI_PERMISSION
                | FLAG_GRANT_PREFIX_URI_PERMISSION);
        if (permFlags != 0) {
            ClipData targetClipData = target.getClipData();
            if (targetClipData == null && target.getData() != null) {
                ClipData.Item item = new ClipData.Item(target.getData());
                String[] mimeTypes;
                if (target.getType() != null) {
                    mimeTypes = new String[] { target.getType() };
                } else {
                    mimeTypes = new String[] { };
                }
                targetClipData = new ClipData(null, mimeTypes, item);
            }
            if (targetClipData != null) {
                intent.setClipData(targetClipData);
                intent.addFlags(permFlags);
            }
        }

        return intent;
    }

0 个答案:

没有答案