android.content.res.AssetFileDescriptor#getDeclaredLength()源码实例Demo

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

源代码1 项目: Zom-Android-XMPP   文件: SystemServices.java
public static FileInfo getContactAsVCardFile(Context context, Uri uri) {
    AssetFileDescriptor fd;
    try {
        fd = context.getContentResolver().openAssetFileDescriptor(uri, "r");
        java.io.FileInputStream in = fd.createInputStream();
        byte[] buf = new byte[(int) fd.getDeclaredLength()];
        in.read(buf);
        in.close();
        String vCardText = new String(buf);
        Log.d("Vcard", vCardText);
        List<String> pathSegments = uri.getPathSegments();
        String targetPath = "/" + pathSegments.get(pathSegments.size() - 1) + ".vcf";
        SecureMediaStore.copyToVfs(buf, targetPath);
        FileInfo info = new FileInfo();
        info.stream = new info.guardianproject.iocipher.FileInputStream(SecureMediaStore.vfsUri(targetPath).getPath());
        info.type = "text/vcard";
        return info;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
private byte[] loadBytes() {
  try {
    AssetFileDescriptor assetFileDescriptor = getAssets().openFd("lite_template/card.wasm");
    long len = assetFileDescriptor.getDeclaredLength();
    ByteBuffer buf = ByteBuffer.allocate((int) len);
    InputStream json = assetFileDescriptor.createInputStream();
    json.read(buf.array());
    json.close();
    return buf.array();
  } catch (IOException e) {
    e.printStackTrace();
  }
  return null;
}
 
/**
 * 加载模型文件
 * @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);
}
 
源代码4 项目: 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);
}
 
源代码5 项目: next18-ai-in-motion   文件: TfLiteCommander.java
/** Memory-map the model file in Assets. */
public MappedByteBuffer loadModelFile() throws IOException {
    AssetFileDescriptor fileDescriptor = ASSET_MANAGER.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);
}
 
源代码7 项目: 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);
}
 
源代码8 项目: ml   文件: DeepLabLite.java
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 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);
}
 
/**
 * 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);
}
 
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;
}
 
/**
 * 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);
}
 
源代码17 项目: PHONK   文件: AssetModelLoader.java
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);
}
 
源代码18 项目: 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);
}
 
源代码19 项目: AndroidComponentPlugin   文件: ContentResolver.java
/**
 * Open a raw file descriptor to access data under a URI.  This
 * is like {@link #openAssetFileDescriptor(Uri, String)}, but uses the
 * underlying {@link ContentProvider#openFile}
 * ContentProvider.openFile()} method, so will <em>not</em> work with
 * providers that return sub-sections of files.  If at all possible,
 * you should use {@link #openAssetFileDescriptor(Uri, String)}.  You
 * will receive a FileNotFoundException exception if the provider returns a
 * sub-section of a file.
 *
 * <h5>Accepts the following URI schemes:</h5>
 * <ul>
 * <li>content ({@link #SCHEME_CONTENT})</li>
 * <li>file ({@link #SCHEME_FILE})</li>
 * </ul>
 *
 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
 * on these schemes.
 * <p>
 * If opening with the exclusive "r" or "w" modes, the returned
 * ParcelFileDescriptor could be a pipe or socket pair to enable streaming
 * of data. Opening with the "rw" mode implies a file on disk that supports
 * seeking. If possible, always use an exclusive mode to give the underlying
 * {@link ContentProvider} the most flexibility.
 * <p>
 * If you are writing a file, and need to communicate an error to the
 * provider, use {@link ParcelFileDescriptor#closeWithError(String)}.
 *
 * @param uri The desired URI to open.
 * @param mode The file mode to use, as per {@link ContentProvider#openFile
 * ContentProvider.openFile}.
 * @param cancellationSignal A signal to cancel the operation in progress,
 *         or null if none. If the operation is canceled, then
 *         {@link OperationCanceledException} will be thrown.
 * @return Returns a new ParcelFileDescriptor pointing to the file.  You
 * own this descriptor and are responsible for closing it when done.
 * @throws FileNotFoundException Throws FileNotFoundException if no
 * file exists under the URI or the mode is invalid.
 * @see #openAssetFileDescriptor(Uri, String)
 */
public final @Nullable ParcelFileDescriptor openFileDescriptor(@NonNull Uri uri,
        @NonNull String mode, @Nullable CancellationSignal cancellationSignal)
                throws FileNotFoundException {
    AssetFileDescriptor afd = openAssetFileDescriptor(uri, mode, cancellationSignal);
    if (afd == null) {
        return null;
    }

    if (afd.getDeclaredLength() < 0) {
        // This is a full file!
        return afd.getParcelFileDescriptor();
    }

    // Client can't handle a sub-section of a file, so close what
    // we got and bail with an exception.
    try {
        afd.close();
    } catch (IOException e) {
    }

    throw new FileNotFoundException("Not a whole file");
}
 
源代码20 项目: android_9.0.0_r45   文件: ContentResolver.java
/**
 * Open a raw file descriptor to access data under a URI.  This
 * is like {@link #openAssetFileDescriptor(Uri, String)}, but uses the
 * underlying {@link ContentProvider#openFile}
 * ContentProvider.openFile()} method, so will <em>not</em> work with
 * providers that return sub-sections of files.  If at all possible,
 * you should use {@link #openAssetFileDescriptor(Uri, String)}.  You
 * will receive a FileNotFoundException exception if the provider returns a
 * sub-section of a file.
 *
 * <h5>Accepts the following URI schemes:</h5>
 * <ul>
 * <li>content ({@link #SCHEME_CONTENT})</li>
 * <li>file ({@link #SCHEME_FILE})</li>
 * </ul>
 *
 * <p>See {@link #openAssetFileDescriptor(Uri, String)} for more information
 * on these schemes.
 * <p>
 * If opening with the exclusive "r" or "w" modes, the returned
 * ParcelFileDescriptor could be a pipe or socket pair to enable streaming
 * of data. Opening with the "rw" mode implies a file on disk that supports
 * seeking. If possible, always use an exclusive mode to give the underlying
 * {@link ContentProvider} the most flexibility.
 * <p>
 * If you are writing a file, and need to communicate an error to the
 * provider, use {@link ParcelFileDescriptor#closeWithError(String)}.
 *
 * @param uri The desired URI to open.
 * @param mode The file mode to use, as per {@link ContentProvider#openFile
 * ContentProvider.openFile}.
 * @param cancellationSignal A signal to cancel the operation in progress,
 *         or null if none. If the operation is canceled, then
 *         {@link OperationCanceledException} will be thrown.
 * @return Returns a new ParcelFileDescriptor pointing to the file.  You
 * own this descriptor and are responsible for closing it when done.
 * @throws FileNotFoundException Throws FileNotFoundException if no
 * file exists under the URI or the mode is invalid.
 * @see #openAssetFileDescriptor(Uri, String)
 */
public final @Nullable ParcelFileDescriptor openFileDescriptor(@NonNull Uri uri,
        @NonNull String mode, @Nullable CancellationSignal cancellationSignal)
                throws FileNotFoundException {
    AssetFileDescriptor afd = openAssetFileDescriptor(uri, mode, cancellationSignal);
    if (afd == null) {
        return null;
    }

    if (afd.getDeclaredLength() < 0) {
        // This is a full file!
        return afd.getParcelFileDescriptor();
    }

    // Client can't handle a sub-section of a file, so close what
    // we got and bail with an exception.
    try {
        afd.close();
    } catch (IOException e) {
    }

    throw new FileNotFoundException("Not a whole file");
}