testButton.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v)
{
imageView.setImageBitmap(Bitmap);
imageView.buildDrawingCache();
Bitmap bm =imageView.getDrawingCache();
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Punch");
imagesFolder.mkdirs();
String fileName = "image" + ".jpg";
File output = new File(imagesFolder, fileName);
Uri uriSavedImage = Uri.fromFile(output);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
OutputStream fos = null;
try {
fos = getContentResolver().openOutputStream(uriSavedImage);
bm.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{}
}
});
First i retrieved an image from sd card to image view of resolution (640 x 480). Then again i saved the image from image view to sd card. But the image saved is of resolution is (188x113). Can anyone suggest how i can save with the same resolution. Any suggestions will be appreciable.
Try this code :
BitmapDrawable btmpDr = (BitmapDrawable) ivPic.getDrawable();
Bitmap bmp = btmpDr.getBitmap();
/*File sdCardDirectory = Environment.getExternalStorageDirectory();*/
try
{
File sdCardDirectory = new File(Environment.getExternalStorageDirectory() + File.separator + "MeeguImages");
sdCardDirectory.mkdirs();
imageNameForSDCard = "image_" + String.valueOf(random.nextInt(1000)) + System.currentTimeMillis() + ".jpg";
File image = new File(sdCardDirectory, imageNameForSDCard);
FileOutputStream outStream;
outStream = new FileOutputStream(image);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
//Refreshing SD card
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
}
catch (Exception e)
{
e.printStackTrace();
Toast.makeText(ViewImage.this, "Image could not be saved : Please ensure you have SD card installed " +
"properly", Toast.LENGTH_LONG).show();
}
bm.compress(CompressFormat.JPEG, 100, fos);
remove this line
Solved, This is how i achieved to save image from ImageView
/*Variable which holds Image*/
{ImageView ivBanner = "Initialise It :)";
FileOutputStream fileOutputStream = openFileOutput("ImageName" + ".jpg", MODE_PRIVATE);
Bitmap bitmap = convertToBitMap(ivBanner.getDrawable(),ivBanner.getWidth(),ivBanner.getHeight());
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fileOutputStream);
File file = getFileStreamPath("ImageName" + ".jpg");
File f = file.getAbsoluteFile();
/*Utilise your path whatever way you want*/
String localPath = f.getAbsolutePath();}
/* Covert Drawable to Bitmap*/
private Bitmap convertToBitMap(Drawable drawable, int width, int height) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0,0,width,height);
drawable.draw(canvas);
return bitmap;
}
Related
I have an application that removes the image background and set a new blue background colour and save it but when I open an image it shows black background.
How I can save this with a blue background ?.
imageView.setBackgroundColor(ContextCompat.getColor(this, R.color.blue));
BitmapDrawable draw = (BitmapDrawable) imageView.getDrawable();
BitmapDrawable draw = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = draw.getBitmap();
FileOutputStream outStream = null;
File file = Environment.getExternalStorageDirectory();
File dir = new File(file.getAbsolutePath() + "/Passport Photo123");
dir.mkdirs();
String fileName = String.format("%d.jpg", System.currentTimeMillis());
File outFile = new File(dir, fileName);
try {
outStream = new FileOutputStream(outFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
try {
outStream.flush();
outStream.close();
Toast.makeText(this, "Photo Saved at: " + outFile, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
Try this line of code :-
imageView.setBackgroundColor(Color.rgb(100, 100, 50));
If it not works than the problem must be elsewhere.
I am trying to store my image from imageView in bitmap, so that I can store it in the gallery of the android device. Every time I save an image, the background of the imageView is not stored. What am I missing?
Here is my code:
ImageView imageView = (ImageView) findViewById(R.id.img);
imageView.setBackgroundResource(R.drawable.img1);
BitmapDrawable draw = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = draw.getBitmap();
Code to store the image into the gallery is:
FileOutputStream outStream = null;
File dir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"MyAlbum");
if (!dir.exists()) {
if (!dir.mkdirs()) {
Log.d("MyAlbum", "failed to create directory");
Toast.makeText(MainActivity.this, "Failed to make directory", Toast.LENGTH_SHORT).show();
}
}
String fileName = String.format("%d.jpg", System.currentTimeMillis());
File outFile = new File(dir, fileName);
try {
outStream = new FileOutputStream(outFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
Toast.makeText(getApplicationContext(), "PICTURE SAVED", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(dir));
sendBroadcast(intent);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
You can take a screenshot of this view (ImageView) in this case, it will simply take what's drawn on this view at this moment and turn it into a bitmap you can save.
Answer is mentioned here already.
The magical part is that
ImageView yourImageView = .. // Get reference it to your view.
yourImageView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(yourImageView.getDrawingCache());
yourImageView.setDrawingCacheEnabled(false);
Ta-da you can use your snapshot btimap.
you can try this out,
private void saveImageToStorage(Bitmap finalBitmap, String image_name) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root);
myDir.mkdirs();
String fname = "Image-" + image_name+ ".jpg";
File file = new File(myDir, fname);
if (file.exists()) file.delete();
Log.i("LOAD", root + fname);
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Happy coding :-)
I have made an app that uses the filter image and save in sdcard it's working good but I want to save image in two resolution high and low, I have never tried on resolution, Any one one can help that how to save in these both resolution? My code is below
private void saveBitmap(Bitmap bmp, String fileName, int resolution, String resolutionQuality) {
// File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + fileName + ".png");
File f = new File(Environment.getExternalStorageDirectory() + "FiltureImages");
if (!f.exists()) {
File wallpaperDirectory = new File("/sdcard/FiltureImages/");
wallpaperDirectory.mkdirs();
}
File file = new File(new File("/sdcard/FiltureImages/"), fileName + resolutionQuality + ".png");
if (file.exists()) {
file.delete();
}
try {
fos = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, resolution, fos);
Toast.makeText(mActivity, "Image save successfully", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
Call like this saveBitmap(bm, "high", "low", 100, 0, "quality");
Quality range from 0 -100
Note: change Bitmap.CompressFormat.PNG to Bitmap.CompressFormat.JPEG beacuse quality works with JPEG format.
private void saveBitmap(Bitmap bmp, String fileNameHigh, String fileNameLow, int resolutionHigh, int resolutionLow, String resolutionQuality) {
File f = new File(Environment.getExternalStorageDirectory() + "FiltureImages");
if (!f.exists()) {
f.mkdirs();
}
File file = new File(f, fileNameHigh + resolutionQuality + ".png");
if (file.exists()) {
file.delete();
}
File file1 = new File(f, fileNameLow + resolutionQuality + ".png");
if (file1.exists()) {
file1.delete();
}
try {
FileOutputStream high = new FileOutputStream(file);
FileOutputStream low = new FileOutputStream(file1);
bmp.compress(Bitmap.CompressFormat.JPEG, resolutionHigh, high);
bmp.compress(Bitmap.CompressFormat.JPEG, resolutionLow, low);
Toast.makeText(this, "Image save successfully", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(this, "Image save falid", Toast.LENGTH_SHORT).show();
}
}
Create a second bitmap with an alternate resolution this way and call scaledBmp.compress() into a new file.
Bitmap scaledBmp = Bitmap.createScaledBitmap(bmp, newWidth, newHeight, false);
I am using share intent in my application,but i am not able to share image and text,i am using image and text from my json response,but it is not working.i am not getting any error,but the the method for sharing is not working
JSON Response : http://pastie.org/10753346
public void onShareItem(View v) {
// Get access to bitmap image from view
// Get access to the URI for the bitmap
Uri bmpUri = getLocalBitmapUri(descpic);
if (bmpUri != null) {
// Construct a ShareIntent with link to image
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.putExtra(Intent.EXTRA_TEXT, desc.getText().toString());
shareIntent.setType("image/*");
// Launch sharing dialog for image
startActivity(Intent.createChooser(shareIntent, "Share Image"));
} else {
// ...sharing failed, handle error
}
}
// Returns the URI path to the Bitmap displayed in specified ImageView
public Uri getLocalBitmapUri(ImageView imageView) {
// Extract Bitmap from ImageView drawable
Drawable drawable = imageView.getDrawable();
Bitmap bmp = null;
if (drawable instanceof BitmapDrawable){
bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
} else {
return null;
}
// Store image to default external storage directory
/*Uri bmpUri = null;
try {
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
file.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (IOException e) {
e.printStackTrace();
}*/
OutputStream fOut = null;
Uri outputFileUri=null;
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 {
bmp.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
}
return outputFileUri;
}
Do this to save image. Replace your method with this
public Uri getLocalBitmapUri(ImageView imageView) {
imageview.buildDrawingCache();
Bitmap bm = imageview.getDrawingCache();
OutputStream fOut = null;
Uri outputFileUri;
try {
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "folder_name" + File.separator);
root.mkdirs();
File imageFile = new File(root, "myPicName.jpg");
outputFileUri = Uri.fromFile(imageFile);
fOut = new FileOutputStream(imageFile);
} catch (Exception e) {
e.printStackTrace();
}
try {
bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
return outputFileUri;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
I have the following code:
YuvImage yuv = new YuvImage(result.getExtractImageData(),
camera.getParameters().getPreviewFormat(),
result.getWidth(),
result.getHeight(), null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
yuv.compressToJpeg(new Rect(0, 0, result.getWidth(), result.getHeight()), 100, out);
byte[] bytes = out.toByteArray();
Bitmap image = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
image = RotateImage(image, rotation);
createDirectoryAndSaveFile(image, "Image_" + result.getCaptureTime() + ".jpg");
String filePath = Environment.getExternalStorageDirectory() + "/MyFolder/Image_" + result.getCaptureTime() + ".jpg";
try {
ExifInterface exif = new ExifInterface(filePath);
exif.getAttribute("UserComment");
// call this next setAttributes a few times to write all the GPS data to it.
exif.setAttribute("UserComment", String.valueOf(result.getCaptureTime()));
exif.saveAttributes();
}
catch (IOException e) {
e.printStackTrace();
}
It is supposed to punt under UserComment the result.getCaptureTime() at which it was captured from the camera. I download it to a Windows folder and I can't see the properties I just created...
What I'm doing wrong?
EDIT:
private void createDirectoryAndSaveFile(Bitmap imageToSave, String fileName)
{
File direct = new File(Environment.getExternalStorageDirectory() + "/MyFolder");
if (!direct.exists()) {
File wallpaperDirectory = new File("/sdcard/MyFolder/");
wallpaperDirectory.mkdirs();
}
File file = new File(new File("/sdcard/MyFolder/"), fileName);
if (file.exists()) {
file.delete();
}
try {
FileOutputStream out = new FileOutputStream(file);
imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}