1

我有一个要存储在自定义内容提供程序中的视频文件。在那之后,我想阅读它并想通过 Uri 播放它。

下面是我的代码。

package com.videocon.nds;

public class FileProvider extends ContentProvider {
    public static final Uri CONTENT_URI=Uri.parse("content://com.videocon.nds/");
    private static final HashMap<String, String> MIME_TYPES=new HashMap<String, String>();
   static {
    MIME_TYPES.put(".mp4", "video/mp4");
   }

@Override
public boolean onCreate() {
File f=new File(getContext().getFilesDir(), "Holy.mp4");

if (!f.exists()) {
    AssetManager assets=getContext().getResources().getAssets();

    try {
    copy(assets.open("Holy.mp4"), f);
    }
    catch (IOException e) {
    Log.e("FileProvider", "Exception copying from assets", e);

    return(false);
    }
}

return(true);
}

@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));
    }
}

return(null);
}

@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
    throws FileNotFoundException {
File f=new File(getContext().getFilesDir(), uri.getPath());

if (f.exists()) {
    return(ParcelFileDescriptor.open(f,
        ParcelFileDescriptor.MODE_READ_ONLY));
}

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");
}

static private void copy(InputStream in, File dst) throws IOException {
FileOutputStream out=new FileOutputStream(dst);
byte[] buf=new byte[1024];
int len;

while((len=in.read(buf))>0) {
    out.write(buf, 0, len);
}

in.close();
out.close();
}
}
4

0 回答 0