I wrote code witch can to save image to sd card.my code working perfect but when i going to sd car with file system my image is public.this is my code
private Bitmap takeScreenShot(Context content, ScrollView view) {
int totalHeight = view.getChildAt(0).getHeight();
int totalWidth = view.getChildAt(0).getWidth();
Bitmap bitmap = getBitmapFromView(view, totalHeight, totalWidth);
File imagePath = new File(Environment.getExternalStorageDirectory()
+ "/image.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage(content.getContentResolver(),
bitmap, "Screen", "screen");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
how i can save imege in sd card to can hide or unpublish my image.i meean ,i don't need to can show it with file manager
if anyone knows solution please help me
Related
I created a bitmap with text and I can view it in an Imageview, but when I save the Bitmap I only get a black image. I have spend three hours looking at similar questions but none of the worked for me. Here is the code.
Thanks for any help.
public void createBitmap(){
Bitmap LabelBitmap;
FileOutputStream fos = null;
//create Text Bitmap
LabelBitmap = textAsBitmap(this,"BRO D 0813","fonts/arialbd.ttf", 4, Color.BLACK);
//load bitmap in to Imageview
ImageView myImageView = (ImageView) findViewById(R.id.imageView);
myImageView.setImageBitmap(LabelBitmap);
// save bitmap
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/myfolder");
myDir.mkdirs();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
LabelBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
if (!myDir.exists()) {
myDir.mkdir();
}
File myDirFile = new File(root +"/myfolder/mybitmap.jpg");
try {
if(myDirFile.exists()){
myDirFile.delete();
}
myDirFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos = new FileOutputStream(myDirFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fos.write(bytes.toByteArray());
fos.flush();
fos.close();
Toast.makeText(this, "Image saved", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
JPEG Image has a black background by default, so if your text color is also black you will get a black image. If your image has no background color, you must save it as PNG. Change as following and have a try:
LabelBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
to:
LabelBitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);
I was Developing an application which will have image on image view .
My need:
What i need is When i click the button then it should store the image that exist in the image view to the sd card(emulator).
Here is how i used:(but no expected results)
Button btnWriteSDFile = (Button) findViewById(R.id.btnWriteSDFile);
btnWriteSDFile.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ImageView myImage = (ImageView) findViewById(R.id.imageView1);
BitmapDrawable drawable = (BitmapDrawable) myImage.getDrawable();
Bitmap bitmap = drawable.getBitmap();
File sdCardDirectory = Environment.getExternalStorageDirectory();
File image = new File(sdCardDirectory, "image.png");
boolean success = false;
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (success) {
Toast.makeText(getApplicationContext(), "Image saved with success",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Error during image saving", Toast.LENGTH_LONG).show();
}
}
});
In my above code i didnt get any error
It simply Shows that "save file in mnt/sd/image.png" but no images found.
It would be appreciable if some one helps me to get me out from this rid.
**
"Found out where the exact issues": My program was running perfectly
# 1st time if i click button and check in gallery means there is no
image but once i close and open the emulator then there is a image.But
i need to see the image as soon as updated how to do this any ideas?
**
Your code looks good, but in order to write something to external storage, you should have the following permission declared in the manifest:
android.permission.WRITE_EXTERNAL_STORAGE
if you use some thing like
ImageView myImage = (ImageView) findViewById(R.id.imageView1);
myImage .setDrawingCacheEnabled(true);
myImage .buildDrawingCache();
Bitmap bitmap = myImage .getDrawingCache();
File sdCardDirectory = Environment.getExternalStorageDirectory();
File image = new File(sdCardDirectory, "image.png");
boolean success = false;
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (success) {
Toast.makeText(getApplicationContext(), "Image saved with success",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Error during image saving", Toast.LENGTH_LONG).show();
}
}
});
and please set the following permission in androidManifest.xml
android.permission.WRITE_EXTERNAL_STORAGE
Try below code to take a screenshot
private static Bitmap takeScreenShot()
{
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
return bitmap;
}
For saving
private File saveBitmap(Bitmap bitmap)
{
File snapShot=null;
try
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "test.jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
} catch (Exception e)
{
e.printStackTrace();
}
return snapShot;
}
Need to give permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
I am trying to save bitmap to sd cart, but it saved like black image.
This code shows like I am creating bitmap
mStoryView.setDrawingCacheEnabled(true);
mStoryView.buildDrawingCache();
Bitmap result = Bitmap.createBitmap(mStoryView.getDrawingCache());
mStoryView.setDrawingCacheEnabled(false);
result = ImageUtil.saveStoryImage(this,result);
On this step bitmap looks good ( I set it on background view).
public static Bitmap saveStoryImage(Context context, Bitmap result) {
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File file = new File(extStorageDirectory, "test.png");
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(file);
boolean b = result.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
After that I go to my sd card ondevice and saw black image.
What is the problem?
When I take a picture with my Android camera, there is an image (imageview3) on my screen that I want to save with my picture.
Here is my onPictureTaken method
public void onPictureTaken(byte[] data, Camera camera) {
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "/Ker");
imagesFolder.mkdirs();
String fileName = "Ker_.jpg";
output = new File(imagesFolder, fileName);
ImageView view = (ImageView) gameactivity.findViewById(R.id.imageView3);
view.setDrawingCacheEnabled(true);
Bitmap b = view.getDrawingCache();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(output);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
b.compress(CompressFormat.JPEG, 95, fos);
try {
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
camera.stopPreview();
}
When I open the folder, the picture saved is only the imageview3 with a black background. Why has the real camera view not been saved?
EDIT
I'm trying something with canvas too:
output = new File(imagesFolder, fileName);
ImageView view = (ImageView) gameactivity.findViewById(R.id.imageView3);
view.setDrawingCacheEnabled(true);
Bitmap b = view.getDrawingCache();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(output);
fos.write(data);
fos.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
FileOutputStream fos2 = null;
b.compress(CompressFormat.JPEG, 95, fos2);
try {
Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fos.getFD());
Bitmap bitmap2 = BitmapFactory.decodeFileDescriptor(fos2.getFD());
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(bitmap2, null, null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Is that correct? How to save the canvas into a file on my sdcard (ie write data from canvas on fileoutputstream)
You're appending the JPEG of the imageview and the JPEG from the camera into a single file.
Create a separate file output stream for each file, and write the data from Bitmap.compress() and the onPictureTaken data array into their own streams.
If you want the two images to be combined into a single image, you'll need to decode the data array into a Bitmap, and then use a Canvas to draw the ImageView bitmap and the camera-captured Bitmap onto the canvas in the arrangement you want, and then save that out as a single jpeg.
You can't simply concatenate two compressed JPEG bitstreams together; the file format doesn't work that way.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Capture Image from Camera and Display in Activity
I`m new in android programation and I´m having a problem with some stuff.
I have an ImageView Without any image loaded, when I take a photo with the camera, the captured image puts into the ImageView. I want to save this, but I dont know how to save the directory of the photo in a string, and how to load this directory in the imageView at the "onCreate".
I know that this is a noob cuestion, but this is my first time with this stuff.
Thanks
This code will get your ImageView as a Bitmap
ImageView imgView = (ImageView) findViewById(R.id.myImageView);
imgView.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(imgView.getDrawingCache());
File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/your_app_package/image.png");
This code will save it to a file
try {
FileOutputStream out = new FileOutputStream(cachePath);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
I doing something like this:
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
picture.compress(Bitmap.CompressFormat.JPEG, 80, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + picId + ".jpg");
FileOutputStream fo = null;
try
{
f.createNewFile();
fo = new FileOutputStream(f);
}
catch (IOException e)
{
e.printStackTrace();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
try
{
fo.write(bytes.toByteArray());
}
catch (IOException e)
{
e.printStackTrace();
}
...where picId is name for a file.