File Path not valid in android

You must Login before you can answer or comment on any questions.

Hi all ,

I am using 2.1.1 titanium sdk and 2.3.3 android sdk , I am trying to show the pdf . But its saying that file path not valid .. My pdf is located in the resource folder .

var f = Ti.Filesystem.getFile('file.pdf');
    Ti.Android.currentActivity.startActivity(Ti.Android.createIntent({
        action: Ti.Android.ACTION_VIEW,
        type: 'application/pdf',
        data: f.getNativePath()
    }));

— asked 10 months ago by mathew orleans
1 Comment
  • -1 because you aren't even trying to help yourself

    — commented 10 months ago by Stephen Feather

5 Answers

Seriously? You don't see the problem here?

var f = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, "file.pdf");

In android, you can't access a resource file from an external reader. So, you need to copy to a temporary folder.

This function will do the job for you

function openPdf(file){
 
    var appFilePath = Ti.Filesystem.resourcesDirectory + '/' + file;
    var appFile = Ti.Filesystem.getFile(appFilePath);
    var tmpFile = undefined,
        newPath = undefined;
 
    if (Ti.Filesystem.isExternalStoragePresent()) {
 
            tmpFile = Ti.Filesystem.createTempFile();
            newPath = tmpFile.getParent().nativePath +'/' + file;
            if(!Ti.Filesystem.getFile(newPath).exists()){
                tmpFile.move(newPath);
                tmpFile = Ti.Filesystem.getFile(newPath);
                tmpFile.write(appFile.read());
            }else{
                tmpFile.deleteFile();
                tmpFile = Ti.Filesystem.getFile(newPath);
            }
 
    } else {
        Ti.API.error('No external storage present');
    }
 
    if (tmpFile) {
        var intent = Ti.Android.createIntent({
            action: Ti.Android.ACTION_VIEW,
            type: "application/pdf",
            data: tmpFile.nativePath
        });
 
        try {
            Ti.Android.currentActivity.startActivity(intent);
        } catch(e) {
            Ti.API.debug(e);
            alert('PDF reader not found');
        }
        return tmpFile;
    }
 }

— answered 10 months ago by Javier Rayon
answer permalink
3 Comments
  • (original code written by Tony Lukasavage, but I couldn't find the original post in developer's blog)

    — commented 10 months ago by Javier Rayon

  • Nothing happened when i tried this code

    button.addEventListener('focus',function(){
        var file='file.pdf';
        openPdf(file);
    });
     
    function openPdf(file){
     
        var appFilePath = Ti.Filesystem.resourcesDirectory + '/' + file;
        var appFile = Ti.Filesystem.getFile(appFilePath);
        var tmpFile = undefined,
            newPath = undefined;
     
        if (Ti.Filesystem.isExternalStoragePresent()) {
     
                tmpFile = Ti.Filesystem.createTempFile();
                newPath = tmpFile.getParent().nativePath +'/' + file;
                if(!Ti.Filesystem.getFile(newPath).exists()){
                    tmpFile.move(newPath);
                    tmpFile = Ti.Filesystem.getFile(newPath);
                    tmpFile.write(appFile.read());
                }else{
                    tmpFile.deleteFile();
                    tmpFile = Ti.Filesystem.getFile(newPath);
                }
     
        } else {
            Ti.API.error('No external storage present');
        }
     
        if (tmpFile) {
            var intent = Ti.Android.createIntent({
                action: Ti.Android.ACTION_VIEW,
                type: "application/pdf",
                data: tmpFile.nativePath
            });
     
            try {
                Ti.Android.currentActivity.startActivity(intent);
            } catch(e) {
                Ti.API.debug(e);
                alert('PDF reader not found');
            }
            return tmpFile;
        }
     }

    — commented 10 months ago by mathew orleans

  • can you try this, instead of your event listener?

    button.addEventListener('click',function(){
        var filename='file.pdf';
     
        var file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, filename);
     
        if(file.exists()){  
            openPdf(file);
        }else{
            alert('File not found in this location');
        }
    });
    Please note that android emulator does not include any pdf reader, so you will have to test on device.

    — commented 10 months ago by Javier Rayon

We're having a similar experience. The intent runs/works fine on our old Motorola Bravo, but when running on our new Nexus we get a "The document path is not valid."

Strangest thing.

— answered 7 months ago by Boost Media
answer permalink
2 Comments
  • I'm also having problems getting the PDF intent to work on a Nexus 7 tablet. Did you get it to work? Please let me know...thanks!

    — commented 7 months ago by Tim Kimberley

  • Nope. No luck at it all. I had to come up with another, undesirable solution. Not sure what the problem here is...

    — commented 7 months ago by Boost Media

Your Answer

Think you can help? Login to answer this question!