Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7740855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:53:03+00:00 2026-06-01T08:53:03+00:00

I have the following code to launch a file : try { path =

  • 0

I have the following code to launch a file :

   try {
                    path = fileJsonObject.getString("filePath");
                     if (path.indexOf("/") == 0) {
                      path = path.substring(1, path.length()); 
                  }
                   path = root + path;
                   final File fileToOpen = new File(path);
                   if (fileToOpen.exists()) {
                      if (fileToOpen.isFile()) {
                             Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW);
                            myIntent.setData(Uri.parse(path));
                            final String pathToCheck = new String(path);
                            pathToCheck.toLowerCase();
                            if (pathToCheck.endsWith(".wav") || pathToCheck.endsWith(".ogg") || pathToCheck.endsWith(".mp3")
                                || pathToCheck.endsWith(".mid") || pathToCheck.endsWith(".midi") || pathToCheck.endsWith(".amr")) {
                            myIntent.setType("audio/*");
                        } else if (pathToCheck.endsWith(".mpg") || pathToCheck.endsWith(".mpeg") || pathToCheck.endsWith(".3gp")
                                || pathToCheck.endsWith(".mp4")) {
                            myIntent.setType("video/*");
                        } else if (pathToCheck.endsWith(".jpg") || pathToCheck.endsWith(".jpeg") || pathToCheck.endsWith(".gif")
                                || pathToCheck.endsWith(".png") || pathToCheck.endsWith(".bmp")) {
                            myIntent.setType("image/*");
                        } else if (pathToCheck.endsWith(".txt") || pathToCheck.endsWith(".csv") || pathToCheck.endsWith(".xml")) {
                            Log.i("txt","Text fileeeeeeeeeeeeeeeeeeeeeeeeee");
                            myIntent.setType("text/*");
                        } else if (pathToCheck.endsWith(".gz") || pathToCheck.endsWith(".rar") || pathToCheck.endsWith(".zip")) {
                            myIntent.setType("package/*");
                        } else if (pathToCheck.endsWith(".apk")) {
                            myIntent.setType("application/vnd.android.package-archive");
                        }
                             ((Activity) context).startActivityForResult(myIntent, RequestCodes.LAUNCH_FILE_CODE);
                    } else {
                        errUrl = resMsgHandler.errMsgResponse(fileJsonObject,
                        "Incorrect path provided. please give correct path of file");

                        return errUrl;
                    }
                } else {
                    errUrl = resMsgHandler.errMsgResponse(fileJsonObject,"Incorrect path provided. please give correct path of file");

                    return errUrl;
                }
            } catch (Exception e) {
                e.printStackTrace();
                Log.i("err","Unable to launch file" + " " + e.getMessage());
                errUrl = resMsgHandler.errMsgResponse(fileJsonObject,
                "Unable to launch file" + " " + e.getMessage());
                return errUrl;
                }  


 @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
       super.onActivityResult(requestCode, resultCode, data);
         try {

   if (requestCode == RequestCodes.LAUNCH_FILE_CODE) {
            if (resultCode == RESULT_CANCELED) {
                     Log.i("err","errrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");
                       String errUrl = responseMsgHandler.errMsgResponse(FileHandler.fileJsonObject, "Unable to launch file");
                mWebView.loadUrl(errUrl);
                } else if (resultCode == RESULT_OK) {
                String successUrl =         responseMsgHandler.launchfileResponse(FileHandler.fileJsonObject);
                mWebView.loadUrl(successUrl);
            }  

Amd the result ctrl is at “if (resultCode == RESULT_CANCELED)”. So how to successfully launch this?

May be in short i am doing this:

final File fileToOpen = new File(path);  
 if (fileToOpen.exists()) {  
 if (fileToOpen.isFile()) {  
Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW);  
myIntent.setData(Uri.parse(path));  

    if (pathToCheck.endsWith(".txt") || pathToCheck.endsWith(".csv") || pathToCheck.endsWith(".xml"))    {     
                                Log.i("txt","Text fileeeeeeeeeeeeeeeeeeeeeeeeee");  
                                    myIntent.setType("text/*");
  startActivityForResult(myIntent, RequestCodes.LAUNCH_FILE_CODE);  

and

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
         super.onActivityResult(requestCode, resultCode, data);
 if (requestCode == RequestCodes.LAUNCH_FILE_CODE) {  
                if (resultCode == RESULT_CANCELED) {  
                Log.i  ("err","errrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");
                   String errUrl = responseMsgHandler.errMsgResponse(FileHandler.fileJsonObject, "Unable to launch file");  
                    mWebView.loadUrl(errUrl);   
            }    else if (resultCode == RESULT_OK) {
                    String successUrl = responseMsgHandler.launchfileResponse(FileHandler.fileJsonObject);
                     mWebView.loadUrl(successUrl);  
                }  

My err log:

    04-04 12:33:08.635: ERROR/AndroidRuntime(3757): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cooliris.media/com.cooliris.media.Gallery}: java.lang.NullPointerException  

    04-04 12:33:08.635: ERROR/AndroidRuntime(3757):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)  
   04-04 12:33:08.635: ERROR/AndroidRuntime(3757):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)  
    04-04 12:33:08.635: ERROR/AndroidRuntime(3757):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)  
   04-04 12:33:08.635: ERROR/AndroidRuntime(3757):     at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
   04-04 12:33:08.635: ERROR/AndroidRuntime(3757):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)   
   04-04 12:33:08.635: ERROR/AndroidRuntime(3757):     at android.os.Handler.dispatchMessage(Handler.java:99)  
   04-04 12:33:08.635: ERROR/AndroidRuntime(3757):     at android.os.Looper.loop(Looper.java:123)  
   04-04 12:33:08.635: ERROR/AndroidRuntime(3757):     at android.app.ActivityThread.main(ActivityThread.java:4627)  
   04-04 12:33:08.635: ERROR/AndroidRuntime(3757):     at java.lang.reflect.Method.invokeNative(Native Method)  
    04-04 12:33:08.635: ERROR/AndroidRuntime(3757):     at java.lang.reflect.Method.invoke(Method.java:521)  
    04-04 12:33:08.635: ERROR/AndroidRuntime(3757):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)  
    04-04 12:33:08.635: ERROR/AndroidRuntime(3757):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629) 
    04-04 12:33:08.635: ERROR/AndroidRuntime(3757):     at dalvik.system.NativeStart.main(Native Method)
   04-04 12:33:08.635: ERROR/AndroidRuntime(3757): Caused by: java.lang.NullPointerException
    04-04 12:33:08.635: ERROR/AndroidRuntime(3757):     at com.cooliris.media.Gallery.onCreate(Gallery.java:305) 
    04-04 12:33:08.635: ERROR/AndroidRuntime(3757):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)  
    04-04 12:33:08.635: ERROR/AndroidRuntime(3757):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)  

Thanks
Sneha

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:53:04+00:00Added an answer on June 1, 2026 at 8:53 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to following code to launch and the audio recorder on Android: final
I have the following C# code to launch an outlook window. The one think
We have following code: try { // some code throwing MyException } catch (MyException
I have a .plist created at the application launch with the following code (MyAppTableViewController):
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following Code Block Which I tried to optimize in the Optimized section
I have following code in my application: // to set tip - photo in
I have following code in my Application. Comments in my code will specify My
I have following code in my application. [self.navigationController pushViewController:x animated:YES]; It will push a
I have following code class User attr_accessor :name end u = User.new u.name =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.