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

The following examples show how to use android.content.res.AssetFileDescriptor#getStartOffset() . 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: Helper.java    From VIA-AI with MIT License 6 votes vote down vote up
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();
    }
}
 
Example 2
Source File: NativeVorbisLoader.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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();
        }
    }
}
 
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: 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 8
Source File: Media.java    From libvlc-sdk-android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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());
}
 
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: 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 11
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 12
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 13
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 14
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 15
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 16
Source File: NativeVorbisLoader.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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();
            }
        }
    }
}
 
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: ApkAssets.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@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);
        }
    }
}
 
Example 19
Source File: MediaUtil.java    From appinventor-extensions with Apache License 2.0 4 votes vote down vote up
/**
 * 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 + ".");
}
 
Example 20
Source File: ContentDataSource.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
@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;
}