List of usage examples for android.graphics BitmapFactory decodeFile
public static Bitmap decodeFile(String pathName, Options opts)
From source file:com.abc.driver.TruckActivity.java
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CellSiteConstants.TAKE_PICTURE || requestCode == CellSiteConstants.PICK_PICTURE) { Uri uri = null;/*from w w w. j a v a2s .com*/ if (requestCode == CellSiteConstants.TAKE_PICTURE) { uri = imageUri; } else if (requestCode == CellSiteConstants.PICK_PICTURE) { uri = data.getData(); } String[] filePathColumn = { MediaStore.Images.Media.DATA }; Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null); if (cursor != null && cursor.moveToFirst()) { int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String filePath = cursor.getString(columnIndex); cursor.close(); Log.d(TAG, "filePath =" + filePath); Log.d(TAG, "uri=" + uri.toString()); imageUri = Uri.fromFile(new File(filePath)); } else // { if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { Toast.makeText(this, R.string.sdcard_occupied, Toast.LENGTH_SHORT).show(); return; } String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); File tmpFile = new File(app.regUserPath + File.separator + "IMG_" + timeStamp + ".png"); File srcFile = new File(uri.getPath()); if (srcFile.exists()) { try { Utils.copyFile(srcFile, tmpFile); app.getUser().getMyTruck().setLicenseImageUrl(tmpFile.getAbsolutePath()); } catch (Exception e) { Toast.makeText(this, R.string.create_tmp_file_fail, Toast.LENGTH_SHORT).show(); return; } } else { Log.d(TAG, "Logic error, should not come to here"); Toast.makeText(this, R.string.file_not_found, Toast.LENGTH_SHORT).show(); return; } imageUri = Uri.fromFile(tmpFile); } // doCrop(); Bitmap tmpBmp = BitmapFactory.decodeFile(imageUri.getPath(), null); Bitmap scaledBmp = Bitmap.createScaledBitmap(tmpBmp, IMAGE_WIDTH, IMAGE_HEIGHT, false); mTLPiv.setImageBitmap(scaledBmp); isPortraitChanged = true; Log.d(TAG, "onActivityResult PICK_PICTURE"); mUpdateImageTask = new UpdateImageTask(); mUpdateImageTask.execute("" + app.getUser().getId(), "" + app.getUser().getMyTruck().getTruckId(), Utils.bitmap2String(scaledBmp), CellSiteConstants.UPDATE_TRUCK_LICENSE_URL); } else if (requestCode == CellSiteConstants.CROP_PICTURE) { Log.d(TAG, "crop picture"); // processFile(); if (data != null) { Bundle extras = data.getExtras(); Bitmap photo = extras.getParcelable("data"); trcukLicenseBmp = photo; mTLPiv.setImageBitmap(trcukLicenseBmp); mUpdateImageTask = new UpdateImageTask(); mUpdateImageTask.execute("" + app.getUser().getId(), "" + app.getUser().getMyTruck().getTruckId(), Utils.bitmap2String(trcukLicenseBmp), CellSiteConstants.UPDATE_DRIVER_LICENSE_URL); isPortraitChanged = true; } } else if (requestCode == CellSiteConstants.TAKE_PICTURE2 || requestCode == CellSiteConstants.PICK_PICTURE2) { Uri uri = null; if (requestCode == CellSiteConstants.TAKE_PICTURE2) { uri = imageUri; } else if (requestCode == CellSiteConstants.PICK_PICTURE2) { uri = data.getData(); } String[] filePathColumn = { MediaStore.Images.Media.DATA }; Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null); if (cursor != null && cursor.moveToFirst()) { int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String filePath = cursor.getString(columnIndex); cursor.close(); Log.d(TAG, "filePath =" + filePath); Log.d(TAG, "uri=" + uri.toString()); imageUri = Uri.fromFile(new File(filePath)); } else // { if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { Toast.makeText(this, R.string.sdcard_occupied, Toast.LENGTH_SHORT).show(); return; } String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); File tmpFile = new File(app.regUserPath + File.separator + "IMG_" + timeStamp + ".png"); File srcFile = new File(uri.getPath()); if (srcFile.exists()) { try { Utils.copyFile(srcFile, tmpFile); app.getUser().getMyTruck().setPhotoImageUrl(tmpFile.getAbsolutePath()); } catch (Exception e) { Toast.makeText(this, R.string.create_tmp_file_fail, Toast.LENGTH_SHORT).show(); return; } } else { Log.d(TAG, "Logic error, should not come to here"); Toast.makeText(this, R.string.file_not_found, Toast.LENGTH_SHORT).show(); return; } imageUri = Uri.fromFile(tmpFile); } // doCrop(); Bitmap tmpBmp = BitmapFactory.decodeFile(imageUri.getPath(), null); Bitmap scaledBmp = Bitmap.createScaledBitmap(tmpBmp, IMAGE_WIDTH, IMAGE_HEIGHT, false); mTPiv.setImageBitmap(scaledBmp); // s isChanged = true; mUpdateImageTask = new UpdateImageTask(); mUpdateImageTask.execute("" + app.getUser().getId(), "" + app.getUser().getMyTruck().getTruckId(), Utils.bitmap2String(scaledBmp), CellSiteConstants.UPDATE_TRUCK_PHOTO_URL); } else if (requestCode == CellSiteConstants.UPDATE_TRUCK_MOBILE_REQUSET) { Log.d(TAG, "mobile changed"); if (app.getUser().getMyTruck().getMobileNum() == null) { mTMtv.setText(app.getUser().getMobileNum()); app.getUser().getMyTruck().setMobileNum(app.getUser().getMobileNum()); } else { mTMtv.setText(app.getUser().getMyTruck().getMobileNum()); } } }
From source file:com.abc.driver.PersonalActivity.java
private void doCrop() { Log.d(TAG, "doCrop()"); final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>(); Intent intent = new Intent("com.android.camera.action.CROP"); intent.setType("image/*"); List<ResolveInfo> list = this.getPackageManager().queryIntentActivities(intent, 0); int size = list.size(); if (size == 0) { Log.d(TAG, " Crop activity is not found. List size is zero."); Bitmap tmpBmp = BitmapFactory.decodeFile(imageUri.getPath(), null); app.setPortaritBitmap(Bitmap.createScaledBitmap(tmpBmp, CellSiteConstants.IMAGE_WIDTH, CellSiteConstants.IMAGE_HEIGHT, false)); mUserPortraitIv.setImageBitmap(app.getPortaritBitmap()); isPortraitChanged = true;//from w w w . j a v a 2s . c o m Log.d(TAG, "set bitmap"); return; } else { Log.d(TAG, "found the crop activity."); intent.setData(imageUri); intent.putExtra("outputX", CellSiteConstants.IMAGE_WIDTH); intent.putExtra("outputY", CellSiteConstants.IMAGE_HEIGHT); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("scale", true); intent.putExtra("return-data", true); if (size == 1) { Log.d(TAG, "Just one as choose it as crop activity."); Intent i = new Intent(intent); ResolveInfo res = list.get(0); i.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); startActivityForResult(i, CellSiteConstants.CROP_PICTURE); } else { Log.d(TAG, "More that one activity for crop is found . will chooose one"); for (ResolveInfo res : list) { final CropOption co = new CropOption(); co.title = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo); co.icon = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo); co.appIntent = new Intent(intent); co.appIntent .setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); cropOptions.add(co); } CropOptionAdapter adapter = new CropOptionAdapter(getApplicationContext(), cropOptions); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Choose Crop App"); builder.setAdapter(adapter, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { startActivityForResult(cropOptions.get(item).appIntent, CellSiteConstants.CROP_PICTURE); } }); builder.setOnCancelListener(new DialogInterface.OnCancelListener() { // @Override public void onCancel(DialogInterface dialog) { if (imageUri != null) { getContentResolver().delete(imageUri, null, null); imageUri = null; isPortraitChanged = false; } } }); AlertDialog alert = builder.create(); alert.show(); } } }
From source file:com.android.aft.AFCoreTools.ImageDownloader.java
public Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) { final BitmapFactory.Options options = createBitmapFactoryOptions(); if (reqWidth == 0 && reqHeight == 0) { return BitmapFactory.decodeFile(path, options); } else {/*ww w . ja va 2 s . co m*/ // First decode with inJustDecodeBounds=true to check dimensions options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); DebugTools.d("Decode file with sample size : " + options.inSampleSize); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(path, options); } }
From source file:com.grass.caishi.cc.activity.AdAddActivity.java
public void addToListImage(final String path) { if (path != null) { dialog.setMessage("..."); dialog.show();/* ww w.j a v a 2 s . co m*/ new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub File file = new File(path); imageUrl = path; FileInputStream fis = null; try { int quality = 90; fis = new FileInputStream(file); int size = fis.available() / 1024; if (size > 150) { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; resizeBmp = BitmapFactory.decodeFile(path, opts); int pic_width = opts.outWidth; int pic_height = opts.outHeight; int be = 1; if (pic_height > pic_width && pic_width > 1500) { be = (int) Math.rint(pic_width / 1440.0); } else if (pic_height < pic_width && pic_height > 2600) { be = (int) Math.rint(pic_height / 2560.0); } if (be <= 0) be = 1; opts.inSampleSize = be;// opts.inJustDecodeBounds = false; resizeBmp = BitmapFactory.decodeFile(path, opts); pic_width = opts.outWidth; pic_height = opts.outHeight; File dirFile = new File(Environment.getExternalStorageDirectory(), Constant.CACHE_DIR_IMAGE); if (!dirFile.exists()) { dirFile.mkdirs(); } int drive_width = MetricsUnit.getWidth(AdAddActivity.this); int drive_height = MetricsUnit.getHeight(AdAddActivity.this); float my_width = (float) drive_width; float my_height = (float) drive_height; double doo = 0.0; if (pic_width > my_width) { doo = (double) my_width / (double) pic_width; my_height = (int) (pic_height * doo); } int c_h = 0; if (my_height > drive_height) { c_h = (int) ((pic_height - my_height) / 2); if (c_h < 0) { c_h = 0; } } Matrix matrix = new Matrix(); matrix.postScale(my_width / pic_width, my_height / pic_height); resizeBmp = Bitmap.createBitmap(resizeBmp, 0, c_h, pic_width, pic_height, matrix, true); File jpegTrueFile = new File(dirFile, "img_" + System.currentTimeMillis() + ".jpg"); FileOutputStream fileOutputStream = new FileOutputStream(jpegTrueFile); resizeBmp.compress(CompressFormat.JPEG, quality, fileOutputStream); imageUrl = jpegTrueFile.getAbsolutePath(); // MyApplication.getInstance().setMatchImages(jpegTrueFile.getAbsolutePath()); //bit.recycle(); } else { resizeBmp = BitmapFactory.decodeStream(fis); } runOnUiThread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub if (select_type == 1) { path_logo = imageUrl; img_logo.setImageBitmap(resizeBmp); } else if (select_type == 2) { path_price = imageUrl; img_price.setImageBitmap(resizeBmp); } else if (select_type == 3) { MyApplication.getInstance().setMatchImages(imageUrl); adapter.notifyDataSetChanged(); } if (dialog.isShowing()) { dialog.dismiss(); } } }); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); } }
From source file:com.pongme.utils.ImageDownloader.java
private Bitmap getBitmapFromFile(File file, int width, int height) { BitmapFactory.Options options = new BitmapFactory.Options(); if (width != 0 && height != 0) { options.inJustDecodeBounds = true; BitmapFactory.decodeFile(file.getAbsolutePath(), options); float heightRatio = (float) options.outHeight / (float) height; float widthRatio = (float) options.outWidth / (float) width; float maxRatio = Math.max(widthRatio, heightRatio); if (maxRatio > 6) options.inSampleSize = 8;// w w w . j a v a2 s.c o m else if (maxRatio > 3) options.inSampleSize = 4; else if (maxRatio > 1.5f) options.inSampleSize = 2; options.inJustDecodeBounds = false; } options.inTempStorage = new byte[16 * 1024]; Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options); return bitmap; }
From source file:com.cloverstudio.spika.CameraCropActivity.java
protected void onPhotoTaken(String path) { String fileName = Uri.parse(path).getLastPathSegment(); mFilePath = CameraCropActivity.this.getExternalCacheDir() + "/" + fileName; if (!path.equals(mFilePath)) { copy(new File(path), new File(mFilePath)); }// w ww .ja v a2 s . com new AsyncTask<String, Void, byte[]>() { boolean loadingFailed = false; @Override protected byte[] doInBackground(String... params) { try { if (params == null) return null; File f = new File(params[0]); ExifInterface exif = new ExifInterface(f.getPath()); int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); int angle = 0; if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { angle = 90; } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { angle = 180; } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { angle = 270; } Matrix mat = new Matrix(); mat.postRotate(angle); BitmapFactory.Options optionsMeta = new BitmapFactory.Options(); optionsMeta.inJustDecodeBounds = true; BitmapFactory.decodeFile(f.getAbsolutePath(), optionsMeta); BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = BitmapManagement.calculateInSampleSize(optionsMeta, 640, 640); options.inPurgeable = true; options.inInputShareable = true; mBitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, options); mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), mat, true); _scaleBitmap(); return null; } catch (Exception ex) { loadingFailed = true; finish(); } return null; } @Override protected void onPostExecute(byte[] result) { super.onPostExecute(result); if (null != mBitmap) { mImageView.setImageBitmap(mBitmap); mImageView.setScaleType(ScaleType.MATRIX); translateMatrix.setTranslate(-(mBitmap.getWidth() - crop_container_size) / 2f, -(mBitmap.getHeight() - crop_container_size) / 2f); mImageView.setImageMatrix(translateMatrix); matrix = translateMatrix; } } }.execute(mFilePath); }
From source file:com.abiansoftware.lib.reader.AbianReaderData.java
public static Bitmap decodeSampledBitmapFromFile(String filePath, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true;/*from w w w. ja v a 2 s. co m*/ BitmapFactory.decodeFile(filePath, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(filePath, options); }
From source file:com.raspi.chatapp.ui.chatting.SendImageFragment.java
/** * saves the image in own image folder/*from w w w . j a v a 2 s . c o m*/ * * @param sourcePath the path of the image the user selected * @param destFile the file where it should be saved * @throws IOException */ private void saveImage(String sourcePath, File destFile) throws IOException { // get the output stream OutputStream out = new FileOutputStream(destFile); // create the options final BitmapFactory.Options options = new BitmapFactory.Options(); options.inDensity = 96; Bitmap image = BitmapFactory.decodeFile(sourcePath, options); // and compress the image into the file image.compress(Bitmap.CompressFormat.JPEG, 42, out); out.close(); }
From source file:com.abc.driver.TruckActivity.java
private void doCrop() { Log.d(TAG, "doCrop()"); final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>(); Intent intent = new Intent("com.android.camera.action.CROP"); intent.setType("image/*"); List<ResolveInfo> list = this.getPackageManager().queryIntentActivities(intent, 0); int size = list.size(); if (size == 0) { Log.d(TAG, " Crop activity is not found. List size is zero."); Bitmap tmpBmp = BitmapFactory.decodeFile(imageUri.getPath(), null); trcukLicenseBmp = Bitmap.createScaledBitmap(tmpBmp, IMAGE_WIDTH, IMAGE_HEIGHT, false); mTLPiv.setImageBitmap(trcukLicenseBmp); isPortraitChanged = true;/*from www . java 2 s. c o m*/ Log.d(TAG, "set bitmap"); return; } else { Log.d(TAG, "found the crop activity."); intent.setData(imageUri); intent.putExtra("outputX", IMAGE_WIDTH); intent.putExtra("outputY", IMAGE_HEIGHT); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("scale", true); intent.putExtra("return-data", true); if (size == 1) { Log.d(TAG, "Just one as choose it as crop activity."); Intent i = new Intent(intent); ResolveInfo res = list.get(0); i.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); startActivityForResult(i, CellSiteConstants.CROP_PICTURE); } else { Log.d(TAG, "More that one activity for crop is found . will chooose one"); for (ResolveInfo res : list) { final CropOption co = new CropOption(); co.title = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo); co.icon = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo); co.appIntent = new Intent(intent); co.appIntent .setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); cropOptions.add(co); } CropOptionAdapter adapter = new CropOptionAdapter(getApplicationContext(), cropOptions); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Choose Crop App"); builder.setAdapter(adapter, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { startActivityForResult(cropOptions.get(item).appIntent, CellSiteConstants.CROP_PICTURE); } }); builder.setOnCancelListener(new DialogInterface.OnCancelListener() { // @Override public void onCancel(DialogInterface dialog) { if (imageUri != null) { getContentResolver().delete(imageUri, null, null); imageUri = null; isPortraitChanged = false; } } }); AlertDialog alert = builder.create(); alert.show(); } } }
From source file:com.snappy.CameraCropActivity.java
protected void onPhotoTaken(final String path) { String fileName = Uri.parse(path).getLastPathSegment(); mFilePath = CameraCropActivity.this.getExternalCacheDir() + "/" + fileName; if (!path.equals(mFilePath)) { copy(new File(path), new File(mFilePath)); }/* w w w.j a va2 s. co m*/ new AsyncTask<String, Void, byte[]>() { boolean loadingFailed = false; @Override protected byte[] doInBackground(String... params) { try { if (!path.equals(mFilePath)) { copy(new File(path), new File(mFilePath)); } if (params == null) return null; File f = new File(params[0]); ExifInterface exif = new ExifInterface(f.getPath()); int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); int angle = 0; if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { angle = 90; } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { angle = 180; } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { angle = 270; } Matrix mat = new Matrix(); mat.postRotate(angle); BitmapFactory.Options optionsMeta = new BitmapFactory.Options(); optionsMeta.inJustDecodeBounds = true; BitmapFactory.decodeFile(f.getAbsolutePath(), optionsMeta); BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = BitmapManagement.calculateInSampleSize(optionsMeta, 640, 640); options.inPurgeable = true; options.inInputShareable = true; mBitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, options); mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), mat, true); _scaleBitmap(); return null; } catch (Exception ex) { loadingFailed = true; finish(); } return null; } @Override protected void onPostExecute(byte[] result) { super.onPostExecute(result); if (null != mBitmap) { mImageView.setImageBitmap(mBitmap); mImageView.setScaleType(ScaleType.MATRIX); translateMatrix.setTranslate(-(mBitmap.getWidth() - crop_container_size) / 2f, -(mBitmap.getHeight() - crop_container_size) / 2f); mImageView.setImageMatrix(translateMatrix); matrix = translateMatrix; } } }.execute(mFilePath); }