Java Code Examples for android.content.res.AssetFileDescriptor#getDeclaredLength()

The following examples show how to use android.content.res.AssetFileDescriptor#getDeclaredLength() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SystemServices.java    From Zom-Android-XMPP with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: ImageClassifier.java    From Machine-Learning-Projects-for-Mobile-Applications with MIT License 5 votes vote down vote up
/** 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);
}
 
Example 3
Source File: TFLiteObjectDetectionAPIModel.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
/** 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);
}
 
Example 4
Source File: AssetModelLoader.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
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);
}
 
Example 5
Source File: TensorFlowHelper.java    From sample-tensorflow-imageclassifier with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 6
Source File: TensorFlowHelper.java    From androidthings-imageclassifier with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 7
Source File: OpenSLMediaPlayer.java    From android-openslmediaplayer with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 8
Source File: TensorFlowHelper.java    From androidthings-imageclassifier with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 9
Source File: TensorFlowImageClassifier.java    From Android-TensorFlow-Lite-Example with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 10
Source File: TFLiteObjectDetectionAPIModel.java    From tfliteSSDminimalWorkingExample with Apache License 2.0 5 votes vote down vote up
/** 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);
}
 
Example 11
Source File: SliceTestActivity.java    From incubator-weex-playground with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 12
Source File: Classifier.java    From Machine-Learning-Projects-for-Mobile-Applications with MIT License 5 votes vote down vote up
/** 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);
}
 
Example 13
Source File: DeepLabLite.java    From ml with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 14
Source File: TFLiteObjectDetectionAPIModel.java    From ml with Apache License 2.0 5 votes vote down vote up
/** 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);
}
 
Example 15
Source File: TfLiteObjectDetection.java    From next18-ai-in-motion with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 16
Source File: TfLiteCommander.java    From next18-ai-in-motion with Apache License 2.0 5 votes vote down vote up
/** 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);
}
 
Example 17
Source File: Classifier.java    From yolov3-android-tflite with MIT License 5 votes vote down vote up
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);
}
 
Example 18
Source File: MyUtil.java    From Android-MobileFaceNet-MTCNN-FaceAntiSpoofing with MIT License 5 votes vote down vote up
/**
 * 加载模型文件
 * @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);
}
 
Example 19
Source File: ContentResolver.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * 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");
}
 
Example 20
Source File: ContentResolver.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
/**
 * 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");
}