文件管理文件发送或打开


    public static void viewFile(final Context context, final String filePath, FileViewInteractionHub fileViewInteractionHub) {
        if (ArchiveHelper.checkIfArchive(filePath)) {
            viewArchive(context, filePath, fileViewInteractionHub);
            return;
        }
        String type = getMimeType(filePath);
        String fileRealPath = filePath;
        if (!TextUtils.isEmpty(type) && !TextUtils.equals(type, "*/*")) {
            /* 设置intent的file与MimeType */
            Intent intent = new Intent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setAction(android.content.Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File(fileRealPath)), type);
            try {
                context.startActivity(intent);
            } catch (ActivityNotFoundException e) {
                Log.e("IntentBuilder", "fail to view file, type: " + type);
                Toast.makeText(context, R.string.msg_unable_open_file, Toast.LENGTH_SHORT).show();
            }
        } else {
            // unknown MimeType
            AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
            dialogBuilder.setTitle(R.string.dialog_select_type);

            CharSequence[] menuItemArray = new CharSequence[] {
                    context.getString(R.string.dialog_type_text),
                    context.getString(R.string.dialog_type_audio),
                    context.getString(R.string.dialog_type_video),
                    context.getString(R.string.dialog_type_image) };
            dialogBuilder.setItems(menuItemArray,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String selectType = "*/*";
                            switch (which) {
                            case 0:
                                selectType = "text/*";
                                break;
                            case 1:
                                selectType = "audio/*";
                                break;
                            case 2:
                                selectType = "video/*";
                                break;
                            case 3:
                                selectType = "image/*";
                                break;
                            }
                            Intent intent = new Intent();
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.setAction(android.content.Intent.ACTION_VIEW);
                            intent.setDataAndType(Uri.fromFile(new File(filePath)), selectType);
                            try {
                                context.startActivity(intent);
                            } catch (ActivityNotFoundException e) {
                                Log.e("IntentBuilder", "fail to view file, type: " + intent.getType());
                                Toast.makeText(context, R.string.msg_unable_open_file, Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
            dialogBuilder.show();
        }
    }
    
    private static void viewArchive(Context context, final String filePath, final FileViewInteractionHub fileViewInteractionHub){
        if(fileViewInteractionHub == null || fileViewInteractionHub.mTabIndex == 0){
            return;
        }
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        String fileName = filePath.substring(filePath.lastIndexOf("/")+1);
        dialogBuilder.setTitle(fileName);
        CharSequence[] menuItemArray = new CharSequence[] {
                context.getString(R.string.decompress_to_current_dir),
                context.getString(R.string.decompress_to)};
        dialogBuilder.setItems(menuItemArray,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        fileViewInteractionHub.mDeComPressFilePath = filePath;
                        switch (which) {
                        case 0:
                            fileViewInteractionHub.onOperationDeCompress();
                            break;
                        case 1:
                            fileViewInteractionHub.updateBarForDeCompress();
                            break;
                        }
                    }
                });
        dialogBuilder.show();
    }

    public static Intent buildSendFile(ArrayList<FileInfo> files) {
        ArrayList<Uri> uris = new ArrayList<Uri>();

        String mimeType = null;
        String preMineType = null;
        boolean isSameMineType = true;
        int sameStringleng = 0;
        for (FileInfo file : files) {
            if (file.IsDir)
                continue;

            File fileIn = new File(file.filePath);
            //modified by tyd mingjun for support bluetooth to send apk
//            mimeType = getSendMimeType(file.fileName);
            mimeType = "audio/mpeg";
            if(preMineType == null){
                sameStringleng = mimeType.indexOf("/");
                preMineType = mimeType.substring(0,sameStringleng + 1);
            }else {
                if(mimeType.startsWith(preMineType) && isSameMineType){
//                    preMineType = mimeType;
                }else {
                    isSameMineType = false;
                }
            }
            Uri u = Uri.fromFile(fileIn);
            uris.add(u);
        }

        if (uris.size() == 0)
            return null;

        boolean multiple = uris.size() > 1;
        Intent intent = new Intent(multiple ? android.content.Intent.ACTION_SEND_MULTIPLE
                : android.content.Intent.ACTION_SEND);

        if (multiple) {
            if(isSameMineType){
                intent.setType(mimeType);                     
            }else {
                intent.setType("*/*");                
            }
            intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
        } else {
            intent.setType(mimeType);
            intent.putExtra(Intent.EXTRA_STREAM, uris.get(0));
        }
        try {
          startActivity(Intent.createChooser(intent, mContext.getString(R.string.send_file)));
        } catch (ActivityNotFoundException e) {
            Log.e(LOG_TAG, "fail to view file: " + e.toString());
        }
        return intent;
    }

    private static String getMimeType(String filePath) {
        int dotPosition = filePath.lastIndexOf('.');
        if (dotPosition == -1)
            return "*/*";

        String ext = filePath.substring(dotPosition + 1, filePath.length()).toLowerCase();
        String mimeType = MimeUtils.guessMimeTypeFromExtension(ext);
        return mimeType != null ? mimeType : "*/*";
    }
    

    private static String getSendMimeType(String filePath) {
        int dotPosition = filePath.lastIndexOf('.');
        if (dotPosition == -1)
            return "application/*";

        String ext = filePath.substring(dotPosition + 1, filePath.length()).toLowerCase();
        String mimeType = MimeUtils.guessMimeTypeFromExtension(ext);
        return mimeType != null ? mimeType : "application/*";
    }
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安卓学习乐园

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值