I have created image view with custom shape using
siyamed/android-shape-imageview
Now i want to save the image on SD card with shape.
How can i achieve this?
As the custom view is just an ImageView, try adding something like this to your buttons on click event to save the image as a bitmap;
ImageView imageViewCustom = (ImageView) findViewById(R.id.image_view_to_save);
try {
imageViewCustom.setDrawingCacheEnabled(true);
FileOutputStream fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory()+"/ACustomShapeFolder/file1234.jpg"));
Bitmap bitmap = imageViewCustom.getDrawingCache();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Toast.makeText(this, "Error occured", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(this, "Error occured", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
Hope this helps!
Related
imageView = (ImageView) v.findViewById(R.id.lbl2);
try {
Bitmap bitmap= BitmapFactory.decodeStream(getContentResolver().openInputStream(Uri.parse(c.getPhotoUrl())));
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// handle errors
Toast.makeText(Chat.this, "File not found", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// handle errors
Toast.makeText(Chat.this, "Error : "+e, Toast.LENGTH_SHORT).show();
}
the URI is jpeg file I'm trying decode it into bitmap
and it keeps catching file not found
I am new to android so please excuse me for this noob question.
i wanted to save bmp in my external storage after some research i found out that i need to compress the bmp and then use the flush method of FileOutputStream which will save the bmp in my desired location but how does this work and is there any way to use write function by converting the image to raw bytes of data.
File file = new File(folder,s);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
b.compress(Bitmap.CompressFormat.PNG, 100, fos);
try {
fos.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Hello Friends i'm making a app with webview
i want to take screenshot of my activity
Currnetly i'm using this code for capture image
public Bitmap takeScreenshot() {
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
And This for Save
public void saveBitmap(Bitmap bitmap) {
File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
It sometime Works and sometime not, i mean ,sometime i can see in gallery screenshot and sometime not
i want to if there is any option to show captured screenshot
Like whhen we press Vol+power button
Main problem is how can i show image when its taken
or know if its taken or not
Thanks in advance
This is a sample approach: save your image -> display result with dialog. And to make it available in Android Gallery, the file path should also be changed:
public void saveBitmap(Bitmap bitmap) {
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File imagePath = new File(path, "screenshot.png");//now gallery can see it
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
displayResult(imagePath.getAbsolutePath())// here you display your result after saving
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
Create another method call displayResult to see the result:
public void displayResult(String imagePath){
//LinearLayOut Setup
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
//ImageView Setup
ImageView imageView = new ImageView(this);//you need control the context of Imageview
//Diaglog setup
final Dialog dialog = new Dialog(this);//you need control the context of this dialog
dialog.setContentView(imageView);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
//Display result
imageView.setImageBitmap(BitmapFactory.decodeFile(imagePath));
dialog.show();
}
Other people have run into issues when trying to get a bitmap from a view's Drawing Cache.
Rather than trying to use the drawing cache, you can instead just get the bitmap directly from the view. Google's DynamicListView example uses the following function to do so.
/** Returns a bitmap showing a screenshot of the view passed in. */
private Bitmap getBitmapFromView(View v) {
Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas (bitmap);
v.draw(canvas);
return bitmap;
}
Here is my code to take screen shot and save it in gallery.tried by debugging here screen is captured but not saving to gallery.
#Override
public void onClick(View v) {
Bitmap bitmap = takeScreenshot();
saveBitmap(bitmap);
}
making screenshot
protected Bitmap takeScreenshot() {
// TODO Auto-generated method stub
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
Toast.makeText(getApplicationContext(), "Screen captured", Toast.LENGTH_LONG).show();
return rootView.getDrawingCache();
}
Saving screen here
protected void saveBitmap(Bitmap bitmap) {
// TODO Auto-generated method stub
File imagePath = new File(Environment.getExternalStorageDirectory()
+ "/screenshot.png");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
Toast.makeText(getApplicationContext(), "Screen saved", Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
Run your code in device please, it may be the problem of emulator, because emulator dont have physical SD Card.
I am reading a image file into a byte array.This byte array i have to save again as a image file onto the sdcard.To read the file i have used the following code:
public void readimage()
{
InputStream ins_image = getResources().openRawResource(R.drawable.btn_cancel);
outputStream=new ByteArrayOutputStream();
try
{
ins_image.available();
} catch (IOException e) { e.printStackTrace(); }
try
{
Log.e( "Size of image", ""+ins_image.available());
} catch (IOException e) {e.printStackTrace();}
int size = 0;
byte[] buffer_image = new byte[200000];
try {
while((size=ins_image.read(buffer_image,0,200000))>=0)
{
outputStream.write(buffer_image,0,size);
}
} catch (IOException e) { e.printStackTrace(); }
int length_of_image= outputStream.toByteArray().length;
byte_image=outputStream.toByteArray();
Log.e("Size of image",""+length_of_image);
}
And the below code to save the file:
public void saveimage_fromarray()
{
File photo=new File(Environment.getExternalStorageDirectory(), "photo.png");
if (photo.exists())
{
photo.delete();
}
try
{
FileOutputStream fos=new FileOutputStream(photo.getPath());
fos.write(byte_image[0]);
fos.close();
}
catch (java.io.IOException e)
}
However the file is being saved but it does not display anything.Can somebody please tell me why is it so?
Set the size of the image you are getting in place of 0.
fos.write(byte_image[0]);
Seems like you're writing only one byte of the image.
fos.write(byte_image[0]);
Please compare source file, byte buffer, array and output file sizes.
why don't you simplify everything and just calls this method:
bmp.compress(Bitmap.CompressFormat.PNG, 100, <pass here a valid file outputstream>);