您好我尝试使用ContentProvider
(视频和照片)保存不同的文件。但我不知道如何在同一个班级中执行此操作。
我试着这样做......
public class MyFileContentProvider extends ContentProvider {
public static final Uri CONTENT_URI = Uri.parse("content://com.renata.ideary/picture");
public static final Uri CONTENT_URI_2 = Uri.parse("content://com.renata.ideary/video");
private static final HashMap<String, String> MIME_TYPES = new HashMap<String, String>();
private static final HashMap<String, String> MIME_TYPES_2= new HashMap<String, String>();
static {
MIME_TYPES.put(".jpg", "image/jpeg");
MIME_TYPES.put(".jpeg", "image/jpeg");
MIME_TYPES_2.put(".mp4","video/mp4");
}
@Override
public boolean onCreate() {
try {
File mFile = new File(getContext().getFilesDir(), "/picture.jpg");
if(!mFile.exists()) {
mFile.createNewFile();
}
File mFile2 = new File(getContext().getFilesDir(), "/video.mp4");
if(!mFile2.exists()) {
mFile2.createNewFile();
}
getContext().getContentResolver().notifyChange(CONTENT_URI, null);
getContext().getContentResolver().notifyChange(CONTENT_URI_2, null);
return (true);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
@Override
public String getType(Uri uri) {
String path = uri.toString();
for (String extension : MIME_TYPES.keySet()) {
if (path.endsWith(extension)) {
return (MIME_TYPES.get(extension));
}
}
for (String extension : MIME_TYPES_2.keySet()) {
if (path.endsWith(extension)) {
return (MIME_TYPES_2.get(extension));
}
}
return (null);
}
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
throws FileNotFoundException {
if (uri.equals(CONTENT_URI)) {
File f = new File(getContext().getFilesDir(), "/picture.jpg");
if (f.exists()) {
return (ParcelFileDescriptor.open(f,
ParcelFileDescriptor.MODE_READ_WRITE));
}
throw new FileNotFoundException(uri.getPath());
}
else {
File f2 = new File(getContext().getFilesDir(), "/video.mp4");
if (f2.exists()) {
return (ParcelFileDescriptor.open(f2,
ParcelFileDescriptor.MODE_READ_WRITE));
}
throw new FileNotFoundException(uri.getPath());
}
}
@Override
public Cursor query(Uri url, String[] projection, String selection,
String[] selectionArgs, String sort) {
throw new RuntimeException("Operation not supported");
}
@Override
public Uri insert(Uri uri, ContentValues initialValues) {
throw new RuntimeException("Operation not supported");
}
@Override
public int update(Uri uri, ContentValues values, String where,
String[] whereArgs) {
throw new RuntimeException("Operation not supported");
}
@Override
public int delete(Uri uri, String where, String[] whereArgs) {
throw new RuntimeException("Operation not supported");
}
}
但总是告诉我同样的错误:
有人可以帮帮我吗?感谢05-06 22:42:37.932 21837-21849 / com.renata.ideary E / JavaBinder:***未捕获的远程异常! (进程之间尚不支持例外。) java.lang.RuntimeException:不支持操作 在com.renata.ideary.MyFileContentProvider.query(MyFileContentProvider.java:137) 在android.content.ContentProvider.query(ContentProvider.java:980) 在android.content.ContentProvider $ Transport.query(ContentProvider.java:213) 在android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112) 在android.os.Binder.execTransact(Binder.java:446)
PD:保存我的视频,但出现了这个错误:(