public static final android.content.Intent | addDrmFile(android.content.ContentResolver cr, java.io.File file, java.lang.String title)Utility function for inserting a file into the DRM content provider.
FileInputStream fis = null;
OutputStream os = null;
Intent result = null;
try {
fis = new FileInputStream(file);
DrmRawContent content = new DrmRawContent(fis, (int) file.length(),
DrmRawContent.DRM_MIMETYPE_MESSAGE_STRING);
String mimeType = content.getContentType();
DrmRightsManager manager = manager = DrmRightsManager.getInstance();
DrmRights rights = manager.queryRights(content);
InputStream stream = content.getContentInputStream(rights);
long size = stream.available();
Uri contentUri = null;
if (mimeType.startsWith("audio/")) {
contentUri = DrmStore.Audio.CONTENT_URI;
} else if (mimeType.startsWith("image/")) {
contentUri = DrmStore.Images.CONTENT_URI;
} else {
Log.w(TAG, "unsupported mime type " + mimeType);
}
if (contentUri != null) {
ContentValues values = new ContentValues(3);
// compute title from file name, if it is not specified
if (title == null) {
title = file.getName();
int lastDot = title.lastIndexOf('.");
if (lastDot > 0) {
title = title.substring(0, lastDot);
}
}
values.put(DrmStore.Columns.TITLE, title);
values.put(DrmStore.Columns.SIZE, size);
values.put(DrmStore.Columns.MIME_TYPE, mimeType);
Uri uri = cr.insert(contentUri, values);
if (uri != null) {
os = cr.openOutputStream(uri);
byte[] buffer = new byte[1000];
int count;
while ((count = stream.read(buffer)) != -1) {
os.write(buffer, 0, count);
}
result = new Intent();
result.setDataAndType(uri, mimeType);
}
}
} catch (Exception e) {
Log.e(TAG, "pushing file failed", e);
} finally {
try {
if (fis != null)
fis.close();
if (os != null)
os.close();
} catch (IOException e) {
Log.e(TAG, "IOException in DrmTest.onCreate()", e);
}
}
return result;
|