android.content.res.AssetManager#openFd()源码实例Demo

下面列出了android.content.res.AssetManager#openFd() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: VIA-AI   文件: Helper.java
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();
    }
}
 
源代码2 项目: dcs-sdk-java   文件: MediaPlayerImpl.java
private void playAsset(String resName) {
    LogUtil.d(TAG, "playAsset:" + resName);
    try {
        AssetManager am = context.getAssets();
        AssetFileDescriptor afd = am.openFd(resName);
        mMediaPlayer.reset();
        mMediaPlayer.setDataSource(afd.getFileDescriptor(),
                afd.getStartOffset(), afd.getLength());
        mMediaPlayer.prepareAsync();
        mCurrentState = IMediaPlayer.PlayState.PREPARING;
    } catch (IOException e) {
        e.printStackTrace();
        LogUtil.d(TAG, "playAsset", e);
        mCurrentState = IMediaPlayer.PlayState.ERROR;
        fireOnError("IOException play playAsset",
                IMediaPlayer.ErrorType.MEDIA_ERROR_INTERNAL_DEVICE_ERROR);
    }
}
 
源代码3 项目: LiveWallPaper   文件: VideoLiveWallpaper.java
@Override
public void onSurfaceCreated(SurfaceHolder holder) {
    L.d("VideoEngine#onSurfaceCreated ");
    super.onSurfaceCreated(holder);
    mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setSurface(holder.getSurface());
    try {
        AssetManager assetMg = getApplicationContext().getAssets();
        AssetFileDescriptor fileDescriptor = assetMg.openFd("test1.mp4");
        mMediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),
                fileDescriptor.getStartOffset(), fileDescriptor.getLength());
        mMediaPlayer.setLooping(true);
        mMediaPlayer.setVolume(0, 0);
        mMediaPlayer.prepare();
        mMediaPlayer.start();

    } catch (IOException e) {
        e.printStackTrace();
    }

}
 
源代码4 项目: LiveWallPaper   文件: DynamicWallPaper.java
private void initMediaPlayer(SurfaceHolder holder){
    mediaPlayer = new MediaPlayer();
    try {
        AssetManager assetMg = getApplicationContext().getAssets();
        AssetFileDescriptor fileDescriptor = assetMg.openFd(此处资源asset请从鸿洋大神那获取);
        mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),
                fileDescriptor.getStartOffset(), fileDescriptor.getLength());
        mediaPlayer.setDisplay(holder);
        mediaPlayer.prepare();
        mediaPlayer.setLooping(true);
        mediaPlayer.setVolume(0, 0);
        mediaPlayer.prepare();
    }catch (Exception e){
        e.printStackTrace();
    }

}
 
源代码5 项目: LLApp   文件: VideoLiveWallpaper.java
@Override
public void onSurfaceCreated(SurfaceHolder holder) {
    LogUtils.d(TAG,"VideoEngine#onSurfaceCreated ");
    super.onSurfaceCreated(holder);
    mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setSurface(holder.getSurface());
    try {
        AssetManager assetMg = getApplicationContext().getAssets();
        AssetFileDescriptor fileDescriptor = assetMg.openFd("test1.mp4");
        mMediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),
                fileDescriptor.getStartOffset(), fileDescriptor.getLength());
        mMediaPlayer.setLooping(true);
        mMediaPlayer.setVolume(0, 0);
        mMediaPlayer.prepare();
        mMediaPlayer.start();

    } catch (IOException e) {
        e.printStackTrace();
    }

}
 
@Override
public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
    // The asset file name should be the last path segment
    final String assetName = uri.getLastPathSegment();

    // If the given asset name is empty, throw an exception
    if (TextUtils.isEmpty(assetName)) {
        throw new FileNotFoundException();
    }

    try {
        // Try and return a file descriptor for the given asset name
        AssetManager am = getContext().getAssets();
        return am.openFd(assetName);
    } catch (IOException e) {
        e.printStackTrace();
        return super.openAssetFile(uri, mode);
    }
}
 
/**
 * 加载模型文件
 * @param assetManager
 * @param modelPath
 * @return
 * @throws IOException
 */
public static 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 AssetFileDescriptor fetchAssetFile(@NonNull final Uri uri, @NonNull final AssetManager am,
        @NonNull final String fileName, @NonNull final String identifier) {
    try {
        return am.openFd(contentPath + identifier + "/" + fileName);
    } catch (final IOException e) {
        Log.e(Objects.requireNonNull(getContext()).getPackageName(),
                "IOException when getting asset file, uri:" + uri, e);
        return null;
    }
}
 
源代码9 项目: yolov3-android-tflite   文件: Classifier.java
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);
}
 
源代码10 项目: tflite-android-transformers   文件: QaClient.java
/** Load tflite model from assets. */
public MappedByteBuffer loadModelFile(AssetManager assetManager) throws IOException {
  try (AssetFileDescriptor fileDescriptor = assetManager.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);
  }
}
 
源代码11 项目: ml   文件: TFLiteObjectDetectionAPIModel.java
/** 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 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);
}
 
源代码13 项目: PHONK   文件: TFLiteObjectDetectionAPIModel.java
/** 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);
}
 
@Nullable
@Override
public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
    AssetManager assetManager = getContext().getAssets();
    try {
        return assetManager.openFd("sample.txt");
    } catch (IOException e) {
        throw new FileNotFoundException("Error: " + e.getMessage());
    }
}
 
源代码15 项目: FimiX8-RE   文件: TFLiteObjectDetectionAPIModel.java
private static MappedByteBuffer loadModelFile(AssetManager assets, String modelFilename) throws IOException {
    AssetFileDescriptor fileDescriptor = assets.openFd(modelFilename);
    return new FileInputStream(fileDescriptor.getFileDescriptor()).getChannel().map(MapMode.READ_ONLY, fileDescriptor.getStartOffset(), fileDescriptor.getDeclaredLength());
}
 
源代码16 项目: sketch   文件: GifAnimationMetaData.java
/**
 * Retrieves metadata from asset.
 *
 * @param assets    AssetManager to read from
 * @param assetName name of the asset
 * @throws IOException          when opening failed
 * @throws NullPointerException if assets or assetName is null
 */
public GifAnimationMetaData(@NonNull AssetManager assets, @NonNull String assetName) throws IOException {
	this(assets.openFd(assetName));
}
 
源代码17 项目: sketch   文件: GifDrawable.java
/**
 * Creates drawable from asset.
 *
 * @param assets    AssetManager to read from
 * @param assetName name of the asset
 * @throws IOException          when opening failed
 * @throws NullPointerException if assets or assetName is null
 */
public GifDrawable(@NonNull AssetManager assets, @NonNull String assetName) throws IOException {
	this(assets.openFd(assetName));
}
 
源代码18 项目: meatspace-android   文件: GifDrawable.java
/**
 * Creates drawable from asset.
 * @param assets AssetManager to read from
 * @param assetName name of the asset
 * @throws java.io.IOException when opening failed
 * @throws NullPointerException if assets or assetName is null
 */
public GifDrawable ( AssetManager assets, String assetName ) throws IOException
{
	this( assets.openFd( assetName ) );
}
 
/**
 * Retrieves metadata from asset.
 *
 * @param assets    AssetManager to read from
 * @param assetName name of the asset
 * @throws IOException          when opening failed
 * @throws NullPointerException if assets or assetName is null
 */
public GifAnimationMetaData(@NonNull AssetManager assets, @NonNull String assetName) throws IOException {
    this(assets.openFd(assetName));
}
 
/**
 * Creates drawable from asset.
 *
 * @param assets    AssetManager to read from
 * @param assetName name of the asset
 * @throws IOException          when opening failed
 * @throws NullPointerException if assets or assetName is null
 */
public GifDrawable(@NonNull AssetManager assets, @NonNull String assetName) throws IOException {
    this(assets.openFd(assetName));
}