Java Code Examples for android.graphics.BitmapFactory#decodeFile()

The following examples show how to use android.graphics.BitmapFactory#decodeFile() . 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: ProjectItem.java    From PHONK with GNU General Public License v3.0 6 votes vote down vote up
public void setProjectInfo(Project p) {
    mProject = p;

    if (p.getName().length() > 2) {
        setIcon(p.getName().substring(0, 2));
    } else {
        setIcon(p.getName().substring(0, 1));
    }
    setText(p.getName());
    setTag(p.getName());

    File f = new File(p.getFullPathForFile("icon.png"));
    // setImage(R.drawable.primarycolor_rounded_rect);

    if (f.exists()) {
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        Bitmap bitmap = BitmapFactory.decodeFile(f.getPath(), bmOptions);
        bitmap = Bitmap.createScaledBitmap(bitmap, 100, 100, true);

        setImage(bitmap);
    }
    setMenu();
}
 
Example 2
Source File: ImageUtility.java    From iBeebo with GNU General Public License v3.0 6 votes vote down vote up
public static boolean isThisBitmapTooLargeToRead(String path) {

        File file = new File(path);

        if (!file.exists()) {
            return false;
        }

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        int width = options.outWidth;
        int height = options.outHeight;
        if (width == -1 || height == -1) {
            return false;
        }

        return width > Utility.getBitmapMaxWidthAndMaxHeight() || height > Utility.getBitmapMaxWidthAndMaxHeight();

    }
 
Example 3
Source File: Malevich.java    From malevich with MIT License 6 votes vote down vote up
/**
 * Decode and sample down a bitmap from a file to the requested width and height.
 *
 * @param filename The full path of the file to decode
 * @param reqWidth The requested width of the resulting bitmap
 * @param reqHeight The requested height of the resulting bitmap
 * @param cache The ImageCache used to find candidate bitmaps for use with inBitmap
 * @return A bitmap sampled down from the original with the same aspect ratio and dimensions
 *         that are equal to or greater than the requested width and height
 */
public static Bitmap decodeSampledBitmapFromFile(String filename,
                                                 int reqWidth, int reqHeight, ImageCache cache) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filename, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // If we're running on Honeycomb or newer, try to use inBitmap
    if (hasHoneycomb()) {
        addInBitmapOptions(options, cache);
    }

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(filename, options);
}
 
Example 4
Source File: BitmapLoader.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
public static Bitmap getBitmapFromFile(String path, int width, int height) {
    BitmapFactory.Options opts = null;
    if (path != null) {
        if (width > 0 && height > 0) {
            opts = new BitmapFactory.Options();
            opts.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, opts);
            final int minSideLength = Math.min(width, height);
            opts.inSampleSize = computeSampleSize(opts, minSideLength,
                    width * height);
            opts.inJustDecodeBounds = false;
            opts.inInputShareable = true;
            opts.inPurgeable = true;
        }
        return BitmapFactory.decodeFile(path, opts);
    } else return null;
}
 
Example 5
Source File: ImageUtility.java    From SquareCamera with MIT License 6 votes vote down vote up
public static Bitmap decodeSampledBitmapFromPath(String path, int reqWidth, int reqHeight) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    options.inMutable = true;
    options.inBitmap = BitmapFactory.decodeFile(path, options);

    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    options.inScaled = true;
    options.inDensity = options.outWidth;
    options.inTargetDensity = reqWidth * options.inSampleSize;

    options.inJustDecodeBounds = false;
    options.inPurgeable = true;
    options.inInputShareable = true;

    return BitmapFactory.decodeFile(path, options);
}
 
Example 6
Source File: LockScreenDeviceIconManager.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Will try to retrieve icon bitmap from cached directory
 * @param iconUrl the url where the icon was retrieved will be hashed and used to look up file location
 * @return bitmap of device icon or null if it fails to find the icon or read from shared preferences
 */
private Bitmap getFileFromCache(String iconUrl) {
    String iconHash = getMD5HashFromIconUrl(iconUrl);
    SharedPreferences sharedPref = this.context.getSharedPreferences(SDL_DEVICE_STATUS_SHARED_PREFS, Context.MODE_PRIVATE);
    String iconLastUpdatedTime = sharedPref.getString(iconHash, null);

    if (iconLastUpdatedTime != null) {
        Bitmap cachedIcon = BitmapFactory.decodeFile(this.context.getCacheDir() + "/" + STORED_ICON_DIRECTORY_PATH + "/" + iconHash);
        if(cachedIcon == null) {
            DebugTool.logError("Failed to get Bitmap from decoding file cache");
            clearIconDirectory();
            sharedPref.edit().clear().commit();
            return null;
        } else {
            return cachedIcon;
        }
    } else {
        DebugTool.logError("Failed to get shared preferences");
        return null;
    }
}
 
Example 7
Source File: ContactEditorFragment.java    From Linphone4Android with GNU General Public License v3.0 6 votes vote down vote up
private void editContactPicture(String filePath, Bitmap image) {
	if (image == null) {
		image = BitmapFactory.decodeFile(filePath);
	}

	Bitmap scaledPhoto;
	int size = getThumbnailSize();
	if (size > 0) {
		scaledPhoto = Bitmap.createScaledBitmap(image, size, size, false);
	} else {
		scaledPhoto = Bitmap.createBitmap(image);
	}
	image.recycle();

	ByteArrayOutputStream stream = new ByteArrayOutputStream();
	scaledPhoto.compress(Bitmap.CompressFormat.PNG , 0, stream);
	contactPicture.setImageBitmap(scaledPhoto);
	photoToAdd = stream.toByteArray();
}
 
Example 8
Source File: BitmapWorkerTask.java    From iBeebo with GNU General Public License v3.0 5 votes vote down vote up
public static Bitmap decodeSampledBitmapFromFile(String filename, int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filename, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filename, options);
    }
 
Example 9
Source File: FileUtil.java    From Audinaut with GNU General Public License v3.0 5 votes vote down vote up
public static Bitmap getAlbumArtBitmap(Context context, MusicDirectory.Entry entry, int size) {
    File albumArtFile = getAlbumArtFile(context, entry);
    if (albumArtFile.exists()) {
        final BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(albumArtFile.getPath(), opt);
        opt.inPurgeable = true;
        opt.inSampleSize = Util.calculateInSampleSize(opt, size, Util.getScaledHeight(opt.outHeight, opt.outWidth, size));
        opt.inJustDecodeBounds = false;

        Bitmap bitmap = BitmapFactory.decodeFile(albumArtFile.getPath(), opt);
        return bitmap == null ? null : getScaledBitmap(bitmap, size);
    }
    return null;
}
 
Example 10
Source File: NativeImageLoader.java    From mobile-manager-tool with MIT License 5 votes vote down vote up
/**
 * 根据View(主要是ImageView)的宽和高来获取图片的缩略图
 * @param path
 * @param viewWidth
 * @param viewHeight
 * @return
 */
private Bitmap decodeThumbBitmapForFile(String path, int viewWidth, int viewHeight){
    BitmapFactory.Options options = new BitmapFactory.Options();
    //设置为true,表示解析Bitmap对象,该对象不占内存
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);
    //设置缩放比例
    options.inSampleSize = computeScale(options, viewWidth, viewHeight);

    //设置为false,解析Bitmap对象加入到内存中
    options.inJustDecodeBounds = false;

    return BitmapFactory.decodeFile(path, options);
}
 
Example 11
Source File: ResultActivity.java    From superXingPostCard with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);

    initToolbar();

    resultIv = (ImageView) findViewById(R.id.result_iv);
    path = getIntent().getStringExtra("resultPath");
    bitmap = BitmapFactory.decodeFile(path);
    resultIv.setImageBitmap(bitmap);

}
 
Example 12
Source File: Utils.java    From ObservableScheduler with Apache License 2.0 5 votes vote down vote up
public static Bitmap getSmallBitmap(String filePath) {

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, 480, 800);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;

        Bitmap bm = BitmapFactory.decodeFile(filePath, options);
        if (bm == null) {
            return null;
        }
        int degree = readPictureDegree(filePath);
        bm = rotateBitmap(bm, degree);
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);

        } finally {
            try {
                if (baos != null)
                    baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bm;

    }
 
Example 13
Source File: FileUtils.java    From SmallGdufe-Android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 加载头像文件
 * @return Bitmap
 */
public static Bitmap loadAvatarBitmap(Context context) {
    File appDir = new File(getDiskCacheDir(context));
    File file = new File(appDir, AVATAR_FILE_NAME);
    if(!file.exists()){
        return null;
    }
    return BitmapFactory.decodeFile(file.getPath());
}
 
Example 14
Source File: GameUtil.java    From AiXiaoChu with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 通过执行screencap命令截图
 * 
 * @return
 */
public static Bitmap getScreenBitmapByProcess() {
	long s = System.currentTimeMillis();
	String path = SystemUtil.screenCap();
	Log.d("Screen", "screencap " + path);
	Bitmap bm = BitmapFactory.decodeFile(path);
	long e = System.currentTimeMillis();
	Log.i("Time", "getScreenBitmap use " + (e - s));
	return bm;
}
 
Example 15
Source File: LuBan.java    From ZhiHu-TopAnswer with Apache License 2.0 5 votes vote down vote up
public int[] getImageSize(String imagePath) {
    int[] res = new int[2];

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    options.inSampleSize = 1;
    BitmapFactory.decodeFile(imagePath, options);

    res[0] = options.outWidth;
    res[1] = options.outHeight;

    return res;
}
 
Example 16
Source File: AlbumImpl.java    From screenshot-tests-for-android with Apache License 2.0 5 votes vote down vote up
/** Returns the stored screenshot in the album, or null if no such test case exists. */
@Nullable
Bitmap getScreenshot(String name) throws IOException {
  if (getScreenshotFile(name) == null) {
    return null;
  }
  return BitmapFactory.decodeFile(getScreenshotFile(name).getAbsolutePath());
}
 
Example 17
Source File: UserIcon.java    From FCM-for-Mojo with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 尝试从文件读取头像。
 *
 * @param context Context
 * @param uid     QQ 号码或群号码
 * @param type    聊天类型
 * @return 头像 Bitmap,若不存在则返回 null
 */
@Nullable
public static Bitmap loadIcon(Context context, long uid, @ChatType int type) {
    if (type == ChatType.DISCUSS
            || type == ChatType.SYSTEM) {
        return null;
    }

    File file = getIconFile(context, uid, type);
    if (file.exists()) {
        return BitmapFactory.decodeFile(file.getAbsolutePath());
    }
    return null;
}
 
Example 18
Source File: GalleryScannerActor.java    From actor-platform with GNU Affero General Public License v3.0 4 votes vote down vote up
private void checkNew() {
    Log.d(TAG, "checkNew");
    cursor = getQuery();
    newMedia.clear();
    boolean firstCycle = true;
    while (cursor != null && cursor.moveToNext()) {
        absolutePathOfImage = cursor.getString(column_index_data);
        if (!listOfAllImages.contains(absolutePathOfImage)) {
            if (firstCycle) {
                firstCycle = false;
                bitmap = BitmapFactory.decodeFile(absolutePathOfImage);
                if (bitmap != null) {
                    bitmap.recycle();
                    newMedia.add(absolutePathOfImage);
                    Log.d(TAG, "checkNew - new media");

                } else {
                    Log.d(TAG, "checkNew - new media writing, breaking for wait");

                    break;
                }
            } else {
                newMedia.add(absolutePathOfImage);
            }

        } else {
            Log.d(TAG, "checkNew - found old media, break");

            break;
        }
    }
    if (newMedia.size() > 0) {
        listOfAllImages.addAll(0, newMedia);
        galleryVM.getGalleryMediaPath().change(new ArrayList<>(listOfAllImages));
        Log.d(TAG, "checkNew - new media add - " + newMedia.size());
    }
    if (visible) {
        Log.d(TAG, "checkNew - visible, schedule next check in " + CHECK_NEW_DELAY);
        schedule(new CheckNew(), CHECK_NEW_DELAY);
    }
}
 
Example 19
Source File: SkiaImageDecoder.java    From PictureSelector with Apache License 2.0 4 votes vote down vote up
@Override
public Bitmap decode(Context context, Uri uri) throws Exception {
    String uriString = uri.toString();
    BitmapFactory.Options options = new BitmapFactory.Options();
    Bitmap bitmap;
    options.inPreferredConfig = bitmapConfig;
    if (uriString.startsWith(RESOURCE_PREFIX)) {
        Resources res;
        String packageName = uri.getAuthority();
        if (context.getPackageName().equals(packageName)) {
            res = context.getResources();
        } else {
            PackageManager pm = context.getPackageManager();
            res = pm.getResourcesForApplication(packageName);
        }

        int id = 0;
        List<String> segments = uri.getPathSegments();
        int size = segments.size();
        if (size == 2 && segments.get(0).equals("drawable")) {
            String resName = segments.get(1);
            id = res.getIdentifier(resName, "drawable", packageName);
        } else if (size == 1 && TextUtils.isDigitsOnly(segments.get(0))) {
            try {
                id = Integer.parseInt(segments.get(0));
            } catch (NumberFormatException ignored) {
            }
        }

        bitmap = BitmapFactory.decodeResource(context.getResources(), id, options);
    } else if (uriString.startsWith(ASSET_PREFIX)) {
        String assetName = uriString.substring(ASSET_PREFIX.length());
        bitmap = BitmapFactory.decodeStream(context.getAssets().open(assetName), null, options);
    } else if (uriString.startsWith(FILE_PREFIX)) {
        bitmap = BitmapFactory.decodeFile(uriString.substring(FILE_PREFIX.length()), options);
    } else {
        InputStream inputStream = null;
        try {
            ContentResolver contentResolver = context.getContentResolver();
            inputStream = contentResolver.openInputStream(uri);
            bitmap = BitmapFactory.decodeStream(inputStream, null, options);
        } finally {
            if (inputStream != null) {
                try { inputStream.close(); } catch (Exception e) { /* Ignore */ }
            }
        }
    }
    if (bitmap == null) {
        throw new RuntimeException("Skia image region decoder returned null bitmap - image format may not be supported");
    }
    return bitmap;
}
 
Example 20
Source File: BitmapUtil.java    From o2oa with GNU Affero General Public License v3.0 2 votes vote down vote up
/**
 * 将图片按照指定的角度进行旋转
 *
 * @param path   需要旋转的图片的路径
 * @param degree 指定的旋转角度
 * @return 旋转后的图片
 */
public static Bitmap rotateBitmapByDegree(String path, int degree) {
    Bitmap bitmap = BitmapFactory.decodeFile(path);
    return rotateBitmapByDegree(bitmap,degree);
}