android.graphics.Bitmap.CompressFormat Java Examples

The following examples show how to use android.graphics.Bitmap.CompressFormat. 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: Util.java    From AssistantBySDK with Apache License 2.0 6 votes vote down vote up
public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
	ByteArrayOutputStream output = new ByteArrayOutputStream();
	bmp.compress(CompressFormat.PNG, 100, output);
	if (needRecycle) {
		bmp.recycle();
	}
	
	byte[] result = output.toByteArray();
	try {
		output.close();
	} catch (Exception e) {
		e.printStackTrace();
	}
	
	return result;
}
 
Example #2
Source File: QQUtil.java    From android-common-utils with Apache License 2.0 6 votes vote down vote up
public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
	ByteArrayOutputStream output = new ByteArrayOutputStream();
	bmp.compress(CompressFormat.PNG, 100, output);
	if (needRecycle) {
		bmp.recycle();
	}
	
	byte[] result = output.toByteArray();
	try {
		output.close();
	} catch (Exception e) {
		e.printStackTrace();
	}
	
	return result;
}
 
Example #3
Source File: OnekeyShareThemeImpl.java    From MyHearts with Apache License 2.0 6 votes vote down vote up
final ShareParams shareDataToShareParams(Platform plat) {
	if (plat == null || shareParamsMap == null) {
		toast("ssdk_oks_share_failed");
		return null;
	}

	try {
		String imagePath = R.forceCast(shareParamsMap.get("imagePath"));
		Bitmap viewToShare = R.forceCast(shareParamsMap.get("viewToShare"));
		if (TextUtils.isEmpty(imagePath) && viewToShare != null && !viewToShare.isRecycled()) {
			String path = R.getCachePath(plat.getContext(), "screenshot");
			File ss = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
			FileOutputStream fos = new FileOutputStream(ss);
			viewToShare.compress(CompressFormat.JPEG, 100, fos);
			fos.flush();
			fos.close();
			shareParamsMap.put("imagePath", ss.getAbsolutePath());
		}
	} catch (Throwable t) {
		t.printStackTrace();
		toast("ssdk_oks_share_failed");
		return null;
	}

	return new ShareParams(shareParamsMap);
}
 
Example #4
Source File: ImageFileUtils.java    From WeCenterMobile-Android with GNU General Public License v2.0 6 votes vote down vote up
public void saveBitmap(String fileName, Bitmap bitmap) throws IOException {
	fileName = fileName.replaceAll("[^\\w]", "");
	if (bitmap == null) {
		return;
	}
	String path = getStorageDirectory();
	File foldFile = new File(path);
	if (!foldFile.exists()) {
		foldFile.mkdir();
	}
	File file = new File(path + File.separator + fileName);
	file.createNewFile();
	FileOutputStream outputStream = new FileOutputStream(file);
	bitmap.compress(CompressFormat.JPEG, 80, outputStream);
	outputStream.flush();
	outputStream.close();
}
 
Example #5
Source File: X8ScreenShotManager.java    From FimiX8-RE with MIT License 6 votes vote down vote up
public static String saveScreenBitmap(Activity activity) {
    Bitmap bitmap = screenShot(activity);
    File file = new File(DirectoryPath.getX8LocalSar() + "/" + DateUtil.getStringByFormat(System.currentTimeMillis(), DateUtil.dateFormatYYMMDDHHMMSS) + ".jpeg");
    String s = "";
    try {
        if (!file.exists()) {
            if (file.getParentFile().exists()) {
                file.createNewFile();
            } else {
                file.getParentFile().mkdirs();
            }
        }
        if (save(bitmap, file, CompressFormat.JPEG, true)) {
            X8ToastUtil.showToast(activity, activity.getString(R.string.x8_ai_fly_sar_save_pic_tip), 0);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return s;
}
 
Example #6
Source File: CameraLauncher.java    From showCaseCordova with Apache License 2.0 6 votes vote down vote up
private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException {
    // Create an ExifHelper to save the exif data that is lost during compression
    String modifiedPath = getTempDirectoryPath() + "/modified.jpg";

    OutputStream os = new FileOutputStream(modifiedPath);
    bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
    os.close();

    // Some content: URIs do not map to file paths (e.g. picasa).
    String realPath = FileHelper.getRealPath(uri, this.cordova);
    ExifHelper exif = new ExifHelper();
    if (realPath != null && this.encodingType == JPEG) {
        try {
            exif.createInFile(realPath);
            exif.readExifData();
            if (this.correctOrientation && this.orientationCorrected) {
                exif.resetOrientation();
            }
            exif.createOutFile(modifiedPath);
            exif.writeExifData();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return modifiedPath;
}
 
Example #7
Source File: File2ByteArrayUtils.java    From BigApp_Discuz_Android with Apache License 2.0 6 votes vote down vote up
public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
	ByteArrayOutputStream output = new ByteArrayOutputStream();
	bmp.compress(CompressFormat.PNG, 100, output);
	if (needRecycle) {
		bmp.recycle();
	}
	
	byte[] result = output.toByteArray();
	try {
		output.close();
	} catch (Exception e) {
		e.printStackTrace();
	}
	
	return result;
}
 
Example #8
Source File: JpegIO.java    From Camdroid with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a compressed JPEG byte representation of this Pix.
 *
 * @param pixs A source pix image.
 * @param quality The quality of the compressed image. Valid range is 0-100.
 * @param progressive Whether to use progressive compression.
 * @return a compressed JPEG byte array representation of the Pix
 */
public static byte[] compressToJpeg(Pix pixs, int quality, boolean progressive) {
    if (pixs == null)
        throw new IllegalArgumentException("Source pix must be non-null");
    if (quality < 0 || quality > 100)
        throw new IllegalArgumentException("Quality must be between 0 and 100 (inclusive)");

    final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    final Bitmap bmp = WriteFile.writeBitmap(pixs);
    bmp.compress(CompressFormat.JPEG, quality, byteStream);
    bmp.recycle();

    final byte[] encodedData = byteStream.toByteArray();

    try {
        byteStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return encodedData;
}
 
Example #9
Source File: MyImageDownloader.java    From repay-android with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves {@link java.io.InputStream} of image by URI (image is accessed using {@link android.content.ContentResolver}).
 *
 * @param imageUri Image URI
 * @param extra    Auxiliary object which was passed to {@link com.nostra13.universalimageloader.core.DisplayImageOptions.Builder#extraForDownloader(Object)
 *                 DisplayImageOptions.extraForDownloader(Object)}; can be null
 * @return {@link java.io.InputStream} of image
 * @throws java.io.FileNotFoundException if the provided URI could not be opened
 */
protected InputStream getStreamFromContent(String imageUri, Object extra) throws FileNotFoundException {
	ContentResolver res = context.getContentResolver();

	Uri uri = Uri.parse(imageUri);
	if (isVideoUri(uri)) { // video thumbnail
		Long origId = Long.valueOf(uri.getLastPathSegment());
		Bitmap bitmap = MediaStore.Video.Thumbnails
				.getThumbnail(res, origId, MediaStore.Images.Thumbnails.MINI_KIND, null);
		if (bitmap != null) {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			bitmap.compress(CompressFormat.PNG, 0, bos);
			return new ByteArrayInputStream(bos.toByteArray());
		}
	} else if (imageUri.startsWith(CONTENT_CONTACTS_URI_PREFIX)) { // contacts photo
		return ContactsContract.Contacts.openContactPhotoInputStream(res, uri, true);
	}

	return res.openInputStream(uri);
}
 
Example #10
Source File: BaseImageDownloader.java    From Android-Universal-Image-Loader-Modify with Apache License 2.0 6 votes vote down vote up
/**
    * 根据文件的路径获取Video文件缩略图流数据
    * @param filePath
    * @return
    */
@TargetApi(Build.VERSION_CODES.FROYO)
private InputStream getVideoThumbnailStream(String filePath) {
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
           //创建缩略图
		Bitmap bitmap = ThumbnailUtils
				.createVideoThumbnail(filePath, MediaStore.Images.Thumbnails.FULL_SCREEN_KIND);
		if (bitmap != null) {
               //图像转成流传入
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			bitmap.compress(CompressFormat.PNG, 0, bos);
			return new ByteArrayInputStream(bos.toByteArray());
		}
	}
	return null;
}
 
Example #11
Source File: FileIOTools.java    From sctalk with Apache License 2.0 6 votes vote down vote up
public void writeBtimapToSD(
		Context ctx, String dir, String file, Bitmap img, CompressFormat format) 
		throws Exception{
	if(!hasSDCard()){
		return;
	}
	if (img == null) {
		return;
	}
	String path = dir + EncryptTools.instance().toMD5(file);
	File targetFile = new File(path);
	if(targetFile.exists()){
		return;
	}
	targetFile.createNewFile();
		FileOutputStream fo = new FileOutputStream(path);
	img.compress(format, 100, fo);
	fo.flush();
	fo.close();
}
 
Example #12
Source File: ImageUtil.java    From NIM_Android_UIKit with MIT License 6 votes vote down vote up
public static String makeThumbnail(File imageFile) {
    String thumbFilePath = StorageUtil.getWritePath(imageFile.getName(), StorageType.TYPE_THUMB_IMAGE);
    File thumbFile = AttachmentStore.create(thumbFilePath);
    if (thumbFile == null) {
        return null;
    }

    boolean result = scaleThumbnail(
            imageFile,
            thumbFile,
            MsgViewHolderThumbBase.getImageMaxEdge(),
            MsgViewHolderThumbBase.getImageMinEdge(),
            CompressFormat.JPEG,
            60);
    if (!result) {
        AttachmentStore.delete(thumbFilePath);
        return null;
    }

    return thumbFilePath;
}
 
Example #13
Source File: CameraLauncher.java    From cordova-android-chromeview with Apache License 2.0 6 votes vote down vote up
/**
 * Compress bitmap using jpeg, convert to Base64 encoded string, and return to JavaScript.
 *
 * @param bitmap
 */
public void processPicture(Bitmap bitmap) {
    ByteArrayOutputStream jpeg_data = new ByteArrayOutputStream();
    try {
        if (bitmap.compress(CompressFormat.JPEG, mQuality, jpeg_data)) {
            byte[] code = jpeg_data.toByteArray();
            byte[] output = Base64.encode(code, Base64.DEFAULT);
            String js_out = new String(output);
            this.callbackContext.success(js_out);
            js_out = null;
            output = null;
            code = null;
        }
    } catch (Exception e) {
        this.failPicture("Error compressing image.");
    }
    jpeg_data = null;
}
 
Example #14
Source File: ImageHelper.java    From fanfouapp-opensource with Apache License 2.0 6 votes vote down vote up
public static boolean writeToFile(final File file, final Bitmap bitmap) {
    if ((bitmap == null) || (file == null) || file.exists()) {
        return false;
    }
    BufferedOutputStream bos = null;
    try {
        bos = new BufferedOutputStream(new FileOutputStream(file),
                ImageHelper.OUTPUT_BUFFER_SIZE);
        return bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
    } catch (final IOException e) {
        if (AppContext.DEBUG) {
            Log.d(ImageHelper.TAG, "writeToFile:" + e.getMessage());
        }
    } finally {
        IOHelper.forceClose(bos);
    }
    return false;
}
 
Example #15
Source File: OnekeyShareThemeImpl.java    From LiuAGeAndroid with MIT License 6 votes vote down vote up
final ShareParams shareDataToShareParams(Platform plat) {
	if (plat == null || shareParamsMap == null) {
		toast("ssdk_oks_share_failed");
		return null;
	}

	try {
		String imagePath = ResHelper.forceCast(shareParamsMap.get("imagePath"));
		Bitmap viewToShare = ResHelper.forceCast(shareParamsMap.get("viewToShare"));
		if (TextUtils.isEmpty(imagePath) && viewToShare != null && !viewToShare.isRecycled()) {
			String path = ResHelper.getCachePath(plat.getContext(), "screenshot");
			File ss = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
			FileOutputStream fos = new FileOutputStream(ss);
			viewToShare.compress(CompressFormat.JPEG, 100, fos);
			fos.flush();
			fos.close();
			shareParamsMap.put("imagePath", ss.getAbsolutePath());
		}
	} catch (Throwable t) {
		t.printStackTrace();
		toast("ssdk_oks_share_failed");
		return null;
	}

	return new ShareParams(shareParamsMap);
}
 
Example #16
Source File: CameraLauncher.java    From reader with MIT License 6 votes vote down vote up
private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException {
    // Create an ExifHelper to save the exif data that is lost during compression
    String modifiedPath = getTempDirectoryPath() + "/modified.jpg";

    OutputStream os = new FileOutputStream(modifiedPath);
    bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
    os.close();

    // Some content: URIs do not map to file paths (e.g. picasa).
    String realPath = FileHelper.getRealPath(uri, this.cordova);
    ExifHelper exif = new ExifHelper();
    if (realPath != null && this.encodingType == JPEG) {
        try {
            exif.createInFile(realPath);
            exif.readExifData();
            if (this.correctOrientation && this.orientationCorrected) {
                exif.resetOrientation();
            }
            exif.createOutFile(modifiedPath);
            exif.writeExifData();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return modifiedPath;
}
 
Example #17
Source File: LocalRepoManager.java    From fdroidclient with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Extracts the icon from an APK and writes it to the repo as a PNG
 */
private void copyIconToRepo(Drawable drawable, String packageName, int versionCode) {
    Bitmap bitmap;
    if (drawable instanceof BitmapDrawable) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    } else {
        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
    }
    File png = getIconFile(packageName, versionCode);
    OutputStream out;
    try {
        out = new BufferedOutputStream(new FileOutputStream(png));
        bitmap.compress(CompressFormat.PNG, 100, out);
        out.close();
    } catch (Exception e) {
        Log.e(TAG, "Error copying icon to repo", e);
    }
}
 
Example #18
Source File: Util.java    From nono-android with GNU General Public License v3.0 6 votes vote down vote up
public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
	ByteArrayOutputStream output = new ByteArrayOutputStream();
	bmp.compress(CompressFormat.PNG, 100, output);
	if (needRecycle) {
		bmp.recycle();
	}
	
	byte[] result = output.toByteArray();
	try {
		output.close();
	} catch (Exception e) {
		e.printStackTrace();
	}
	
	return result;
}
 
Example #19
Source File: ImageUtil.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
public static File getScaledImageFileWithMD5(File imageFile, String mimeType) {
    String filePath = imageFile.getPath();

    if (!isInvalidPictureFile(mimeType)) {
        return null;
    }

    String tempFilePath = getTempFilePath(FileUtil.getExtensionName(filePath));
    Log.i("ImageUtil", "tempFilePath:"+tempFilePath);
    File tempImageFile = AttachmentStore.create(tempFilePath);
    if (tempImageFile == null) {
        return null;
    }

    CompressFormat compressFormat = CompressFormat.JPEG;
    // 压缩数值由第三方开发者自行决定
    int maxWidth = 720;
    int quality = 60;

    if (ImageUtil.scaleImage(imageFile, tempImageFile, maxWidth, compressFormat, quality)) {
        return tempImageFile;
    } else {
        return null;
    }
}
 
Example #20
Source File: OnekeyShareThemeImpl.java    From fingerpoetry-android with Apache License 2.0 6 votes vote down vote up
final ShareParams shareDataToShareParams(Platform plat) {
	if (plat == null || shareParamsMap == null) {
		toast("ssdk_oks_share_failed");
		return null;
	}

	try {
		String imagePath = R.forceCast(shareParamsMap.get("imagePath"));
		Bitmap viewToShare = R.forceCast(shareParamsMap.get("viewToShare"));
		if (TextUtils.isEmpty(imagePath) && viewToShare != null && !viewToShare.isRecycled()) {
			String path = R.getCachePath(plat.getContext(), "screenshot");
			File ss = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
			FileOutputStream fos = new FileOutputStream(ss);
			viewToShare.compress(CompressFormat.JPEG, 100, fos);
			fos.flush();
			fos.close();
			shareParamsMap.put("imagePath", ss.getAbsolutePath());
		}
	} catch (Throwable t) {
		t.printStackTrace();
		toast("ssdk_oks_share_failed");
		return null;
	}

	return new ShareParams(shareParamsMap);
}
 
Example #21
Source File: CameraLauncher.java    From reader with MIT License 6 votes vote down vote up
private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException {
    // Create an ExifHelper to save the exif data that is lost during compression
    String modifiedPath = getTempDirectoryPath() + "/modified.jpg";

    OutputStream os = new FileOutputStream(modifiedPath);
    bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
    os.close();

    // Some content: URIs do not map to file paths (e.g. picasa).
    String realPath = FileHelper.getRealPath(uri, this.cordova);
    ExifHelper exif = new ExifHelper();
    if (realPath != null && this.encodingType == JPEG) {
        try {
            exif.createInFile(realPath);
            exif.readExifData();
            if (this.correctOrientation && this.orientationCorrected) {
                exif.resetOrientation();
            }
            exif.createOutFile(modifiedPath);
            exif.writeExifData();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return modifiedPath;
}
 
Example #22
Source File: RxImageTool.java    From RxTools-master with Apache License 2.0 6 votes vote down vote up
/**
 * 按采样大小压缩
 *
 * @param src        源图片
 * @param sampleSize 采样率大小
 * @param recycle    是否回收
 * @return 按采样率压缩后的图片
 */
public static Bitmap compressBySampleSize(Bitmap src, int sampleSize, boolean recycle) {
    if (isEmptyBitmap(src)) return null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = sampleSize;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    src.compress(CompressFormat.JPEG, 100, baos);
    byte[] bytes = baos.toByteArray();
    if (recycle && !src.isRecycled()) src.recycle();
    return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
}
 
Example #23
Source File: OnekeyShareThemeImpl.java    From POCenter with MIT License 6 votes vote down vote up
final ShareParams shareDataToShareParams(Platform plat) {
	if (plat == null || shareParamsMap == null) {
		toast("ssdk_oks_share_failed");
		return null;
	}

	try {
		String imagePath = ResHelper.forceCast(shareParamsMap.get("imagePath"));
		Bitmap viewToShare = ResHelper.forceCast(shareParamsMap.get("viewToShare"));
		if (TextUtils.isEmpty(imagePath) && viewToShare != null && !viewToShare.isRecycled()) {
			String path = ResHelper.getCachePath(plat.getContext(), "screenshot");
			File ss = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
			FileOutputStream fos = new FileOutputStream(ss);
			viewToShare.compress(CompressFormat.JPEG, 100, fos);
			fos.flush();
			fos.close();
			shareParamsMap.put("imagePath", ss.getAbsolutePath());
		}
	} catch (Throwable t) {
		t.printStackTrace();
		toast("ssdk_oks_share_failed");
		return null;
	}

	return new ShareParams(shareParamsMap);
}
 
Example #24
Source File: PlayActivity.java    From AnimeTaste with MIT License 6 votes vote down vote up
@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1) {
    mDetailImageView.setImageBitmap(bitmap);
    mDetailPicture = bitmap;
    mLoadingGif.setVisibility(View.INVISIBLE);
    mPrePlayButton.setVisibility(View.VISIBLE);
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    mDetailPicture.compress(CompressFormat.JPEG, 100, bytes);

    File file = new File(mContext.getCacheDir(), "toshare.jpg");
    try {
        if (file.exists()) {
            file.delete();
        }
        file.createNewFile();
        FileOutputStream fo = new FileOutputStream(file);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #25
Source File: ImgUtil.java    From QiQuYing with Apache License 2.0 6 votes vote down vote up
public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
	ByteArrayOutputStream output = new ByteArrayOutputStream();
	bmp.compress(CompressFormat.PNG, 100, output);
	if (needRecycle) {
		bmp.recycle();
	}
	
	byte[] result = output.toByteArray();
	try {
		output.close();
	} catch (Exception e) {
		Log.e("bmpToByteArray", "bmpToByteArray error", e);
	}
	
	return result;
}
 
Example #26
Source File: Ocr.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
public static String ocr(Bitmap image) throws IOException {
	// ============ 读图片数据===========
	// DataInputStream in = new DataInputStream(new FileInputStream(img));
	ByteArrayOutputStream stream = new ByteArrayOutputStream();
	image.compress(CompressFormat.JPEG, 100, stream);
	stream.close();
	return ocr(stream.toByteArray());
}
 
Example #27
Source File: ChangeIconActivity.java    From emerald with GNU General Public License v3.0 5 votes vote down vote up
public void saveCustomIcon(String appComponent, String iconComponent) {
	File customIconFile = MyCache.getCustomIconFile(this, appComponent);
	Bitmap customBitmap = LauncherApp.getInstance().getIconPackManager().getBitmap(iconComponent);
	if (customIconFile != null) {
		try {
			// save icon in cache
			FileOutputStream out = new FileOutputStream(customIconFile);
			customBitmap.compress(CompressFormat.PNG, 100, out);
			out.close();
		} catch (Exception e) {
			customIconFile.delete();
		}
	}
}
 
Example #28
Source File: ImageUtils.java    From Android-UtilCode with Apache License 2.0 5 votes vote down vote up
/**
 * 按质量压缩
 *
 * @param src     源图片
 * @param quality 质量
 * @param recycle 是否回收
 * @return 质量压缩后的图片
 */
public static Bitmap compressByQuality(Bitmap src, @IntRange(from = 0, to = 100) int quality, boolean recycle) {
    if (isEmptyBitmap(src)) return null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    src.compress(Bitmap.CompressFormat.JPEG, quality, baos);
    byte[] bytes = baos.toByteArray();
    if (recycle && !src.isRecycled()) src.recycle();
    return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
 
Example #29
Source File: Global.java    From Favorite-Android-Client with Apache License 2.0 5 votes vote down vote up
public static Uri getImageUri(Context inContext, Bitmap inImage) {
	ByteArrayOutputStream bytes = new ByteArrayOutputStream();
	inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
	String path = Images.Media.insertImage(inContext.getContentResolver(),
			inImage, "Title", null);
	return Uri.parse(path);
}
 
Example #30
Source File: ImageUtils.java    From Android-UtilCode with Apache License 2.0 5 votes vote down vote up
/**
 * 按质量压缩
 *
 * @param src         源图片
 * @param maxByteSize 允许最大值字节数
 * @param recycle     是否回收
 * @return 质量压缩压缩过的图片
 */
public static Bitmap compressByQuality(Bitmap src, long maxByteSize, boolean recycle) {
    if (isEmptyBitmap(src) || maxByteSize <= 0) return null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int quality = 100;
    src.compress(CompressFormat.JPEG, quality, baos);
    while (baos.toByteArray().length > maxByteSize && quality > 0) {
        baos.reset();
        src.compress(CompressFormat.JPEG, quality -= 5, baos);
    }
    if (quality < 0) return null;
    byte[] bytes = baos.toByteArray();
    if (recycle && !src.isRecycled()) src.recycle();
    return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}