Example usage for android.content.res AssetFileDescriptor getFileDescriptor

List of usage examples for android.content.res AssetFileDescriptor getFileDescriptor

Introduction

In this page you can find the example usage for android.content.res AssetFileDescriptor getFileDescriptor.

Prototype

public FileDescriptor getFileDescriptor() 

Source Link

Document

Returns the FileDescriptor that can be used to read the data in the file.

Usage

From source file:Main.java

/**
 * Play video file from res folder.//from  ww  w .  j  a va  2 s  .  co m
 * Then call mediaPlayer.start();
 * @param fileName
 * @param listener
 * @return
 * @throws Exception
 */
public static MediaPlayer playSound(AssetManager assetManager, String fileName,
        MediaPlayer.OnCompletionListener listener) throws Exception {
    MediaPlayer mediaPlayer = new MediaPlayer();
    if (listener != null) {
        mediaPlayer.setOnCompletionListener(listener);
    }

    AssetFileDescriptor descriptor = assetManager.openFd(fileName);
    mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(),
            descriptor.getLength());
    descriptor.close();
    mediaPlayer.prepare();

    return mediaPlayer;
}

From source file:Main.java

public static Bitmap getTempBitmap(Context context) {
    Bitmap capturedBitmap = null;//from   w ww .  j  av a  2 s  .  c o  m
    final File file = getTempFile(context);
    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;

        AssetFileDescriptor fileDescriptor = context.getContentResolver()
                .openAssetFileDescriptor(Uri.fromFile(file), "r");
        capturedBitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return capturedBitmap;
}

From source file:Main.java

public static void playNotifycationMusic(Context context, String voicePath) throws IOException {
    // paly music ...
    AssetFileDescriptor fileDescriptor = context.getAssets().openFd(voicePath);
    if (mediaPlayer == null) {
        mediaPlayer = new MediaPlayer();
    }/*from ww w  . ja  va 2s .co m*/
    if (mediaPlayer.isPlaying()) {
        mediaPlayer.stop();
    }
    mediaPlayer.reset();
    mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(),
            fileDescriptor.getLength());
    mediaPlayer.prepare();
    mediaPlayer.setLooping(false);
    mediaPlayer.start();
}

From source file:Main.java

/**
 * Reads a Bitmap from an Uri./*from w  w  w  . j  a  va  2s .c o  m*/
 *
 * @param context
 * @param selectedImage
 * @return Bitmap
 */
public static Bitmap readBitmap(Context context, Uri selectedImage) {
    Bitmap bm = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    options.inScaled = false;
    //      options.inSampleSize = 3;
    AssetFileDescriptor fileDescriptor = null;
    try {
        fileDescriptor = context.getContentResolver().openAssetFileDescriptor(selectedImage, "r");
    } catch (FileNotFoundException e) {
        return null;
    } finally {
        try {
            bm = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
            fileDescriptor.close();
        } catch (IOException e) {
            return null;
        }
    }
    return bm;
}

From source file:Main.java

/**
 * Reads a Bitmap from an Uri.//from   w w  w .  ja v  a 2s.  com
 *
 * @param context
 * @param selectedImage
 * @return Bitmap
 */
public static Bitmap readBitmap(Context context, Uri selectedImage) {
    Bitmap bm = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    options.inScaled = false;
    //      options.inSampleSize = 3;
    AssetFileDescriptor fileDescriptor = null;
    try {
        fileDescriptor = context.getContentResolver().openAssetFileDescriptor(selectedImage, "r");
    } catch (FileNotFoundException e) {
        return null;
    } finally {
        try {
            bm = BitmapFactory.decodeFileDescriptor(
                    fileDescriptor != null ? fileDescriptor.getFileDescriptor() : null, null, options);
            if (fileDescriptor != null) {
                fileDescriptor.close();
            }
        } catch (IOException e) {
            return null;
        }
    }
    return bm;
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromFile(Resources res, ContentResolver contentResolver, Uri uri,
        int reqWidthInPixel, int reqHeightInPixel) throws IOException {

    Bitmap bitmap;/*from  ww  w. j  a v  a  2  s .  co  m*/
    AssetFileDescriptor fileDescriptor;

    fileDescriptor = contentResolver.openAssetFileDescriptor(uri, "r");

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidthInPixel, reqHeightInPixel);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
    fileDescriptor.close();
    return bitmap;
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromFile(Resources res, ContentResolver contentResolver, File file,
        int reqWidthInPixel, int reqHeightInPixel) throws IOException {

    Bitmap bitmap;//  w  w w  .  j a va  2  s.  c o m
    AssetFileDescriptor fileDescriptor;

    fileDescriptor = contentResolver.openAssetFileDescriptor(Uri.fromFile(file), "r");

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidthInPixel, reqHeightInPixel);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
    fileDescriptor.close();
    return bitmap;
}

From source file:com.aegiswallet.utils.BasicUtils.java

public static boolean isPasswordInDictionary(Context context, String password) {
    boolean resultBool = false;

    if (password == null)
        return false;

    try {//from  w  w  w.  j a  va2s.com

        AssetFileDescriptor descriptor = context.getAssets().openFd("commonpasswords.xmf");
        FileReader reader = new FileReader(descriptor.getFileDescriptor());

        // create a case sensitive word list and sort it
        ArrayWordList awl = WordLists.createFromReader(new FileReader[] { reader }, true, new ArraysSort());

        WordListDictionary dict = new WordListDictionary(awl);

        DictionarySubstringRule dictRule = new DictionarySubstringRule(dict);
        dictRule.setWordLength(6); // size of words to check in the password
        dictRule.setMatchBackwards(true); // match dictionary words backwards

        List<Rule> ruleList = new ArrayList<Rule>();
        ruleList.add(dictRule);

        PasswordValidator validator = new PasswordValidator(ruleList);
        PasswordData passwordData = new PasswordData(new Password(password.toLowerCase()));

        RuleResult result = validator.validate(passwordData);
        if (result.isValid()) {
            Log.d(TAG, "Valid password");
            resultBool = true;
        } else {
            Log.d(TAG, "Invalid password");
        }
    } catch (FileNotFoundException e) {
        Log.d(TAG, e.getMessage());
    } catch (IOException e) {
        Log.d(TAG, e.getMessage());
    }

    return resultBool;
}

From source file:com.mvc.imagepicker.ImagePicker.java

private static Bitmap decodeBitmap(Context context, Uri theUri, int sampleSize) {
    Bitmap actuallyUsableBitmap = null;/*from w  w w. ja  v a2 s.  c  o  m*/
    AssetFileDescriptor fileDescriptor = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = sampleSize;

    try {
        fileDescriptor = context.getContentResolver().openAssetFileDescriptor(theUri, "r");
        actuallyUsableBitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null,
                options);
        if (actuallyUsableBitmap != null) {
            Log.i(TAG, "Trying sample size " + options.inSampleSize + "\t\t" + "Bitmap width: "
                    + actuallyUsableBitmap.getWidth() + "\theight: " + actuallyUsableBitmap.getHeight());
        }
        fileDescriptor.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return actuallyUsableBitmap;
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromFile(Resources res, ContentResolver contentResolver, Uri uri,
        float reqWidthInDip, float reqHeightInDip) throws IOException {

    Bitmap bitmap;//from  w w w .jav a  2s  . c o  m
    AssetFileDescriptor fileDescriptor;
    int reqWidthInPixel = (int) dipToPixels(res, reqWidthInDip);
    int reqHeightInPixel = (int) dipToPixels(res, reqHeightInDip);
    fileDescriptor = contentResolver.openAssetFileDescriptor(uri, "r");

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidthInPixel, reqHeightInPixel);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
    fileDescriptor.close();
    return bitmap;
}