Java Code Examples for android.content.ContentResolver#openAssetFileDescriptor()

The following examples show how to use android.content.ContentResolver#openAssetFileDescriptor() . 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: FileUtils.java    From Common with Apache License 2.0 6 votes vote down vote up
/**
 * Return whether the file exists.
 *
 * @param filePath The path of file.
 * @return {@code true}: yes<br>{@code false}: no
 */
public static boolean isFileExists(final Context context, final String filePath) {
    if (Build.VERSION.SDK_INT < 29) {
        return isFileExists(new File(filePath));
    } else {
        try {
            Uri uri = Uri.parse(filePath);
            ContentResolver cr = context.getContentResolver();
            AssetFileDescriptor afd = cr.openAssetFileDescriptor(uri, "r");
            if (afd == null) return false;
            try {
                afd.close();
            } catch (IOException ignore) {
            }
        } catch (FileNotFoundException e) {
            return false;
        }
        return true;
    }
}
 
Example 2
Source File: MediaPlayer.java    From BambooPlayer with Apache License 2.0 6 votes vote down vote up
public void setDataSource(Context context, Uri uri, Map<String, String> headers) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
  if (context == null || uri == null)
    throw new IllegalArgumentException();
  String scheme = uri.getScheme();
  if (scheme == null || scheme.equals("file")) {
    setDataSource(FileUtils.getPath(uri.toString()));
    return;
  }

  try {
    ContentResolver resolver = context.getContentResolver();
    mFD = resolver.openAssetFileDescriptor(uri, "r");
    if (mFD == null)
      return;
    setDataSource(mFD.getParcelFileDescriptor().getFileDescriptor());
    return;
  } catch (Exception e) {
    closeFD();
  }
  setDataSource(uri.toString(), headers);
}
 
Example 3
Source File: MediaMetadataRetriever.java    From BambooPlayer with Apache License 2.0 6 votes vote down vote up
public void setDataSource(Context context, Uri uri) throws IOException, IllegalArgumentException,
    SecurityException, IllegalStateException {
  if (context == null || uri == null)
    throw new IllegalArgumentException();
  String scheme = uri.getScheme();
  if (scheme == null || scheme.equals("file")) {
    setDataSource(FileUtils.getPath(uri.toString()));
    return;
  }

  try {
    ContentResolver resolver = context.getContentResolver();
    mFD = resolver.openAssetFileDescriptor(uri, "r");
    if (mFD == null)
      return;
    setDataSource(mFD.getParcelFileDescriptor().getFileDescriptor());
    return;
  } catch (Exception e) {
    closeFD();
  }
  Log.e("Couldn't open file on client side, trying server side %s", uri.toString());
  setDataSource(uri.toString());
  return;
}
 
Example 4
Source File: MediaPlayer.java    From Vitamio with Apache License 2.0 6 votes vote down vote up
public void setDataSource(Context context, Uri uri, Map<String, String> headers) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
  if (context == null || uri == null)
    throw new IllegalArgumentException();
  String scheme = uri.getScheme();
  if (scheme == null || scheme.equals("file")) {
    setDataSource(FileUtils.getPath(uri.toString()));
    return;
  }

  try {
    ContentResolver resolver = context.getContentResolver();
    mFD = resolver.openAssetFileDescriptor(uri, "r");
    if (mFD == null)
      return;
    setDataSource(mFD.getParcelFileDescriptor().getFileDescriptor());
    return;
  } catch (Exception e) {
    closeFD();
  }
  setDataSource(uri.toString(), headers);
}
 
Example 5
Source File: MediaMetadataRetriever.java    From react-native-android-vitamio with MIT License 6 votes vote down vote up
public void setDataSource(Context context, Uri uri) throws IOException, IllegalArgumentException,
    SecurityException, IllegalStateException {
  if (context == null || uri == null)
    throw new IllegalArgumentException();
  String scheme = uri.getScheme();
  if (scheme == null || scheme.equals("file")) {
    setDataSource(FileUtils.getPath(uri.toString()));
    return;
  }

  try {
    ContentResolver resolver = context.getContentResolver();
    mFD = resolver.openAssetFileDescriptor(uri, "r");
    if (mFD == null)
      return;
    setDataSource(mFD.getParcelFileDescriptor().getFileDescriptor());
    return;
  } catch (Exception e) {
    closeFD();
  }
  Log.e("Couldn't open file on client side, trying server side %s", uri.toString());
  setDataSource(uri.toString());
  return;
}
 
Example 6
Source File: MediaPlayer.java    From react-native-android-vitamio with MIT License 6 votes vote down vote up
public void setDataSource(Context context, Uri uri, Map<String, String> headers) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
  if (context == null || uri == null)
    throw new IllegalArgumentException();
  String scheme = uri.getScheme();
  if (scheme == null || scheme.equals("file")) {
    setDataSource(FileUtils.getPath(uri.toString()));
    return;
  }

  try {
    ContentResolver resolver = context.getContentResolver();
    mFD = resolver.openAssetFileDescriptor(uri, "r");
    if (mFD == null)
      return;
    setDataSource(mFD.getParcelFileDescriptor().getFileDescriptor());
    return;
  } catch (Exception e) {
    closeFD();
  }
  setDataSource(uri.toString(), headers);
}
 
Example 7
Source File: MediaMetadataRetriever.java    From NetEasyNews with GNU General Public License v3.0 6 votes vote down vote up
public void setDataSource(Context context, Uri uri) throws IOException, IllegalArgumentException,
    SecurityException, IllegalStateException {
  if (context == null || uri == null)
    throw new IllegalArgumentException();
  String scheme = uri.getScheme();
  if (scheme == null || scheme.equals("file")) {
    setDataSource(FileUtils.getPath(uri.toString()));
    return;
  }

  try {
    ContentResolver resolver = context.getContentResolver();
    mFD = resolver.openAssetFileDescriptor(uri, "r");
    if (mFD == null)
      return;
    setDataSource(mFD.getParcelFileDescriptor().getFileDescriptor());
    return;
  } catch (Exception e) {
    closeFD();
  }
  Log.e("Couldn't open file on client side, trying server side %s", uri.toString());
  setDataSource(uri.toString());
  return;
}
 
Example 8
Source File: MediaPlayer.java    From NetEasyNews with GNU General Public License v3.0 6 votes vote down vote up
public void setDataSource(Context context, Uri uri, Map<String, String> headers) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
  if (context == null || uri == null)
    throw new IllegalArgumentException();
  String scheme = uri.getScheme();
  if (scheme == null || scheme.equals("file")) {
    setDataSource(FileUtils.getPath(uri.toString()));
    return;
  }

  try {
    ContentResolver resolver = context.getContentResolver();
    mFD = resolver.openAssetFileDescriptor(uri, "r");
    if (mFD == null)
      return;
    setDataSource(mFD.getParcelFileDescriptor().getFileDescriptor());
    return;
  } catch (Exception e) {
    closeFD();
  }
  setDataSource(uri.toString(), headers);
}
 
Example 9
Source File: MediaPlayer.java    From video-player with MIT License 6 votes vote down vote up
public void setDataSource(Context context, Uri uri, Map<String, String> headers) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
  if (context == null || uri == null)
    throw new IllegalArgumentException();
  String scheme = uri.getScheme();
  if (scheme == null || scheme.equals("file")) {
    setDataSource(FileUtils.getPath(uri.toString()));
    return;
  }

  try {
    ContentResolver resolver = context.getContentResolver();
    mFD = resolver.openAssetFileDescriptor(uri, "r");
    if (mFD == null)
      return;
    setDataSource(mFD.getParcelFileDescriptor().getFileDescriptor());
    return;
  } catch (Exception e) {
    closeFD();
  }
  setDataSource(uri.toString(), headers);
}
 
Example 10
Source File: MediaMetadataRetriever.java    From HPlayer with Apache License 2.0 6 votes vote down vote up
public void setDataSource(Context context, Uri uri) throws IOException, IllegalArgumentException,
    SecurityException, IllegalStateException {
  if (context == null || uri == null)
    throw new IllegalArgumentException();
  String scheme = uri.getScheme();
  if (scheme == null || scheme.equals("file")) {
    setDataSource(FileUtils.getPath(uri.toString()));
    return;
  }

  try {
    ContentResolver resolver = context.getContentResolver();
    mFD = resolver.openAssetFileDescriptor(uri, "r");
    if (mFD == null)
      return;
    setDataSource(mFD.getParcelFileDescriptor().getFileDescriptor());
    return;
  } catch (Exception e) {
    closeFD();
  }
  Log.e("Couldn't open file on client side, trying server side %s", uri.toString());
  setDataSource(uri.toString());
  return;
}
 
Example 11
Source File: MediaPlayer.java    From HPlayer with Apache License 2.0 6 votes vote down vote up
public void setDataSource(Context context, Uri uri, Map<String, String> headers) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
  if (context == null || uri == null)
    throw new IllegalArgumentException();
  String scheme = uri.getScheme();
  if (scheme == null || scheme.equals("file")) {
    setDataSource(FileUtils.getPath(uri.toString()));
    return;
  }

  try {
    ContentResolver resolver = context.getContentResolver();
    mFD = resolver.openAssetFileDescriptor(uri, "r");
    if (mFD == null)
      return;
    setDataSource(mFD.getParcelFileDescriptor().getFileDescriptor());
    return;
  } catch (Exception e) {
    closeFD();
  }
  setDataSource(uri.toString(), headers);
}
 
Example 12
Source File: MediaMetadataRetriever.java    From MyHearts with Apache License 2.0 6 votes vote down vote up
public void setDataSource(Context context, Uri uri) throws IOException, IllegalArgumentException,
    SecurityException, IllegalStateException {
  if (context == null || uri == null)
    throw new IllegalArgumentException();
  String scheme = uri.getScheme();
  if (scheme == null || scheme.equals("file")) {
    setDataSource(FileUtils.getPath(uri.toString()));
    return;
  }

  try {
    ContentResolver resolver = context.getContentResolver();
    mFD = resolver.openAssetFileDescriptor(uri, "r");
    if (mFD == null)
      return;
    setDataSource(mFD.getParcelFileDescriptor().getFileDescriptor());
    return;
  } catch (Exception e) {
    closeFD();
  }
  Log.e("Couldn't open file on client side, trying server side %s", uri.toString());
  setDataSource(uri.toString());
  return;
}
 
Example 13
Source File: MediaPlayer.java    From MyHearts with Apache License 2.0 6 votes vote down vote up
public void setDataSource(Context context, Uri uri, Map<String, String> headers) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
  if (context == null || uri == null)
    throw new IllegalArgumentException();
  String scheme = uri.getScheme();
  if (scheme == null || scheme.equals("file")) {
    setDataSource(FileUtils.getPath(uri.toString()));
    return;
  }

  try {
    ContentResolver resolver = context.getContentResolver();
    mFD = resolver.openAssetFileDescriptor(uri, "r");
    if (mFD == null)
      return;
    setDataSource(mFD.getParcelFileDescriptor().getFileDescriptor());
    return;
  } catch (Exception e) {
    closeFD();
  }
  setDataSource(uri.toString(), headers);
}
 
Example 14
Source File: FileUtils.java    From AndroidUtilCode with Apache License 2.0 6 votes vote down vote up
private static boolean isFileExistsApi29(String filePath) {
    if (Build.VERSION.SDK_INT >= 29) {
        try {
            Uri uri = Uri.parse(filePath);
            ContentResolver cr = Utils.getApp().getContentResolver();
            AssetFileDescriptor afd = cr.openAssetFileDescriptor(uri, "r");
            if (afd == null) return false;
            try {
                afd.close();
            } catch (IOException ignore) {
            }
        } catch (FileNotFoundException e) {
            return false;
        }
        return true;
    }
    return false;
}
 
Example 15
Source File: FilePathHelper.java    From qcloud-sdk-android-samples with MIT License 6 votes vote down vote up
private static InputStream getInputStreamForVirtualFile(Context context, Uri uri)
        throws IOException {

    ContentResolver resolver = context.getContentResolver();

    String[] openableMimeTypes = resolver.getStreamTypes(uri, "*/*");

    if (openableMimeTypes == null ||
            openableMimeTypes.length < 1) {
        throw new FileNotFoundException();
    }

    AssetFileDescriptor assetFileDescriptor = resolver.openAssetFileDescriptor(uri, openableMimeTypes[0], null);
    if (assetFileDescriptor == null) {
        throw new IOException("open virtual file failed");
    }
    return assetFileDescriptor.createInputStream();
}
 
Example 16
Source File: UriUtil.java    From fresco with MIT License 5 votes vote down vote up
/**
 * Gets the AssetFileDescriptor for a local file. This offers an alternative solution for opening
 * content:// scheme files
 *
 * @param contentResolver the content resolver which will query for the source file
 * @param srcUri The source uri
 * @return The AssetFileDescriptor for the file or null if it doesn't exist
 */
@Nullable
public static AssetFileDescriptor getAssetFileDescriptor(
    ContentResolver contentResolver, final Uri srcUri) {
  if (isLocalContentUri(srcUri)) {
    try {
      return contentResolver.openAssetFileDescriptor(srcUri, "r");
    } catch (FileNotFoundException e) {
      return null;
    }
  }
  return null;
}
 
Example 17
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 18
Source File: GifInfoHandle.java    From sketch with Apache License 2.0 5 votes vote down vote up
static GifInfoHandle openUri(ContentResolver resolver, Uri uri) throws IOException {
	if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) { //workaround for #128
		return new GifInfoHandle(uri.getPath());
	}
	final AssetFileDescriptor assetFileDescriptor = resolver.openAssetFileDescriptor(uri, "r");
	if (assetFileDescriptor == null) {
		throw new IOException("Could not open AssetFileDescriptor for " + uri);
	}
	return new GifInfoHandle(assetFileDescriptor);
}
 
Example 19
Source File: BindDeviceAdminFragment.java    From android-testdpc with Apache License 2.0 5 votes vote down vote up
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == INSTALL_CA_CERTIFICATE_REQUEST_CODE) {
            final Uri uri;
            if (data != null && (uri = data.getData()) != null) {
                ContentResolver contentResolver = getActivity().getContentResolver();
                AssetFileDescriptor afd;
                try {
                    afd = contentResolver.openAssetFileDescriptor(uri, "r");
                } catch (FileNotFoundException e) {
                    Log.e(TAG, "Could not find certificate file", e);
                    return;
                }
                boolean bindSuccess = mBindDeviceAdminServiceHelper.crossUserCall(service -> {
                    boolean isCaInstalled = service.installCaCertificate(afd);
                    String toastMessage = isCaInstalled ?
                            getString(R.string.install_ca_successfully)
                            : getString(R.string.install_ca_fail);
                    getActivity().runOnUiThread(() -> Toast.makeText(getActivity(),
                            toastMessage, Toast.LENGTH_SHORT).show());
                    Toast.makeText(getActivity(), toastMessage, Toast.LENGTH_SHORT).show();
                });
                if (!bindSuccess) {
                    Toast.makeText(getActivity(),
                            R.string.bind_to_profile_owner_failed, Toast.LENGTH_LONG)
                            .show();
                }
            }
        }
    }
}
 
Example 20
Source File: AddContactActivity.java    From BaldPhone with Apache License 2.0 5 votes vote down vote up
private static void addFullSizePhoto(int rawContactId, byte[] fullSizedPhotoData, final ContentResolver cr) throws IOException {
    final Uri baseUri = ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, rawContactId);
    final Uri displayPhotoUri = Uri.withAppendedPath(baseUri, ContactsContract.RawContacts.DisplayPhoto.CONTENT_DIRECTORY);
    final AssetFileDescriptor fileDescriptor = cr.openAssetFileDescriptor(displayPhotoUri, "rw");
    final FileOutputStream photoStream = fileDescriptor.createOutputStream();
    photoStream.write(fullSizedPhotoData);
    photoStream.close();
    fileDescriptor.close();
}