Example usage for android.graphics BitmapFactory decodeFileDescriptor

List of usage examples for android.graphics BitmapFactory decodeFileDescriptor

Introduction

In this page you can find the example usage for android.graphics BitmapFactory decodeFileDescriptor.

Prototype

public static Bitmap decodeFileDescriptor(FileDescriptor fd) 

Source Link

Document

Decode a bitmap from the file descriptor.

Usage

From source file:Main.java

public static Bitmap getBitmapFromUri(Uri uri, Context context) {
    ParcelFileDescriptor parcelFileDescriptor = null;
    try {/*from  w  w w.  j  a va 2 s. co  m*/

        parcelFileDescriptor = context.getContentResolver().openFileDescriptor(uri, "r");
        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
        Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
        parcelFileDescriptor.close();
        return image;
    } catch (Exception e) {
        Log.e(TAG, "Failed to load image.", e);
        return null;
    } finally {
        try {
            if (parcelFileDescriptor != null) {
                parcelFileDescriptor.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(TAG, "Error closing ParcelFile Descriptor");
        }
    }
}

From source file:com.instagram.igdiskcache.demo.cache.BitmapCache.java

/**
 * Get the decoded Bitmap from disk cache
 *///from   w ww.  j a v a  2s.co m
public Bitmap getBitmapFromDiskCache(String key) {
    Bitmap bitmap = null;
    OptionalStream<SnapshotInputStream> input = getDiskCache().get(key);
    if (input.isPresent()) {
        try {
            FileDescriptor fd = input.get().getFD();
            bitmap = BitmapFactory.decodeFileDescriptor(fd);
        } catch (IOException e) {
            Log.e(TAG, "getBitmapFromDiskCache - " + e);
        } finally {
            Utils.closeQuietly(input.get());
        }
    }
    return bitmap;
}

From source file:com.githang.androidkit.cache.BitmapLruCache.java

/**
 * ??{@code key}Bitmapnull//from  ww  w. ja  va2s  .c  om
 * 
 * @param key
 * @return
 */
public Bitmap getBitmapFromDiskCache(String key) {
    synchronized (mDiskCacheLock) {
        while (mDiskCacheStarting) {
            try {
                mDiskCacheLock.wait();
            } catch (InterruptedException e) {
                log.e(e.getMessage(), e);
            }
        }
        if (mDiskLruCache != null) {
            InputStream is = null;
            try {
                String hashKey = hashKeyForDisk(key);
                final DiskLruCache.Snapshot snapshot = mDiskLruCache.get(hashKey);
                if (snapshot != null) {
                    is = snapshot.getInputStream(DISK_CACHE_INDEX);
                    log.d("inputstream....." + is);
                    if (is != null) {
                        FileDescriptor fd = ((FileInputStream) is).getFD();
                        return BitmapFactory.decodeFileDescriptor(fd);
                    }
                }
            } catch (IOException e) {
                log.e(e.getMessage(), e);
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
    }
    return null;
}

From source file:net.ustyugov.jtalk.activity.vcard.SetVcardActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == IMAGE && data != null) {
            Uri uri = Uri.parse(data.getDataString());
            if (uri != null) {
                try {
                    FileInputStream fileInput = getContentResolver().openAssetFileDescriptor(uri, "r")
                            .createInputStream();
                    bytes = new byte[fileInput.available()];
                    fileInput.read(bytes);
                    Bitmap bm = Bitmap.createScaledBitmap(BitmapFactory.decodeFileDescriptor(fileInput.getFD()),
                            240, 240, true);
                    av.setImageBitmap(bm);
                    fileInput.close();//from  ww w  .  j a  v a  2 s .  c o  m
                } catch (FileNotFoundException e) {
                } catch (IOException e) {
                }
            }
        }
    }
}

From source file:info.papdt.blacklight.ui.statuses.NewPostActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Image picked, decode
    if (requestCode == REQUEST_PICK_IMG && resultCode == RESULT_OK) {
        if (Build.VERSION.SDK_INT >= 19) {
            try {
                ParcelFileDescriptor parcelFileDescriptor = getContentResolver()
                        .openFileDescriptor(data.getData(), "r");
                FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
                Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
                parcelFileDescriptor.close();
                addPicture(image, null);
            } catch (FileNotFoundException e) {
                e.printStackTrace();/*from  ww  w .jav a 2  s .c  o m*/
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            Cursor cursor = getContentResolver().query(data.getData(),
                    new String[] { MediaStore.Images.Media.DATA }, null, null, null);
            cursor.moveToFirst();
            String filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            cursor.close();

            // Then decode
            addPicture(null, filePath);
        }
    } else if (requestCode == REQUEST_CAPTURE_PHOTO && resultCode == RESULT_OK) {
        addPicture(null, Utility.lastPicPath);
    } else if (resultCode == MultiPicturePicker.PICK_OK) {
        ArrayList<String> paths = data.getStringArrayListExtra("img");

        for (String path : paths) {
            addPicture(null, path);

            if (mBitmaps.size() >= 9) {
                break;
            }
        }
    }
}

From source file:com.proalias.awesomestatus.MainActivity.java

private Bitmap getBitmapFromUri(Uri uri) throws IOException {
    ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r");
    FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
    Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
    parcelFileDescriptor.close();/*from   ww  w .ja  v  a 2  s  .c  o  m*/
    return image;
}

From source file:com.gecq.musicwave.cache.ImageCache.java

/**
 * Used to fetch the artwork for an album locally from the user's device
 *
 * @param context The {@link Context} to use
 * @param albumID The ID of the album to find artwork for
 * @return The artwork for an album/*  w w  w . j a  v a 2  s  .  c  o  m*/
 */
public final Bitmap getArtworkFromFile(final Context context, final long albumId) {
    if (albumId < 0) {
        return null;
    }
    Bitmap artwork = null;
    waitUntilUnpaused();
    try {
        final Uri uri = ContentUris.withAppendedId(mArtworkUri, albumId);
        final ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver().openFileDescriptor(uri,
                "r");
        if (parcelFileDescriptor != null) {
            final FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
            artwork = BitmapFactory.decodeFileDescriptor(fileDescriptor);
        }
    } catch (final IllegalStateException e) {
        // Log.e(TAG, "IllegalStateExcetpion - getArtworkFromFile - ", e);
    } catch (final FileNotFoundException e) {
        // Log.e(TAG, "FileNotFoundException - getArtworkFromFile - ", e);
    } catch (final OutOfMemoryError evict) {
        // Log.e(TAG, "OutOfMemoryError - getArtworkFromFile - ", evict);
        evictAll();
    }
    return artwork;
}

From source file:com.boko.vimusic.cache.ImageCache.java

/**
 * Used to fetch the artwork for an album locally from the user's device
 * /*w w w.j  a v  a2  s  .  c  om*/
 * @param context
 *            The {@link Context} to use
 * @param albumID
 *            The ID of the album to find artwork for
 * @return The artwork for an album
 */
public final Bitmap getArtworkFromFile(final Context context, final String albumId) {
    if (albumId == null) {
        return null;
    }
    Bitmap artwork = null;
    waitUntilUnpaused();
    try {
        final Uri uri = ContentUris.withAppendedId(mArtworkUri, Long.valueOf(albumId));
        final ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver().openFileDescriptor(uri,
                "r");
        if (parcelFileDescriptor != null) {
            final FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
            artwork = BitmapFactory.decodeFileDescriptor(fileDescriptor);
        }
    } catch (final IllegalStateException e) {
        // Log.e(TAG, "IllegalStateExcetpion - getArtworkFromFile - ", e);
    } catch (final FileNotFoundException e) {
        // Log.e(TAG, "FileNotFoundException - getArtworkFromFile - ", e);
    } catch (final OutOfMemoryError evict) {
        // Log.e(TAG, "OutOfMemoryError - getArtworkFromFile - ", evict);
        evictAll();
    }
    return artwork;
}

From source file:com.andrew.apollo.cache.ImageCache.java

/**
 * Used to fetch the artwork for an album locally from the user's device
 * /*from www.j a  va 2  s .  co  m*/
 * @param context The {@link Context} to use
 * @param albumID The ID of the album to find artwork for
 * @return The artwork for an album
 */
public final Bitmap getArtworkFromFile(final Context context, final String albumId) {
    if (TextUtils.isEmpty(albumId)) {
        return null;
    }
    Bitmap artwork = null;
    while (mPauseDiskAccess) {
        // Pause for a moment
    }
    try {
        final Uri uri = ContentUris.withAppendedId(mArtworkUri, Long.valueOf(albumId));
        final ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver().openFileDescriptor(uri,
                "r");
        if (parcelFileDescriptor != null) {
            final FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
            artwork = BitmapFactory.decodeFileDescriptor(fileDescriptor);
        }
    } catch (final IllegalStateException e) {
        // Log.e(TAG, "IllegalStateExcetpion - getArtworkFromFile - ", e);
    } catch (final FileNotFoundException e) {
        // Log.e(TAG, "FileNotFoundException - getArtworkFromFile - ", e);
    } catch (final OutOfMemoryError evict) {
        // Log.e(TAG, "OutOfMemoryError - getArtworkFromFile - ", evict);
        evictAll();
    }
    return artwork;
}

From source file:uno.weichen.abnd10_inventoryapp.DetailActivity.java

private Bitmap getBitmapFromUri(Uri uri) {
    ParcelFileDescriptor parcelFileDescriptor = null;
    try {/* w  ww . j  a  va  2  s  . c o m*/
        parcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r");
        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
        Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
        parcelFileDescriptor.close();
        return image;
    } catch (Exception e) {
        Log.e(LOG_TAG, getString(R.string.get_bitmap_from_uri_exception), e);
        return null;
    } finally {
        try {
            if (parcelFileDescriptor != null) {
                parcelFileDescriptor.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(LOG_TAG, getString(R.string.get_bitmap_from_uri_error));
        }
    }
}