1. Check out our app, Forums for Android! Download and leave feedback here!

creating our own local content provider

Discussion in 'Application Development' started by murali.lancer, Feb 26, 2010.

  1. murali.lancer

    murali.lancer Member
    Thread Starter
    16

    Sep 17, 2009
    18
    1
    16
    Hi today, i am posting how to create a local content provider ,which is specific to our application .


    you should a class ContentProvider first .
    and u should override only one method ,i.e. public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException ,and u need not touch any other method.



    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 LocalFileContentProvider extends ContentProvider
    {

    @Override
    public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException
    {
    //here u should mention the path from where u want to get the data
    URI uri1 = URI.create("file:///data/data/package name /files"+uri.getPath());
    if u are requesting for file system .here the path what u are sending ,that here will replace(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 u should mention provider in the manifest file like

    <application android:icon="@drawable/sample_7"
    android:label="@string/app_name">
    <provider android:name=".LocalFileContentProvider"
    android:authorities="package name" />
    <activity android:name="klgjdslkj"
    android:label="n,bnsgdjm">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>
    it should be in this order only .



    And while calling the content provider u should call like this
    imageView.setImageURI("content://package name /the path to where u need to cal ");


    Enjoy this if it is useful

    Murali dhuli
    android developer
     

    Advertisement

    SotirisBK likes this.
  2. SotirisBK

    SotirisBK New Member
    5

    Mar 16, 2011
    1
    0
    5
    Hi murali,

    Very interesting post and almost does the work I want. For some reason when I try to get a video fileusing content://pkg/path-to-file.mp4 it does not seem to pass through the overriden "openFile" function. And of course the videoplayer says that cannot play video. The wierd part is that in emulator works perfectly. But in real device does not!
    Are there any ideas to resolve this issue? I lost another day today trying to figure this out!
     

Share This Page

Loading...