Android 内容提供商的问题(视频和照片)
您好,我正在尝试使用Android 内容提供商的问题(视频和照片),android,android-contentprovider,Android,Android Contentprovider,您好,我正在尝试使用ContentProvider(视频和照片)保存不同的文件。但我不知道如何在同一个类中保存。 我试着这样做 public class MyFileContentProvider extends ContentProvider { public static final Uri CONTENT_URI = Uri.parse("content://com.renata.ideary/picture"); public static final Uri CONT
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");
}
}
公共类MyFileContentProvider扩展ContentProvider{
公共静态最终Uri内容\u Uri=Uri.parse(“content://com.renata.ideary/picture");
公共静态最终Uri内容\u Uri\u 2=Uri.parse(“content://com.renata.ideary/video");
私有静态最终HashMap MIME_TYPES=new HashMap();
私有静态最终HashMap MIME_TYPES_2=新HashMap();
静止的{
MIME_类型.put(“.jpg”,“image/jpeg”);
MIME_类型.put(“.jpeg”,“image/jpeg”);
MIME_类型_2.put(“.mp4”、“video/mp4”);
}
@凌驾
公共布尔onCreate(){
试一试{
File mFile=新文件(getContext().getFilesDir(),“/picture.jpg”);
如果(!mFile.exists()){
mFile.createNewFile();
}
File mFile2=新文件(getContext().getFilesDir(),“/video.mp4”);
如果(!mFile2.exists()){
mFile2.createNewFile();
}
getContext().getContentResolver().notifyChange(内容URI,null);
getContext().getContentResolver().notifyChange(CONTENT\u URI\u 2,null);
返回(真);
}捕获(例外e){
e、 printStackTrace();
返回false;
}
}
@凌驾
公共字符串getType(Uri){
字符串路径=uri.toString();
for(字符串扩展名:MIME_TYPES.keySet()){
if(路径endsWith(扩展)){
返回(MIME_TYPES.get(扩展));
}
}
for(字符串扩展名:MIME\u TYPES\u 2.keySet()){
if(路径endsWith(扩展)){
返回(MIME_TYPES_2.get(扩展));
}
}
返回(空);
}
@凌驾
公共ParcelFileDescriptor openFile(Uri,字符串模式)
抛出FileNotFoundException{
if(uri.equals(CONTENT_uri)){
文件f=新文件(getContext().getFilesDir(),“/picture.jpg”);
如果(f.exists()){
返回(ParcelFileDescriptor.open)(f,
ParcelFileDescriptor.MODE_READ_WRITE));
}
抛出新的FileNotFoundException(uri.getPath());
}
否则{
文件f2=新文件(getContext().getFilesDir(),“/video.mp4”);
如果(f2.exists()){
返回(ParcelFileDescriptor.open)(f2,
ParcelFileDescriptor.MODE_READ_WRITE));
}
抛出新的FileNotFoundException(uri.getPath());
}
}
@凌驾
公共游标查询(Uri url、字符串[]投影、字符串选择、,
字符串[]selectionArgs,字符串排序){
抛出新的RuntimeException(“不支持操作”);
}
@凌驾
公共Uri插入(Uri、ContentValues和initialValues){
抛出新的RuntimeException(“不支持操作”);
}
@凌驾
公共int更新(Uri、ContentValues、字符串,其中,
字符串[](RGS){
抛出新的RuntimeException(“不支持操作”);
}
@凌驾
public int delete(Uri、字符串where、字符串[]wherergs){
抛出新的RuntimeException(“不支持操作”);
}
}
但总是告诉我同样的错误:
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:保存我的视频,但出现以下错误:(