下面列出了android.content.res.AssetFileDescriptor#getStartOffset() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void findAPKFile(String filepath, Context context) {
String apkFilepath = getAPKFilepath(context);
// Get the offset and length for the file: theUrl, that is in your
// assets folder
AssetManager assetManager = context.getAssets();
try {
AssetFileDescriptor assFD = assetManager.openFd(filepath);
if (assFD != null) {
long offset = assFD.getStartOffset();
long fileSize = assFD.getLength();
assFD.close();
// **** offset and fileSize are the offset and size
// **** in bytes of the asset inside the APK
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static AudioBuffer loadBuffer(AssetInfo assetInfo) throws IOException {
AndroidAssetInfo aai = (AndroidAssetInfo) assetInfo;
AssetFileDescriptor afd = null;
NativeVorbisFile file = null;
try {
afd = aai.openFileDescriptor();
int fd = afd.getParcelFileDescriptor().getFd();
file = new NativeVorbisFile(fd, afd.getStartOffset(), afd.getLength());
ByteBuffer data = BufferUtils.createByteBuffer(file.totalBytes);
file.readFully(data);
AudioBuffer ab = new AudioBuffer();
ab.setupFormat(file.channels, 16, file.sampleRate);
ab.updateData(data);
return ab;
} finally {
if (file != null) {
file.close();
}
if (afd != null) {
afd.close();
}
}
}
@CalledByNative
public static long[] open(String fileName) {
AssetFileDescriptor afd = null;
try {
AssetManager manager = ContextUtils.getApplicationContext().getAssets();
afd = manager.openNonAssetFd(fileName);
return new long[] {afd.getParcelFileDescriptor().detachFd(), afd.getStartOffset(),
afd.getLength()};
} catch (IOException e) {
// As a general rule there's no point logging here because the caller should handle
// receiving an fd of -1 sensibly, and the log message is either mirrored later, or
// unwanted (in the case where a missing file is expected), or wanted but will be
// ignored, as most non-fatal logs are.
// It makes sense to log here when the file exists, but is unable to be opened as an fd
// because (for example) it is unexpectedly compressed in an apk. In that case, the log
// message might save someone some time working out what has gone wrong.
// For that reason, we only suppress the message when the exception message doesn't look
// informative (Android framework passes the filename as the message on actual file not
// found, and the empty string also wouldn't give any useful information for debugging).
if (!e.getMessage().equals("") && !e.getMessage().equals(fileName)) {
Log.e(LOGTAG, "Error while loading asset " + fileName + ": " + e);
}
return new long[] {-1, -1, -1};
} finally {
try {
if (afd != null) {
afd.close();
}
} catch (IOException e2) {
Log.e(LOGTAG, "Unable to close AssetFileDescriptor", e2);
}
}
}
protected MappedByteBuffer loadModelFile(AssetManager assetManager, String modelPath) throws IOException {
AssetFileDescriptor fileDescriptor = assetManager.openFd(modelPath);
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
FileChannel fileChannel = inputStream.getChannel();
long startOffset = fileDescriptor.getStartOffset();
long declaredLength = fileDescriptor.getDeclaredLength();
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
}
private static AudioStream loadStream(AssetInfo assetInfo) throws IOException {
AndroidAssetInfo aai = (AndroidAssetInfo) assetInfo;
AssetFileDescriptor afd = null;
NativeVorbisFile file = null;
boolean success = false;
try {
afd = aai.openFileDescriptor();
int fd = afd.getParcelFileDescriptor().getFd();
file = new NativeVorbisFile(fd, afd.getStartOffset(), afd.getLength());
AudioStream stream = new AudioStream();
stream.setupFormat(file.channels, 16, file.sampleRate);
stream.updateData(new VorbisInputStream(afd, file), file.duration);
success = true;
return stream;
} finally {
if (!success) {
if (file != null) {
file.close();
}
if (afd != null) {
afd.close();
}
}
}
}
/** Memory-map the model file in Assets. */
private static MappedByteBuffer loadModelFile(AssetManager assets, String modelFilename)
throws IOException {
AssetFileDescriptor fileDescriptor = assets.openFd(modelFilename);
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
FileChannel fileChannel = inputStream.getChannel();
long startOffset = fileDescriptor.getStartOffset();
long declaredLength = fileDescriptor.getDeclaredLength();
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
}
private static MappedByteBuffer loadModelFile(Context context, String modelFile) {
if (context == null
|| TextUtils.isEmpty(modelFile)) {
return null;
}
MappedByteBuffer buffer = null;
try {
AssetFileDescriptor df = context.getAssets().openFd(modelFile);
FileInputStream inputStream = new FileInputStream(df.getFileDescriptor());
FileChannel fileChannel = inputStream.getChannel();
long startOffset = df.getStartOffset();
long declaredLength = df.getDeclaredLength();
buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
} catch (IOException e) {
Logger.debug("load tflite model from [%s] failed: %s",
modelFile,
e.toString());
buffer = null;
}
return buffer;
}
/** Memory-map the model file in Assets. */
private MappedByteBuffer loadModelFile(Activity activity) throws IOException {
AssetFileDescriptor fileDescriptor = activity.getAssets().openFd(getModelPath());
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
FileChannel fileChannel = inputStream.getChannel();
long startOffset = fileDescriptor.getStartOffset();
long declaredLength = fileDescriptor.getDeclaredLength();
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
}
/** Memory-map the model file in Assets. */
private MappedByteBuffer loadModelFile(Activity activity) throws IOException {
AssetFileDescriptor fileDescriptor = activity.getAssets().openFd(MODEL_PATH);
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
FileChannel fileChannel = inputStream.getChannel();
long startOffset = fileDescriptor.getStartOffset();
long declaredLength = fileDescriptor.getDeclaredLength();
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
}
/** Memory-map the model file in Assets. */
private static MappedByteBuffer loadModelFile(AssetManager assets, String modelFilename)
throws IOException {
AssetFileDescriptor fileDescriptor = assets.openFd(modelFilename);
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
FileChannel fileChannel = inputStream.getChannel();
long startOffset = fileDescriptor.getStartOffset();
long declaredLength = fileDescriptor.getDeclaredLength();
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
}
private void setDataSourceInternalContentUri(Context context, Uri uri)
throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
final ContentResolver cr = context.getContentResolver();
AssetFileDescriptor afd = cr.openAssetFileDescriptor(uri, "r");
FileDescriptor fd = afd.getFileDescriptor();
final int nativeFD;
try {
nativeFD = checkAndObtainNativeFileDescriptor(fd);
} catch (IllegalArgumentException e) {
closeQuietly(afd);
throw e;
}
final int result;
final long declLength = afd.getDeclaredLength();
final long startOffset = afd.getStartOffset();
if (declLength < 0) {
result = setDataSourceFdImplNative(mNativeHandle, nativeFD);
} else {
result = setDataSourceFdImplNative(
mNativeHandle, nativeFD, startOffset, declLength);
}
parseResultAndThrowException(result);
mContentAssetFd = afd;
}
private MappedByteBuffer loadModelFile(AssetManager assetManager, String modelPath) throws IOException {
AssetFileDescriptor fileDescriptor = assetManager.openFd(modelPath);
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
FileChannel fileChannel = inputStream.getChannel();
long startOffset = fileDescriptor.getStartOffset();
long declaredLength = fileDescriptor.getDeclaredLength();
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
}
/**
* Create a Media from libVLC and an AssetFileDescriptor
*
* @param libVLC a valid LibVLC
* @param afd asset file descriptor object
*/
public Media(LibVLC libVLC, AssetFileDescriptor afd) {
super(libVLC);
long offset = afd.getStartOffset();
long length = afd.getLength();
nativeNewFromFdWithOffsetLength(libVLC, afd.getFileDescriptor(), offset, length);
mUri = VLCUtil.UriFromMrl(nativeGetMrl());
}
/**
* Memory-map the model file in Assets.
*/
public static MappedByteBuffer loadModelFile(Context context, String modelFile)
throws IOException {
AssetFileDescriptor fileDescriptor = context.getAssets().openFd(modelFile);
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
FileChannel fileChannel = inputStream.getChannel();
long startOffset = fileDescriptor.getStartOffset();
long declaredLength = fileDescriptor.getDeclaredLength();
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
}
/**
* Memory-map the model file in Assets.
*/
public static MappedByteBuffer loadModelFile(Context context, String modelFile)
throws IOException {
AssetFileDescriptor fileDescriptor = context.getAssets().openFd(modelFile);
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
FileChannel fileChannel = inputStream.getChannel();
long startOffset = fileDescriptor.getStartOffset();
long declaredLength = fileDescriptor.getDeclaredLength();
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
}
/**
* Memory-map the model file in Assets.
*/
public static MappedByteBuffer loadModelFile(Context context, String modelFile)
throws IOException {
AssetFileDescriptor fileDescriptor = context.getAssets().openFd(modelFile);
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
FileChannel fileChannel = inputStream.getChannel();
long startOffset = fileDescriptor.getStartOffset();
long declaredLength = fileDescriptor.getDeclaredLength();
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
}
protected MappedByteBuffer loadMappedFile(String filePath) throws IOException {
AssetFileDescriptor fileDescriptor = assetManager.openFd(this.directoryName + "/" + filePath);
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
FileChannel fileChannel = inputStream.getChannel();
long startOffset = fileDescriptor.getStartOffset();
long declaredLength = fileDescriptor.getDeclaredLength();
return fileChannel.map(MapMode.READ_ONLY, startOffset, declaredLength);
}
/** Memory-map the model file in Assets. */
private static MappedByteBuffer loadModelFile(AssetManager assets, String modelFilename)
throws IOException {
AssetFileDescriptor fileDescriptor = assets.openFd(modelFilename);
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
FileChannel fileChannel = inputStream.getChannel();
long startOffset = fileDescriptor.getStartOffset();
long declaredLength = fileDescriptor.getDeclaredLength();
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
}
@Override
public long open(DataSpec dataSpec) throws ContentDataSourceException {
try {
Uri uri = dataSpec.uri;
this.uri = uri;
transferInitializing(dataSpec);
AssetFileDescriptor assetFileDescriptor = resolver.openAssetFileDescriptor(uri, "r");
this.assetFileDescriptor = assetFileDescriptor;
if (assetFileDescriptor == null) {
throw new FileNotFoundException("Could not open file descriptor for: " + uri);
}
FileInputStream inputStream = new FileInputStream(assetFileDescriptor.getFileDescriptor());
this.inputStream = inputStream;
long assetStartOffset = assetFileDescriptor.getStartOffset();
long skipped = inputStream.skip(assetStartOffset + dataSpec.position) - assetStartOffset;
if (skipped != dataSpec.position) {
// We expect the skip to be satisfied in full. If it isn't then we're probably trying to
// skip beyond the end of the data.
throw new EOFException();
}
if (dataSpec.length != C.LENGTH_UNSET) {
bytesRemaining = dataSpec.length;
} else {
long assetFileDescriptorLength = assetFileDescriptor.getLength();
if (assetFileDescriptorLength == AssetFileDescriptor.UNKNOWN_LENGTH) {
// The asset must extend to the end of the file. If FileInputStream.getChannel().size()
// returns 0 then the remaining length cannot be determined.
FileChannel channel = inputStream.getChannel();
long channelSize = channel.size();
bytesRemaining = channelSize == 0 ? C.LENGTH_UNSET : channelSize - channel.position();
} else {
bytesRemaining = assetFileDescriptorLength - skipped;
}
}
} catch (IOException e) {
throw new ContentDataSourceException(e);
}
opened = true;
transferStarted(dataSpec);
return bytesRemaining;
}
/**
* Loads the audio or video specified by mediaPath into the given
* MediaPlayer.
*
* @param mediaPlayer the MediaPlayer
* @param form the Form
* @param mediaPath the path to the media
*/
public static void loadMediaPlayer(MediaPlayer mediaPlayer, Form form, String mediaPath)
throws IOException {
MediaSource mediaSource = determineMediaSource(form, mediaPath);
switch (mediaSource) {
case ASSET:
AssetFileDescriptor afd = getAssetsIgnoreCaseAfd(form,mediaPath);
try {
FileDescriptor fd = afd.getFileDescriptor();
long offset = afd.getStartOffset();
long length = afd.getLength();
mediaPlayer.setDataSource(fd, offset, length);
} finally {
afd.close();
}
return;
case REPL_ASSET:
form.assertPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
mediaPlayer.setDataSource(replAssetPath(mediaPath));
return;
case SDCARD:
form.assertPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
mediaPlayer.setDataSource(mediaPath);
return;
case FILE_URL:
if (isExternalFileUrl(mediaPath)) {
form.assertPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
}
mediaPlayer.setDataSource(fileUrlToFilePath(mediaPath));
return;
case URL:
// This works both for streaming and non-streaming.
// TODO(halabelson): Think about whether we could get improved
// performance if we did buffering control.
mediaPlayer.setDataSource(mediaPath);
return;
case CONTENT_URI:
mediaPlayer.setDataSource(form, Uri.parse(mediaPath));
return;
case CONTACT_URI:
throw new IOException("Unable to load audio or video for contact " + mediaPath + ".");
}
throw new IOException("Unable to load audio or video " + mediaPath + ".");
}