murali.lancer
Newbie
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
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