Launching a file using ACTION_VIEW Intent Action

Posted by Sneha on Stack Overflow See other posts from Stack Overflow or by Sneha
Published on 2012-04-04T04:47:01Z Indexed on 2012/04/04 5:29 UTC
Read the original article Hit count: 246

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 10:53:57.077: ERROR/AndroidRuntime(6861): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tf.thinkdroid.sstablet/com.tf.thinkdroid.write.editor.WriteEditorActivity}: java.lang.NullPointerException  
04-04 10:53:57.077: ERROR/AndroidRuntime(6861):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)  
04-04 10:53:57.077: ERROR/AndroidRuntime(6861):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)

.....

 04-04 10:53:57.077: ERROR/AndroidRuntime(6861): Caused by: java.lang.NullPointerException  
04-04 10:53:57.077: ERROR/AndroidRuntime(6861):     at com.tf.thinkdroid.common.app.TFActivity.storeDataToFileIfNecessary(Unknown Source)  
04-04 10:53:57.077: ERROR/AndroidRuntime(6861):     at com.tf.thinkdroid.common.app.TFActivity.onPostCreate(Unknown Source)  

...

Thanks
Sneha

© Stack Overflow or respective owner

Related posts about android

Related posts about android-intent