jwBurnside
jwBurnside

Reputation: 867

Specify filename in ContentProvider

For file storage, I'm using a custom ContentProvider cobbled together from a few examples I found online. It seems to be working pretty well as long as I use one file name defined in the class itself (mImageName), but I need to be able to define the file name with a parameter outside of the class itself. Can someone help me with this? Here is a snippet of my ContentProvider class:

public class FileStorageContentProvider extends ContentProvider {

static final String AUTHORITY = "content://com.jwburnside.provider";
public static final Uri CONTENT_URI = Uri.parse(AUTHORITY);
private static final HashMap<String, String> MIME_TYPES = new HashMap<String, String>();

private String mImageName = "myImage";

static {
    MIME_TYPES.put(".jpg", "image/jpeg");
    MIME_TYPES.put(".jpeg", "image/jpeg");
}

public FileStorageContentProvider() {}

@Override
public boolean onCreate() {

    try {

        File mFile = new File(getContext().getFilesDir(),mImageName);
        if(!mFile.exists()) {
            mFile.createNewFile();
            getContext().getContentResolver().notifyChange(CONTENT_URI, null);
        }

        return (true);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

And in my activity:

mImageCaptureUri = FileStorageContentProvider.CONTENT_URI;

Upvotes: 0

Views: 463

Answers (1)

Adam Lopresto
Adam Lopresto

Reputation: 346

You'll usually specify things like that as part of the URI that gets passed to your methods. So you'd use something like

mImageCaptureUri = Uri.withAppendedPath(FileStorageContentProvider.CONTENT_URI, "myImage");

Then inside your various methods (like query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)) you can extract the filename from the URI with uri.getPath().

Upvotes: 1

Related Questions