Example usage for android.content.res AssetFileDescriptor close

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

Introduction

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

Prototype

@Override
public void close() throws IOException 

Source Link

Document

Convenience for calling getParcelFileDescriptor().close().

Usage

From source file:Main.java

public static void closeQuietly(AssetFileDescriptor afd) {
    if (afd == null)
        return;/*from www  . j  a  va 2 s .  co m*/

    try {
        afd.close();
    } catch (Exception e) {
    }
}

From source file:Main.java

public final static void closeQuietly(AssetFileDescriptor afd) {
    try {/*  w w  w.  j  ava  2 s .  c  o m*/
        if (afd != null)
            afd.close();
    } catch (Exception e) {
        Log.e("OpenVPN", "closing AssetFileDescriptor", e);
    }
}

From source file:Main.java

/**
 * Play video file from res folder./*from www  .  j a  v a2 s.c  om*/
 * 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 getBitmapForDensity(Resources res, int displayDpi, int resId) {
    try {/*w w w  .ja  v  a2s  . c o m*/
        TypedValue value = new TypedValue();
        res.getValueForDensity(resId, displayDpi, value, true);
        AssetFileDescriptor fd = res.getAssets().openNonAssetFd(value.assetCookie, value.string.toString());
        Options opt = new Options();
        opt.inTargetDensity = displayDpi;
        Bitmap bitmap = BitmapFactory.decodeResourceStream(res, value, fd.createInputStream(), null, opt);
        bitmap.setDensity(res.getDisplayMetrics().densityDpi);
        fd.close();
        return bitmap;
    } catch (Exception e) {
        return BitmapFactory.decodeResource(res, resId);
    }
}

From source file:Main.java

/**
 * Reads a Bitmap from an Uri./*from   w  w  w  .j  a  v  a 2s. co  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.//  www .  jav  a2s.co 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 != 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;//w  ww .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;//from www.j a va 2  s .  co  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:org.kontalk.util.MediaStorage.java

public static long getLength(Context context, Uri media) throws IOException {
    AssetFileDescriptor stat = null;
    long length = 0;
    try {/*from  w  w w . j  a  v  a2 s.  c o m*/
        stat = context.getContentResolver().openAssetFileDescriptor(media, "r");
        if (stat != null)
            length = stat.getLength();
    } finally {
        try {
            if (stat != null)
                stat.close();
        } catch (IOException e) {
            // ignored
        }
    }

    if (length == 0) {
        // try to count bytes by reading it
        InputStream in = null;
        try {
            in = context.getContentResolver().openInputStream(media);
            CountingInputStream counter = new CountingInputStream(in);
            counter.consume();
            length = counter.getByteCount();
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
                // ignored
            }
        }
    }

    return length;
}

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

private static Bitmap decodeBitmap(Context context, Uri theUri, int sampleSize) {
    Bitmap actuallyUsableBitmap = null;//from ww  w.  j  a v a2s .  co 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;
}