Im trying to capture user signature and store it under SD Card. App runs without any error.But the image file is not storing to the SD card.it only shows folder named sign.jpg.
Image is missing .please help me to slove this. im new to android..
Here is my current code
public Bitmap save(View v) {
Log.v("log_tag", "Width: " + v.getWidth());
Log.v("log_tag", "Height: " + v.getHeight());
if (mBitmap == null) {
mBitmap = Bitmap.createBitmap(mContent.getWidth(),
mContent.getHeight(), Bitmap.Config.RGB_565);
}
Canvas canvas = new Canvas(mBitmap);
try {
FileOutputStream mFileOutStream = new FileOutputStream(mypath);
v.draw(canvas);
mBitmap.compress(Bitmap.CompressFormat.PNG, 90, mFileOutStream);
mFileOutStream.flush();
mFileOutStream.close();
String url = Images.Media.insertImage(getContentResolver(),
mBitmap, "title", null);
Log.v("log_tag", "url: " + url);
} catch (Exception e) {
Log.v("log_tag", e.toString());
}
return mBitmap;
}
Logcat displays this error message
09-16 17:11:52.517: E/BitmapFactory(1123): Unable to decode stream: java.io.FileNotFoundException: /storage/sdcard0/Captures/sign1945.jpg: open failed: EISDIR (Is a directory)
your problem is in mypath variable
-be sure to use Environment.getExternalStorageDirectory().getPath() to get the SD Card path
-you need to tell the compress method : hey i want to save bitmap to this directory with this name! like this:
//saveDirectoryPath = Environment.getExternalStorageDirectory().getPath() + "/folder/";
File file = new File(saveDirectoryPath);
if (!file.exists())
file.mkdir(); //or file.mkdirs(); depends on your need
File bitmapFile = new File(file, "yourImageFileName.jpg");
FileOutputStream mFileOutStream = new FileOutputStream(bitmapFile);
mBitmap.compress(CompressFormat.PNG, 90, mFileOutStream);
Related
I want to save bitmaps in the gallery.
Currently, I am using the following code:
public void saveBitmap(Bitmap output){
String filepath = Environment.getExternalStorageDirectory().toString() + "/Imverter/ImverterEffectedImage";
File dir = new File(filepath);
if(!dir.exists()){
dir.mkdir();
}
String fileName = "Imverter" + System.currentTimeMillis() + ".jpg";
File image = new File(dir, fileName);
try {
FileOutputStream fileOutputStream = new FileOutputStream(image);
output.compress(Bitmap.CompressFormat.JPEG, 80, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
It saves one single bitmap efficiently, but in my app, I have to deal with multiple bitmaps, and this method results in the slow output.
I want to store every single bitmap in a different files.
Thanks in advance.
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);
The below code captures the webview image and save it in Pictures folder. The code is as follows
public static void getBitmapOfWebView(final WebView webView){
Date now = new Date();
String mPath = Environment.getExternalStorageDirectory() + File.separator + Environment.DIRECTORY_PICTURES + File.separator + "Screenshot_" + now + ".jpg";
webView.setDrawingCacheEnabled(true);
webView.buildDrawingCache(true);
Picture picture = webView.capturePicture();
Bitmap b = Bitmap.createBitmap( webView.getWidth(), webView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
picture.draw(c);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mPath);
if ( fos != null ) {
b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
}
}
catch(Exception e) {}
b.recycle();
}
The above code works and saves the screenshot in the pictures folder. But the problem is when i scroll down the WebView and try to execute the same code again, it saves the old screenshot of the webview in the pictures folder.
How do i code in such a way that when i scroll the Webview, it should save the current screenshot of the webview.
Any help Appreciated.
Thank you.
I have a bitmap file which i need to upload to my php server but as the file is very large I decided to resize it and save it. Later on I try to read it back to display resized image. But this time I am not getting the same image
Below is code for writing image and returning File
public static File savebitmap(Bitmap bmp) throws IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "testimage.jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
return f;
}
Below is code for reading and displaying
File file=ImageUtil.savebitmap(this.bitmap);
this.imgChoosenImage.setImageURI(Uri.parse(file.getAbsolutePath()));
Please tell me what exactly is going wrong here
first check the images are saved in ur path as defined, and Make sure ur giving correct path for retriving image.
I have used this below code for saving imge in gallery
String iconsStoragePath = Environment.getExternalStorageDirectory()+ File.separator;
File sdIconStorageDir = new File(iconsStoragePath);
//create storage directories, if they don't exist
sdIconStorageDir.mkdirs();
try {
String filePath = null;
filePath = Environment.getExternalStorageDirectory() + File.separator + "testimage" + ".jpg";
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
bmp.compress(Bitmap.CompressFormat.JPG, 100, bos);
bos.flush();
bos.close();
} catch (IOException e) {
Log.w("TAG", "Error saving image file: " + e.getMessage());
Toast.makeText(getApplicationContext(), "Failed to Create folder",
Toast.LENGTH_SHORT).show();
}
For bitmap display in imageview :
File imgFile = new File("/sdcard/Images/testimage.jpg");
//Here File file = ur file path
if(imgFile.exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
}
Permissions:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
This is my code and im trying to capture screenshot of my application.I have background as animation(hearts falling)which looks like a live wallpaper.I want to take screenshot of current page>But its not working.I have used a button to take scrrenshot and imageview to show preview.when button is clicked nothing happens>Iam new to android.Plz help.Thanks in advance!!
View view = findViewById(R.id.Flayout);
view.setDrawingCacheEnabled(true);
Bitmap bitmap = view.getDrawingCache();
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
imgshot = (ImageView) findViewById(R.id.imagescreen);
// set screenshot bitmapdrawable to imageview
imgshot.setBackgroundDrawable(bitmapDrawable);
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState()))
{
// we check if external storage is available, otherwise
// display an error message to the user using Toast Message
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File(sdCard.getAbsolutePath()
+ "/ScreenShots");
directory.mkdirs();
String filename = "screenshot" + i + ".jpg";
File yourFile = new File(directory, filename);
while (yourFile.exists())
{
i++;
filename = "screenshot" + i + ".jpg";
yourFile = new File(directory, filename);
}
if (!yourFile.exists())
{
if (directory.canWrite())
{
try
{
FileOutputStream out = new FileOutputStream(
yourFile, true);
bitmap.compress(Bitmap.CompressFormat.PNG, 90,
out);
out.flush();
out.close();
Toast.makeText(
ResultActivity.this,
"File exported to /sdcard/ScreenShots/screenshot"
+ i + ".jpg",
Toast.LENGTH_SHORT).show();
i++;
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
} else
{
Toast.makeText(ResultActivity.this,
"Sorry SD Card not available in your Device!",
Toast.LENGTH_SHORT).show();
}
break;
}
}
}
Well, check this out:
How to programmatically take a screenshot in Android?
Here looks like what you want to do (as i understand), so test it out.
you have not get the root view. try this code
File file = new File(Environment.getExternalStorageDirectory()+ File.separator + "myimage.jpg");
// create bitmap
Bitmap bitmap;
View v1 = getWindow().getDecorView().getRootView(); //if this didnt work then try sol -3 at the bottom of this answer
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
fo.write(bytes.toByteArray());
fo.close();
if this didnt work
View v1 = mCurrentUrlMask.getRootView();
then try this
View v1 = getWindow().getDecorView().getRootView();
or you can do
sol -3 ) View v1 = findViewById(R.id.linear_layout_id);// add the root view id
Don't forget to add permissions or it wont work:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>