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

The following examples show how to use android.content.res.AssetFileDescriptor#getFileDescriptor() . 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: TrickRandomAccessFile.java    From UniFile with Apache License 2.0 6 votes vote down vote up
@NonNull
static RandomAccessFile create(AssetFileDescriptor afd, String mode) throws IOException {
    if (afd == null) {
        throw new IOException("AssetFileDescriptor is null");
    }

    checkReflection();

    try {
        FileDescriptor fd = afd.getFileDescriptor();
        if (fd == null) {
            throw new IOException("Can't get FileDescriptor");
        }

        TrickRandomAccessFile file = create(fd, mode);
        file.mAfd = afd;
        return file;
    } catch (IOException e) {
        // Close AssetFileDescriptor if failed
        afd.close();
        throw e;
    }
}
 
Example 2
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 3
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 4
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 5
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 6
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 7
Source File: GifDrawable.java    From meatspace-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Creates drawable from AssetFileDescriptor.
 * Convenience wrapper for {@link pl.droidsonroids.gif.GifDrawable#GifDrawable(java.io.FileDescriptor)}
 * @param afd source
 * @throws NullPointerException if afd is null
 * @throws java.io.IOException when opening failed
 */
public GifDrawable ( AssetFileDescriptor afd ) throws IOException
{
	if (afd==null)
		throw new NullPointerException( "Source is null" );		
	FileDescriptor fd = afd.getFileDescriptor();
	mGifInfoPtr = openFd( mMetaData, fd, afd.getStartOffset() );
	mColors = new int[ mMetaData[ 0 ] * mMetaData[ 1 ] ];
	checkError();
}
 
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: 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 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: 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 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: 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 14
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 15
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 16
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 17
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 18
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;
}
 
Example 19
Source File: MediaMetadataRetriever.java    From DroidDLNA with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Sets the data source as a content Uri. Call this method before the rest
 * of the methods in this class. This method may be time-consuming.
 * 
 * @param context
 *            the Context to use when resolving the Uri
 * @param uri
 *            the Content URI of the data you want to play
 * @throws IllegalArgumentException
 *             if the Uri is invalid
 * @throws SecurityException
 *             if the Uri cannot be used due to lack of permission.
 */
public void setDataSource(Context context, Uri uri)
		throws IllegalArgumentException, SecurityException {
	if (uri == null) {
		throw new IllegalArgumentException();
	}

	String scheme = uri.getScheme();
	if (scheme == null || scheme.equals("file")) { // 匹配文件
		setDataSource(uri.getPath());
		return;
	}

	AssetFileDescriptor fd = null;
	try {
		ContentResolver resolver = context.getContentResolver();
		try {
			fd = resolver.openAssetFileDescriptor(uri, "r");// 读取文件
		} catch (FileNotFoundException e) {
			throw new IllegalArgumentException();
		}
		if (fd == null) {
			throw new IllegalArgumentException();
		}
		FileDescriptor descriptor = fd.getFileDescriptor();
		if (!descriptor.valid()) {
			throw new IllegalArgumentException();
		}
		// Note: using getDeclaredLength so that our behavior is the same
		// as previous versions when the content provider is returning
		// a full file.
		if (fd.getDeclaredLength() < 0) {
			setDataSource(descriptor);
		} else {
			setDataSource(descriptor, fd.getStartOffset(),
					fd.getDeclaredLength());
		}
		return;
	} catch (SecurityException ex) { 
	} finally {
		try {
			if (fd != null) {
				fd.close();
			}
		} catch (IOException ioEx) {
		}
	}
	setDataSource(uri.toString());
}
 
Example 20
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 + ".");
}