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.
Related
How to write image in phone directory. Directory is created but i could not write image in this folder. Here is my code .please check if i am doing something wrong thanks in advance.
Bitmap bitmap;
String directory = new_path; // I am getting path here of image like sdcard/0/emulated/image.jpg
String folder_name = "abc"
bitmap = BitmapFactory.decodeFile(directory);
iv_6.setImageBitmap(bitmap); // image displayed here but not saving in directory.
try {
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator +folder_name);
f.mkdirs();
FileOutputStream fo = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, fo);
fo.close();
}catch (Exception e)
{
e.printStackTrace();
}
public void savePng(Bitmap bitmap, String filePath) {
try {
File temp = new File(filePath);
FileOutputStream os = new FileOutputStream(temp + ".png");
bitmap.compress(Bitmap.CompressFormat.PNG, 50, os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
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
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"/>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Am developing a coloring of images in android. So after applying colors to my image when i click another imageview like save, then i have to save that image to gallery.
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) {
}
You have to
Save the image to your persistent storage.
Add an entry to the MediaStore content provider.
First one can be achieved using the following code:
FileOutputStream out = new FileOutputStream(filePath);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
Second,
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, imagePath, name, description);
First get the drawingCache(bitmap) of the imageView and then save the bitmap to the SDCard.
File folder = new File(Environment.getExternalStorageDirectory()+"/folder/");
if(!folder.exists()) folderAppointment.mkdirs();
try {
this.setDrawingCacheEnabled(true);
FileOutputStream fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory()+"/folder/file"));
Bitmap bitmap = YOUR_IMAGE_VIEW.getDrawingCache();
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}