Custom Content Provider Problem

Put your problem here if it does not fit any of the other categories.

Custom Content Provider Problem

Postby DaKaron » Sun Feb 27, 2011 5:54 pm

hi all,

i'm tryng to open a pdf file stored in my assets folder with an external application using a custom content provider, so i've this content provider :
Code: Select all
package package.name;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URI;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;

public class FileContentProvider extends ContentProvider {
       private static final String URI_PREFIX = "content://package.name.filecontentprovider";

       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 {
           URI uri1 = URI.create("file:///data/data/package.name/"+uri.getPath());
           File file = new File(uri1.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");
       }

    }


and here how i call the opening of the file:

Code: Select all
      Intent i = new Intent();
      i.setDataAndType(Uri.parse("content://package.name/prova.pdf"), "application/pdf");
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        try {
            startActivity(i);
        }
        catch (ActivityNotFoundException e) {
            Toast.makeText(ctx,
                "No Application Available to View PDF",
                Toast.LENGTH_SHORT).show();
        }


i've added the following line in the android manifest, inside the tag:

Code: Select all
<provider android:name=".FileContentProvider" android:authorities="package.name"></provider>


but when i run on the device, the external application (adobe reader or thinkfree office) say: "impossible to open the file" (traduction from italian soi don't know if in english is exactely the same).

the logcat don't throw any message... don't know where is the problem :(

thanks all for any help!
DaKaron
Freshman
Freshman
 
Posts: 2
Joined: Sun Feb 27, 2011 5:44 pm

Top

Re: Custom Content Provider Problem

Postby DaKaron » Sun Feb 27, 2011 5:56 pm

little copy/paste problem in the code above, i've:

Code: Select all
URI uri1 = URI.create("file:///data/data/package.name/assets/"+uri.getPath());


instead of:

Code: Select all
URI uri1 = URI.create("file:///data/data/package.name/"+uri.getPath());
DaKaron
Freshman
Freshman
 
Posts: 2
Joined: Sun Feb 27, 2011 5:44 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 11 guests