In this question ImageView will reset to original state after rotating, I know how to rotate an imageView and keep it after rotating.
But now, I need to do some scaling based on the rotated image. When scaling, the image will reset to original one before rotating, this is not what I want.
So I want to get the bitmap from ImageView after rotating, and let the ImageView use the new one. The question is:
How to get the bitmap from ImageView after rotating?
To get bitmap from imageview:
imageview.buildDrawingCache();
Bitmap bm=imageview.getDrawingCache();
To save it in a file:
OutputStream fOut = null;
Uri outputFileUri;
try {
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "folder_name" + File.separator);
root.mkdirs();
File sdImageMainDirectory = new File(root, "myPicName.jpg");
outputFileUri = Uri.fromFile(sdImageMainDirectory);
fOut = new FileOutputStream(sdImageMainDirectory);
} catch (Exception e) {
Toast.makeText(this, "Error occured. Please try again later.",
Toast.LENGTH_SHORT).show();
}
try {
bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
}
Related
I want to load an image from a file path which I have already defined, but don't want to instantiate another file object since I have already defined the path when saving the image.
I have tried retrieving the image with:
Picasso.with(this).load(filename).into(image_tv);
This is my code for saving the image;
Bitmap bitMapImg;
void saveImage() {
File filename;
try {
String path =
Environment.getExternalStorageDirectory().toString();
new File(path + "/folder/subfolder").mkdirs();
filename = new
File(path+"/folder/subfolder/image.jpg");
FileOutputStream out = new FileOutputStream(filename);
bitMapImg.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Use this
Picasso.with(context).load(Uri.parse("file://" + yourFilePath).into(imageView);
Ok i'm completely editing this post... I have made it so that I can save the file path to my data base. this works and is saved as /storage/emulated/0/1508blah blah.jpg . Now i cannot get my code to read this item back into a picture.
imagePhoto = (ImageView)findViewById(R.id.detail_recipe_image);
Toast.makeText(this, recipe.image, Toast.LENGTH_SHORT).show();
Bitmap bmp = BitmapFactory.decodeFile(String.valueOf(recipe.image));
imagePhoto.setImageBitmap(bmp);
am I missing something here? cause the Toast Is reading the recipe.image just fine and is displaying the path. why Is the rest not displaying the image?
Storage Code
private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
String picturePath = destination.toString();
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
textImagePath.setText(picturePath.toString());
ImageView img = (ImageView)findViewById(R.id.addphotoview);
img.setImageBitmap(thumbnail);
}
Adding in the files paths seem to be the best solution to the problem i am having so that you #ModularSynth for your help with this. Always making sure all the info is your code to make the file paths work helps.
I am trying to create bitmap of all view.but I am not able to create bitmap of all view,please help me to resolve this problem.
public Bitmap overlay(FrameLayout frame_layout){
frame_layout.setDrawingCacheEnabled(true);
frame_layout.buildDrawingCache();
return frame_layout.getDrawingCache();
}
public void saveImage(){
Bitmap bitmap=overlay(print_layout);
try{
String path = Environment.getExternalStorageDirectory().toString()+ File.separator+"bitmap";
File path_file=new File(path);
path_file.mkdirs();
OutputStream fOut = null;
File file = new File(path+ File.separator+"print.png"); // the File to save to
fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate
fOut.flush();
fOut.close();
Log.d("sun","saved");
}
catch (Exception e){
Log.d("sun",e.toString());
}
}
I want to replace the bitmap image after rotating it, from the original image stored any where in the mobile. I have rotated an image at 90 degree but I'm unable to replace it from that original image.
MediaStore.Images.Media.insertImage(getContentResolver(), rotatedBitmapImage, imageName, "");
I have used above code but it is storing image in DCIM -> camera folder. But I want to replace it from URI of the original image.
I have got answer of this question. Inspite of using above code I have used:
OutputStream fOut = null;
File file = new File(imagePath);
try {
fOut = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
resizeBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
//---------------Used Media Scanner-----------
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri
.parse("file://" + Environment.getExternalStorageDirectory())));
The idea of my app is to capture image from camera then crop specified area from it.
The problem :
When i save the cropped image in my sd card for the first time to launch the app, it saved properly. but when run my app one more time and take image then crop it. when save it the first image that take and crop at first time appear in the sd card not the current one.
This is my code for save images:
public static void save(Activity activity, Bitmap bm, String name) {
OutputStream outStream = null;
File externalFilesDir = activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File outFile = new File(externalFilesDir, "IDOCR" + File.separator + "Numbers");
if (!outFile.exists())
outFile.mkdirs();
File number = new File(outFile, name + ".PNG");
//if (number.exists())
// number.delete();
try {
//outStream = new FileOutputStream(new File(path));
outStream = new FileOutputStream(number);
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
bm.recycle();
System.gc();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Maybe if you are trying to overwrite the previous version of the file, you should first delete the previous one...
You can add:
if (!outFile.exists())
outFile.mkdirs();
else {
outFile.delete();
outFile.createNewFile();
}