android 拖拽发送不成功调起分享的功能

ViewRootImpl.java

// When the drag operation ends, reset drag-related state
                if (what == DragEvent.ACTION_DRAG_ENDED) {
                    mCurrentDragView = null;
                    setLocalDragState(null);
                    mAttachInfo.mDragToken = null;
                    if (mAttachInfo.mDragSurface != null) {
                        mAttachInfo.mDragSurface.release();
                        mAttachInfo.mDragSurface = null;
                    }
                }
                //add begin
                if (!result && event.mClipData != null) {
                    int count = event.getClipData().getItemCount();
                    Intent intent = new Intent();
                    if (count == 1) {
                        intent = new Intent(Intent.ACTION_SEND);
                        intent.setType(getCommonMimeType(event));
                        intent.putExtra(Intent.EXTRA_STREAM, getUris(event).get(0));
                    } else if (count > 1) {
                        intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
                        intent.setType(getCommonMimeType(event));
                        intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, getUris(event));
                    }
                    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
                    StrictMode.setVmPolicy(builder.build());
                    builder.detectFileUriExposure();
                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    intent.addCategory(Intent.CATEGORY_DEFAULT);
                    Intent chooserIntent = Intent.createChooser(
                            intent, "").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(chooserIntent);
                }
public static String getCommonMimeType(List<String> mimeTypes) {
    if (mimeTypes == null || mimeTypes.size() <= 0) {
        return null;
    }

    boolean sameType = true;
    for (int i = 1; i < mimeTypes.size(); ++i) {
        if (!mimeTypes.get(0).equals(mimeTypes.get(i))) {
            sameType = false;
            break;
        }
    }
    if (sameType) {
        return mimeTypes.get(0);
    }

    int index0 = mimeTypes.get(0).indexOf("/");
    if (index0 == -1) {
        return null;
    }
    for (int i = 1; i < mimeTypes.size(); ++i) {
        int indexNow = mimeTypes.get(i).indexOf("/");
        if (indexNow == -1) {
            return null;
        }
        if (indexNow != index0
                || !mimeTypes.get(0).regionMatches(0, mimeTypes.get(i), 0,
                indexNow)) {
            return "*/*";
        }
    }
    return mimeTypes.get(0).substring(0, index0 + 1) + "*";
}

public static String getCommonMimeType(DragEvent event) {
    List<String> mimeTypes = new ArrayList<String>();
    for (int i = 0; i < event.getClipDescription().getMimeTypeCount(); ++i) {
        mimeTypes.add(event.getClipDescription().getMimeType(i));
    }
    return getCommonMimeType(mimeTypes);
}

public static ArrayList<Uri> getUris(DragEvent event) {
    ArrayList<Uri> uris = new ArrayList<Uri>();
    for (int i = 0; i < event.getClipData().getItemCount(); ++i) {
        uris.add(event.getClipData().getItemAt(i).getUri());
    }
    return uris;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值