Android BitmapFactory - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android BitmapFactory class.

This code example shows how to use the following methods:decodeStream, readUTF, close
        BufferedInputStream b = null;
        DataInputStream d = null;
        try { 
            f = new FileInputStream(filePath);
            b = new BufferedInputStream(f, BUFSIZE);
            d = new DataInputStream(b);
            Uri uri = Uri.parse(d.readUTF());
            Bitmap thumb = BitmapFactory.decodeStream(d);
            setData(uri, thumb);
            d.close();
        } catch (IOException e) {
            return false; 
        } finally { 
            MenuHelper.closeSilently(f);
            MenuHelper.closeSilently(b);
            MenuHelper.closeSilently(d);
        } 
        return true; 
    } 
 
    public void updateDisplayIfNeeded() { 
8
CodeRank
This code example shows how to use the following methods:decodeStream
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
 
        BufferedInputStream stream = null;
        try { 
            stream = new BufferedInputStream(new FileInputStream(file));
            BitmapFactory.decodeStream(stream, null, options);
        } catch (FileNotFoundException e) {
            Log.e("HtmlImage.setSampleSize", e);
        } finally { 
            IOUtils.closeQuietly(stream);
        } 
 
        int scale = 1;
        if (options.outHeight > maxHeight || options.outWidth > maxWidth) {
            scale = Math.max(options.outHeight / maxHeight, options.outWidth / maxWidth);
        } 
        bfOptions.inSampleSize = scale;
    } 
 
8
CodeRank
This code example shows how to use the following methods:decodeByteArray
     * @param image 
     * @param sampleSize 
     * @return bitmap or null if read fails 
     */ 
    public static Bitmap getBitmap(final byte[] image, int sampleSize) {
        final Options options = new Options();
        options.inDither = false;
        options.inSampleSize = sampleSize;
        return BitmapFactory.decodeByteArray(image, 0, image.length, options);
    } 
 
    /** 
     * Get scale for image of size and max height/width 
     * 
     * @param size 
     * @param width 
     * @param height 
     * @return scale 
     */ 
    public static int getScale(Point size, int width, int height) {
7
CodeRank
This code example shows how to use the following methods:decodeStream
		decodeOptions.inJustDecodeBounds = true;
 
		InputStream in = null;
		try { 
			in = pAssetManager.open(pAssetPath);
			BitmapFactory.decodeStream(in, null, decodeOptions);
		} catch (final IOException e) {
			Debug.e("Failed loading Bitmap in AssetBitmapTextureAtlasSource. AssetPath: " + pAssetPath, e);
		} finally { 
			StreamUtils.close(in);
		} 
 
		return new AssetBitmapTextureAtlasSource(pAssetManager, pAssetPath, pTextureX, pTextureY, decodeOptions.outWidth, decodeOptions.outHeight);
	} 
 
	AssetBitmapTextureAtlasSource(final AssetManager pAssetManager, final String pAssetPath, final int pTextureX, final int pTextureY, final int pTextureWidth, final int pTextureHeight) {
		super(pTextureX, pTextureY, pTextureWidth, pTextureHeight);
 
		this.mAssetManager = pAssetManager;
		this.mAssetPath = pAssetPath;
8
CodeRank
This code example shows how to use the following methods:decodeStream
				URL aURL = new URL(url);
				URLConnection conn = aURL.openConnection();
 
				conn.connect();
 
				is = new BufferedInputStream(conn.getInputStream());
 
				return BitmapFactory.decodeStream(is);
			} catch (IOException e) {
				AphidLog.e(e, "Failed to load bitmap from url: " + url);
			} finally { 
				IO.close(is);
			} 
 
			return null; 
		} 
 
		@Override 
		protected void onPostExecute(Bitmap result) {
			if (isCancelled())
8
CodeRank
This code example shows how to use the following methods:decodeFile
        } 
    } 
 
    public static Bitmap requestDecode(JobContext jc, final String filePath,
            Options options) {
        if (options == null) options = new Options();
        jc.setCancelListener(new DecodeCanceller(options));
        return ensureGLCompatibleBitmap( 
                BitmapFactory.decodeFile(filePath, options));
    } 
 
    public static Bitmap requestDecode(JobContext jc, FileDescriptor fd, Options options) {
        if (options == null) options = new Options();
        jc.setCancelListener(new DecodeCanceller(options));
        return ensureGLCompatibleBitmap( 
                BitmapFactory.decodeFileDescriptor(fd, null, options));
    } 
 
    public static Bitmap requestDecode(JobContext jc, byte[] bytes,
            Options options) {
8
CodeRank
This code example shows how to use the following methods:decodeStream, readUTF, close
        BufferedInputStream b = null;
        DataInputStream d = null;
        try { 
            f = new FileInputStream(filePath);
            b = new BufferedInputStream(f, BUFSIZE);
            d = new DataInputStream(b);
            Uri uri = Uri.parse(d.readUTF());
            Bitmap thumb = BitmapFactory.decodeStream(d);
            setData(uri, thumb);
            d.close();
        } catch (IOException e) {
            return false; 
        } finally { 
            MenuHelper.closeSilently(f);
            MenuHelper.closeSilently(b);
            MenuHelper.closeSilently(d);
        } 
        return true; 
    } 
 
    public void updateDisplayIfNeeded(int duration) {
7
CodeRank
This code example shows how to use the following methods:decodeStream
		InputStream stream = openInputStream( context, uri );
		if( null == stream ) return null;
		 
		try { 
			// decode the bitmap via android BitmapFactory 
			bitmap = BitmapFactory.decodeStream( stream, null, options );
			IOUtils.closeSilently( stream );
 
			if ( bitmap != null ) {
				if( maxW > 0 && maxH > 0 ) {
					newBitmap = BitmapUtils.resizeBitmap( bitmap, maxW, maxH, orientation );
					if ( bitmap != newBitmap ) {
						bitmap.recycle();
					} 
					bitmap = newBitmap;
				} 
			} 
 
		} catch ( OutOfMemoryError error ) {
			IOUtils.closeSilently( stream );

7
CodeRank
This code example shows how to use the following methods:decodeStream, close
	 */ 
	public static Bitmap getBitmapByFile(File file) {
		FileInputStream fis = null;
		Bitmap bitmap =null; 
		try {  
			fis = new FileInputStream(file);
			bitmap = BitmapFactory.decodeStream(fis);
		} catch (FileNotFoundException e) {  
			e.printStackTrace();
		} catch (OutOfMemoryError e) {
			e.printStackTrace();
		} finally{ 
			try { 
				fis.close();
			} catch (Exception e) {}
		} 
		return bitmap;
	} 
	 
	/** 
7
CodeRank
This code example shows how to use the following methods:decodeStream
		final BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
		decodeOptions.inJustDecodeBounds = true;
 
		InputStream in = null;
		try { 
			in = new FileInputStream(pFile);
			BitmapFactory.decodeStream(in, null, decodeOptions);
		} catch (final IOException e) {
			Debug.e("Failed loading Bitmap in " + FileBitmapTextureAtlasSource.class.getSimpleName() + ". File: " + pFile, e);
		} finally { 
			StreamUtils.close(in);
		} 
 
		return new FileBitmapTextureAtlasSource(pFile, pTextureX, pTextureY, decodeOptions.outWidth, decodeOptions.outHeight);
	} 
 
	public static FileBitmapTextureAtlasSource createFromInternalStorage(final Context pContext, final String pFilePath, final int pTextureX, final int pTextureY) {
		return FileBitmapTextureAtlasSource.create(new File(FileUtils.getAbsolutePathOnInternalStorage(pContext, pFilePath)), pTextureX, pTextureY);
	} 
 
8
CodeRank

Related Android BitmapFactory Questions & Answers:

See Code Examples for Android BitmapFactory Methods: