Java Code Examples for android.os.ParcelFileDescriptor#getStatSize()

The following examples show how to use android.os.ParcelFileDescriptor#getStatSize() . 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: VideoUploader.java    From kognitivo with Apache License 2.0 6 votes vote down vote up
private void initialize()
        throws FileNotFoundException {
    ParcelFileDescriptor fileDescriptor = null;
    try {
        if (Utility.isFileUri(videoUri)) {
            fileDescriptor = ParcelFileDescriptor.open(
                    new File(videoUri.getPath()),
                    ParcelFileDescriptor.MODE_READ_ONLY);
            videoSize = fileDescriptor.getStatSize();
            videoStream = new ParcelFileDescriptor.AutoCloseInputStream(fileDescriptor);
        } else if (Utility.isContentUri(videoUri)) {
            videoSize = Utility.getContentSize(videoUri);
            videoStream = FacebookSdk
                    .getApplicationContext()
                    .getContentResolver()
                    .openInputStream(videoUri);
        } else {
            throw new FacebookException("Uri must be a content:// or file:// uri");
        }
    } catch (FileNotFoundException e) {
        Utility.closeQuietly(videoStream);

        throw e;
    }
}
 
Example 2
Source File: FileSnippet.java    From mobly-bundled-snippets with Apache License 2.0 6 votes vote down vote up
@Rpc(description = "Compute MD5 hash on a content URI. Return the MD5 has has a hex string.")
public String fileMd5Hash(String uri) throws IOException, NoSuchAlgorithmException {
    Uri uri_ = Uri.parse(uri);
    ParcelFileDescriptor pfd = mContext.getContentResolver().openFileDescriptor(uri_, "r");
    MessageDigest md = MessageDigest.getInstance("MD5");
    int length = (int) pfd.getStatSize();
    byte[] buf = new byte[length];
    ParcelFileDescriptor.AutoCloseInputStream stream =
            new ParcelFileDescriptor.AutoCloseInputStream(pfd);
    DigestInputStream dis = new DigestInputStream(stream, md);
    try {
        dis.read(buf, 0, length);
        return Utils.bytesToHexString(md.digest());
    } finally {
        dis.close();
        stream.close();
    }
}
 
Example 3
Source File: DictionaryProvider.java    From Android-Keyboard with Apache License 2.0 5 votes vote down vote up
/**
 * Opens an asset file for an URI.
 *
 * Called by {@link ContentResolver#openAssetFileDescriptor(Uri, String)} or
 * {@link ContentResolver#openInputStream(Uri)} from a client requesting a
 * dictionary.
 * @see ContentProvider#openAssetFile(Uri, String)
 *
 * @param uri the URI the file is for.
 * @param mode the mode to read the file. MUST be "r" for readonly.
 * @return the descriptor, or null if the file is not found or if mode is not equals to "r".
 */
@Override
public AssetFileDescriptor openAssetFile(final Uri uri, final String mode) {
    if (null == mode || !"r".equals(mode)) return null;

    final int match = matchUri(uri);
    if (DICTIONARY_V1_DICT_INFO != match && DICTIONARY_V2_DATAFILE != match) {
        // Unsupported URI for openAssetFile
        Log.w(TAG, "Unsupported URI for openAssetFile : " + uri);
        return null;
    }
    final String wordlistId = uri.getLastPathSegment();
    final String clientId = getClientId(uri);
    final ContentValues wordList = getWordlistMetadataForWordlistId(clientId, wordlistId);

    if (null == wordList) return null;

    try {
        final int status = wordList.getAsInteger(MetadataDbHelper.STATUS_COLUMN);
        if (MetadataDbHelper.STATUS_DELETING == status) {
            // This will return an empty file (R.raw.empty points at an empty dictionary)
            // This is how we "delete" the files. It allows Android Keyboard to fake deleting
            // a default dictionary - which is actually in its assets and can't be really
            // deleted.
            final AssetFileDescriptor afd = getContext().getResources().openRawResourceFd(
                    R.raw.empty);
            return afd;
        }
        final String localFilename =
                wordList.getAsString(MetadataDbHelper.LOCAL_FILENAME_COLUMN);
        final File f = getContext().getFileStreamPath(localFilename);
        final ParcelFileDescriptor pfd =
                ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
        return new AssetFileDescriptor(pfd, 0, pfd.getStatSize());
    } catch (FileNotFoundException e) {
        // No file : fall through and return null
    }
    return null;
}
 
Example 4
Source File: UploadDataProviders.java    From cronet with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Uploads an entire file, closing the descriptor when it is no longer needed.
 *
 * @param fd The file descriptor to upload
 * @throws IllegalArgumentException if {@code fd} is not a file.
 * @return A new UploadDataProvider for the given file descriptor
 */
public static UploadDataProvider create(final ParcelFileDescriptor fd) {
    return new FileUploadProvider(new FileChannelProvider() {
        @Override
        public FileChannel getChannel() throws IOException {
            if (fd.getStatSize() != -1) {
                return new ParcelFileDescriptor.AutoCloseInputStream(fd).getChannel();
            } else {
                fd.close();
                throw new IllegalArgumentException("Not a file: " + fd);
            }
        }
    });
}
 
Example 5
Source File: DictionaryProvider.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 5 votes vote down vote up
/**
 * Opens an asset file for an URI.
 *
 * Called by {@link ContentResolver#openAssetFileDescriptor(Uri, String)} or
 * {@link ContentResolver#openInputStream(Uri)} from a client requesting a
 * dictionary.
 * @see ContentProvider#openAssetFile(Uri, String)
 *
 * @param uri the URI the file is for.
 * @param mode the mode to read the file. MUST be "r" for readonly.
 * @return the descriptor, or null if the file is not found or if mode is not equals to "r".
 */
@Override
public AssetFileDescriptor openAssetFile(final Uri uri, final String mode) {
    if (null == mode || !"r".equals(mode)) return null;

    final int match = matchUri(uri);
    if (DICTIONARY_V1_DICT_INFO != match && DICTIONARY_V2_DATAFILE != match) {
        // Unsupported URI for openAssetFile
        Log.w(TAG, "Unsupported URI for openAssetFile : " + uri);
        return null;
    }
    final String wordlistId = uri.getLastPathSegment();
    final String clientId = getClientId(uri);
    final ContentValues wordList = getWordlistMetadataForWordlistId(clientId, wordlistId);

    if (null == wordList) return null;

    try {
        final int status = wordList.getAsInteger(MetadataDbHelper.STATUS_COLUMN);
        if (MetadataDbHelper.STATUS_DELETING == status) {
            // This will return an empty file (R.raw.empty points at an empty dictionary)
            // This is how we "delete" the files. It allows Android Keyboard to fake deleting
            // a default dictionary - which is actually in its assets and can't be really
            // deleted.
            final AssetFileDescriptor afd = getContext().getResources().openRawResourceFd(
                    R.raw.empty);
            return afd;
        }
        final String localFilename =
                wordList.getAsString(MetadataDbHelper.LOCAL_FILENAME_COLUMN);
        final File f = getContext().getFileStreamPath(localFilename);
        final ParcelFileDescriptor pfd =
                ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
        return new AssetFileDescriptor(pfd, 0, pfd.getStatSize());
    } catch (FileNotFoundException e) {
        // No file : fall through and return null
    }
    return null;
}
 
Example 6
Source File: MainActivity.java    From revolution-irc with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE_PICK_FILE_DCC && data != null && data.getData() != null) {
        Uri uri = data.getData();
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
        int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
        cursor.moveToFirst();
        String name = DCCUtils.escapeFilename(cursor.getString(nameIndex));
        long size = cursor.isNull(sizeIndex) ? -1 : cursor.getLong(sizeIndex);

        String channel = ((ChatFragment) getCurrentFragment()).getCurrentChannel();
        try {
            ParcelFileDescriptor desc = getContentResolver().openFileDescriptor(uri, "r");
            if (desc == null)
                throw new IOException();
            if (size == -1)
                size = desc.getStatSize();

            DCCServer.FileChannelFactory fileFactory = () -> new FileInputStream(
                    desc.getFileDescriptor()).getChannel().position(0);
            DCCManager.getInstance(this).startUpload(((ChatFragment) getCurrentFragment())
                    .getConnectionInfo(), channel, fileFactory, name, size);
        } catch (IOException e) {
            Toast.makeText(this, R.string.error_file_open, Toast.LENGTH_SHORT).show();
            return;
        }
        return;
    }
    mDCCDialogHandler.onActivityResult(requestCode, resultCode, data);
    super.onActivityResult(requestCode, resultCode, data);
}
 
Example 7
Source File: DictionaryProvider.java    From Indic-Keyboard with Apache License 2.0 5 votes vote down vote up
/**
 * Opens an asset file for an URI.
 *
 * Called by {@link android.content.ContentResolver#openAssetFileDescriptor(Uri, String)} or
 * {@link android.content.ContentResolver#openInputStream(Uri)} from a client requesting a
 * dictionary.
 * @see android.content.ContentProvider#openAssetFile(Uri, String)
 *
 * @param uri the URI the file is for.
 * @param mode the mode to read the file. MUST be "r" for readonly.
 * @return the descriptor, or null if the file is not found or if mode is not equals to "r".
 */
@Override
public AssetFileDescriptor openAssetFile(final Uri uri, final String mode) {
    if (null == mode || !"r".equals(mode)) return null;

    final int match = matchUri(uri);
    if (DICTIONARY_V1_DICT_INFO != match && DICTIONARY_V2_DATAFILE != match) {
        // Unsupported URI for openAssetFile
        Log.w(TAG, "Unsupported URI for openAssetFile : " + uri);
        return null;
    }
    final String wordlistId = uri.getLastPathSegment();
    final String clientId = getClientId(uri);
    final ContentValues wordList = getWordlistMetadataForWordlistId(clientId, wordlistId);

    if (null == wordList) return null;

    try {
        final int status = wordList.getAsInteger(MetadataDbHelper.STATUS_COLUMN);
        if (MetadataDbHelper.STATUS_DELETING == status) {
            // This will return an empty file (R.raw.empty points at an empty dictionary)
            // This is how we "delete" the files. It allows Android Keyboard to fake deleting
            // a default dictionary - which is actually in its assets and can't be really
            // deleted.
            final AssetFileDescriptor afd = getContext().getResources().openRawResourceFd(
                    R.raw.empty);
            return afd;
        }
        final String localFilename =
                wordList.getAsString(MetadataDbHelper.LOCAL_FILENAME_COLUMN);
        final File f = getContext().getFileStreamPath(localFilename);
        final ParcelFileDescriptor pfd =
                ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
        return new AssetFileDescriptor(pfd, 0, pfd.getStatSize());
    } catch (FileNotFoundException e) {
        // No file : fall through and return null
    }
    return null;
}