tags:

views:

15

answers:

0

I can't seem to get check if a file exists or not - it always catches, even if it does exists; what am I doing wrong? The file exists in file:///data/data/com.generic.name/files/testImageView.html

    private static String url = "/testImageView.html";
    private static String uri;

    @Override
    public void onCreate(Bundle icicle) {

        super.onCreate(icicle);
        setContentView(R.layout.main);

        this.localFileContentProvider = new LocalFileContentProvider();
        Uri uri = Uri.parse(this.localFileContentProvider.constructUri(url));

        TextView txtStatus=(TextView)findViewById(R.id.Application);
        try {

            this.localFileContentProvider.openFile(uri, null);
            txtStatus.setText("The file is found");


        } catch (FileNotFoundException e) {

            txtStatus.setText("The file is not found");
            e.printStackTrace();

        }

My LocalFileContentProvider class was copied from blogs; it System.outs the correct path of the file:

package.generic.name

import java.io.*;

import android.content.*;
import android.database.*;
import android.net.*;
import android.os.*;

public class LocalFileContentProvider extends ContentProvider {
   private static final String URI_PREFIX = "file:///data/data/com.generic.name/files";

   public static String constructUri(String url) {
       Uri uri = Uri.parse(url);
       return uri.isAbsolute() ? url : URI_PREFIX + url;
   }

   @Override
   public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {

       //for testing
       System.out.println(uri.toString());

       File file = new File(uri.getPath());
       ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
       return parcel;
   }

   @Override
   public boolean onCreate() {
       return true;
   }

   @Override
   public int delete(Uri uri, String s, String[] as) {
       throw new UnsupportedOperationException("Not supported by this provider");
   }

   @Override
   public String getType(Uri uri) {
       throw new UnsupportedOperationException("Not supported by this provider");
   }

   @Override
   public Uri insert(Uri uri, ContentValues contentvalues) {
       throw new UnsupportedOperationException("Not supported by this provider");
   }

   @Override
   public Cursor query(Uri uri, String[] as, String s, String[] as1, String s1) {
       throw new UnsupportedOperationException("Not supported by this provider");
   }

   @Override
   public int update(Uri uri, ContentValues contentvalues, String s, String[] as) {
       throw new UnsupportedOperationException("Not supported by this provider");
   }

}