Launching a file using ACTION_VIEW Intent Action

耗尽温柔 提交于 2019-12-06 09:48:52

try this

if (item_ext.equalsIgnoreCase(".mp3") || 
                 item_ext.equalsIgnoreCase(".m4a")||
                 item_ext.equalsIgnoreCase(".mp4")) {

            if(mReturnIntent) {
                returnIntentResults(file);
            } else {
                Intent i = new Intent();
                i.setAction(android.content.Intent.ACTION_VIEW);
                i.setDataAndType(Uri.fromFile(file), "audio/*");
                startActivity(i);
            }
        }

        /*photo file selected*/
        else if(item_ext.equalsIgnoreCase(".jpeg") || 
                item_ext.equalsIgnoreCase(".jpg")  ||
                item_ext.equalsIgnoreCase(".png")  ||
                item_ext.equalsIgnoreCase(".gif")  || 
                item_ext.equalsIgnoreCase(".tiff")) {

            if (file.exists()) {
                if(mReturnIntent) {
                    returnIntentResults(file);

                } else {
                    Intent picIntent = new Intent();
                    picIntent.setAction(android.content.Intent.ACTION_VIEW);
                    picIntent.setDataAndType(Uri.fromFile(file), "image/*");
                    startActivity(picIntent);
                }
            }
        }

        /*video file selected--add more video formats*/
        else if(item_ext.equalsIgnoreCase(".m4v") || 
                item_ext.equalsIgnoreCase(".3gp") ||
                item_ext.equalsIgnoreCase(".wmv") || 
                item_ext.equalsIgnoreCase(".mp4") || 
                item_ext.equalsIgnoreCase(".ogg") ||
                item_ext.equalsIgnoreCase(".wav")) {

            if (file.exists()) {
                if(mReturnIntent) {
                    returnIntentResults(file);

                } else {
                    Intent movieIntent = new Intent();
                    movieIntent.setAction(android.content.Intent.ACTION_VIEW);
                    movieIntent.setDataAndType(Uri.fromFile(file), "video/*");
                    startActivity(movieIntent);
                }
            }
        }

        /*zip file */
        else if(item_ext.equalsIgnoreCase(".zip")) {

            if(mReturnIntent) {
                returnIntentResults(file);

            } else {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                AlertDialog alert;
                mZippedTarget = mFileMag.getCurrentDir() + "/" + item;
                CharSequence[] option = {"Extract here", "Extract to..."};

                builder.setTitle("Extract");
                builder.setItems(option, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        switch(which) {
                            case 0:
                                String dir = mFileMag.getCurrentDir();
                                mHandler.unZipFile(item, dir + "/");
                                break;

                            case 1:
                                mDetailLabel.setText("Holding " + item + 
                                                     " to extract");
                                mHoldingZip = true;
                                break;
                        }
                    }
                });

                alert = builder.create();
                alert.show();
            }
        }

        /* gzip files, this will be implemented later */
        else if(item_ext.equalsIgnoreCase(".gzip") ||
                item_ext.equalsIgnoreCase(".gz")) {

            if(mReturnIntent) {
                returnIntentResults(file);

            } else {
                //TODO:
            }
        }

        /*pdf file selected*/
        else if(item_ext.equalsIgnoreCase(".pdf")) {

            if(file.exists()) {
                if(mReturnIntent) {
                    returnIntentResults(file);

                } else {
                    Intent pdfIntent = new Intent();
                    pdfIntent.setAction(android.content.Intent.ACTION_VIEW);
                    pdfIntent.setDataAndType(Uri.fromFile(file), 
                                             "application/pdf");

                    try {
                        startActivity(pdfIntent);
                    } catch (ActivityNotFoundException e) {
                        Toast.makeText(this, "Sorry, couldn't find a pdf viewer", 
                                Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }

        /*Android application file*/
        else if(item_ext.equalsIgnoreCase(".apk")){

            if(file.exists()) {
                if(mReturnIntent) {
                    returnIntentResults(file);

                } else {
                    Intent apkIntent = new Intent();
                    apkIntent.setAction(android.content.Intent.ACTION_VIEW);
                    apkIntent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
                    startActivity(apkIntent);
                }
            }
        }

        /* HTML file */
        else if(item_ext.equalsIgnoreCase(".html")) {

            if(file.exists()) {
                if(mReturnIntent) {
                    returnIntentResults(file);

                } else {
                    Intent htmlIntent = new Intent();
                    htmlIntent.setAction(android.content.Intent.ACTION_VIEW);
                    htmlIntent.setDataAndType(Uri.fromFile(file), "text/html");

                    try {
                        startActivity(htmlIntent);
                    } catch(ActivityNotFoundException e) {
                        Toast.makeText(this, "Sorry, couldn't find a HTML viewer", 
                                            Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }

        /* text file*/
        else if(item_ext.equalsIgnoreCase(".txt")) {

            if(file.exists()) {
                if(mReturnIntent) {
                    returnIntentResults(file);

                } else {
                    Intent txtIntent = new Intent();
                    txtIntent.setAction(android.content.Intent.ACTION_VIEW);
                    txtIntent.setDataAndType(Uri.fromFile(file), "text/plain");

                    try {
                        startActivity(txtIntent);
                    } catch(ActivityNotFoundException e) {
                        txtIntent.setType("text/*");
                        startActivity(txtIntent);
                    }
                }
            }
        }

        /* generic intent */
        else {
            if(file.exists()) {
                if(mReturnIntent) {
                    returnIntentResults(file);

                } else {
                    Intent generic = new Intent();
                    generic.setAction(android.content.Intent.ACTION_VIEW);
                    generic.setDataAndType(Uri.fromFile(file), "text/plain");

                    try {
                        startActivity(generic);
                    } catch(ActivityNotFoundException e) {
                        Toast.makeText(this, "Sorry, couldn't find anything " +
                                       "to open " + file.getName(), 
                                       Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }

resultCode must equal RESULT_CANCELED because the only way out of that activity is pressing the back button on the phone this publishes a canceled result code not an ok result code

Manju

I think for that intent you are not setting the result like

youractivity.setResult(Activity.RESULT_OK)

in your case may be youractivity.setResult(RESULT_OK) before startactivityforresult(intent)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!