Android Image Not saving to sdcard - android

After pressing capture Button , image should save in sdcard, it is taking snapshot but not saving the image with that,
Then how can I put the capture button wherever I want? I have an overlay in the Imageview and I need to put the button over the overlay.

Use this one to store image in sd card
public void save(Bitmap image)
{
File sdcard = Environment.getExternalStorageDirectory();
File f = new File (sdcard, imagename);
FileOutputStream out=null;
try {
out = new FileOutputStream(f);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
image.compress(Bitmap.CompressFormat.PNG, 90, out);
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}

int picture callback, use like this
jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
camera.startPreview();
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(
"/mnt/sdcard/myphoto.jpg");
outStream.write(data);
outStream.close();
Log.d("Log", "onPictureTaken - wrote bytes: " + data.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Log.d("Log", "onPictureTaken - jpeg");
}
};

stream=new ByteArrayOutputStream();
temBitmap=Bitmap.createBitmap(bmp);
temBitmap.compress(Bitmap.CompressFormat.JPEG,100, stream);
path+=String.format(
getString(R.string._sdcard_d_jpg),
System.currentTimeMillis());
outStream = new FileOutputStream(path+extension); // <9>
outStream.write(stream.toByteArray());
outStream.close();
change the above snippet this way:
path+=String.format(
getString(R.string._sdcard_d_jpg),
System.currentTimeMillis());
outStream = new FileOutputStream(path+extension); // <9>
temBitmap=Bitmap.createBitmap(bmp);
temBitmap.compress(Bitmap.CompressFormat.JPEG,100, outStream);
outStream.close();
temBitmap.recycle()

Related

How does the flush method work?

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();
}

Recorded video Can't read from the file Location Instantly?

I am Recording a video using my android application.when i am trying to read the video from the path after recording i did'nt get the full recorded file.
but after some time i can read the full file.i don't know what is the problem?
please help.
Here is my code
try {
videoFile = getOutputMediaFile(MEDIA_TYPE_VIDEO);
videothumb = new File(getOutputMediaThumbFile(MEDIA_TYPE_VIDEO), videoFile.getName());
db.insertFileInfo(videoFile.getName(), 0, videoFile.length(), 0, "video/mp4");
if (videoFile == null) {
Log.d("", "Error creating media file, check storage permissions: ");
safeToTakePicture = true;
return null;
}
videothumb.createNewFile();
videoFile.createNewFile();
Thread.sleep(10000);
upVFile = new File(videoFile.getAbsolutePath());
FileOutputStream fos = new FileOutputStream(videoFile);
FileOutputStream ftos = new FileOutputStream(videothumb);
thumb = Bitmap.createScaledBitmap(bmv, 200, 200, false);
thumb.compress(Bitmap.CompressFormat.JPEG, 90, ftos);
ftos.flush();
ftos.close();
bmv.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmv.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
baos.close();
String encodedImage = new String(Base64.encodeBase64(b));
editor.putString("image_data", encodedImage);
editor.commit();
editor.putString(IMAGE_NAME, videoFile.getName());
editor.commit();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
write a method for refreshing gallery
private void refreshGallery(File file){
Intent mediaScanIntent=new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(Uri.fromFile(file));
sendBroadcast(mediaScanIntent);
}

Save button not working

i have bitmap image when return result and then i want to save it in Photo Art Camera folder in SD card but it is not saved. it shows the toast "Photo Not Saved Sucessfully".
This is the code:
mSave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
File cacheDir;
Bitmap bitmap = result;
// String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
Date d = new Date();
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
cacheDir = new File(android.os.Environment.getExternalStorageDirectory(), "Photo Art Camera");
} else { cacheDir = MainActivity.this.getCacheDir(); }
if (!cacheDir.exists()) { cacheDir.mkdirs(); }
File file = new File(cacheDir, "PhotoMarge" + d.getTime() + ".jpg");
try {
fOut = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 150, fOut);
// getImageBitmap(myurl).compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
Toast.makeText(MainActivity.this, "Photo Saved Sucessfully", 500).show();
// mDialog.dismiss();
// MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
}
catch (FileNotFoundException e) { e.printStackTrace(); }
catch (Exception e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "Photo Not Saved Sucessfully", 500).show();
}
}
});
i removed my misstake from code .Actually i am saving another bitmap instead saving and converting framelayout bitmap
File cacheDir;
frame.setDrawingCacheEnabled(true);
icon = Bitmap.createBitmap(frame.getDrawingCache());
Bitmap bitmap = icon;
// String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
Date d=new Date();
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"Photo Art Camera");
else
cacheDir=MainActivity.this.getCacheDir();
if(!cacheDir.exists())
cacheDir.mkdirs();
File file = new File(cacheDir, "PhotoMarge"+d.getTime()+".jpg");
try {
fOut = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, fOut);
//getImageBitmap(myurl).compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
count();
Toast.makeText(MainActivity.this, "Photo Saved Sucessfully", 500).show();
// mDialog.dismiss();
//MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(MainActivity.this, "Photo Not Saved Sucessfully", 500).show();
}
}
});
you are compressing it with different argument make it to 100 or 120 it should work..
OutputStream fOut = new FileOutputStream(output);
merged.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show();
OR
String strMyImagePath = f.getAbsolutePath();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.PNG,70, fos);
fos.flush();
fos.close();
// MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
}catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
save22.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
File cacheDir;
Toast.makeText(Main.this, "Photo", 500)
.show();
Bitmap icon;
relativelayout.setDrawingCacheEnabled(true);
icon = Bitmap.createBitmap(relativelayout.getDrawingCache());
Bitmap bitmap = icon;
relativelayout.setDrawingCacheEnabled(false);
//File mFile1 = Environment.getExternalStorageDirectory();
Date d=new Date();
String fileName = d.getTime()+"mg1.jpg";
File storagePath = (Environment
.getExternalStorageDirectory());
File dest = new File(storagePath + "/CityAppImages");
if (!dest.exists()) {
dest.mkdirs();
}
File mFile2 = new File(dest, fileName);
sdpath= mFile2.getAbsolutePath();
Log.d("qqqqqqqqqqqqqqqqqqqqqqq", "zzzzzzzz" + sdpath);
try {
FileOutputStream outStream;
outStream = new FileOutputStream(mFile2);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
Toast.makeText(Main.this, "Photo Saved Sucessfully", 500)
.show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(Main.this, "Photo Not Saved Sucessfully",
500).show();
}

Saving jpg to file messes it up

When I save and load a jpg image (with a white background) to external storage, it looks like this:
The code:
Save:
try {
outStream = new FileOutputStream(fileUri);
image.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        outStream.flush();
      outStream.close();
} catch (Exception e) {
Log.e("cache", e.getMessage());
e.printStackTrace();
}
Load:
Bitmap bm = BitmapFactory.decodeFile(fileUri.toString());
What am I doing wrong? Thanks.
This is the code I use (should work for you):
Save Bitmap:
public void writeBitmapToMemory(String filename, Bitmap bitmap) {
FileOutputStream fos;
// Use the compress method on the Bitmap object to write image to the OutputStream
try {
fos = game.openFileOutput(filename, Context.MODE_PRIVATE);
// Writing the bitmap to the output stream
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
And to read:
public Bitmap readBitmapFromMemory(String filename) {
Bitmap defautBitmap = null;
File filePath = game.getFileStreamPath(filename);
FileInputStream fi;
try {
fi = new FileInputStream(filePath);
defautBitmap = BitmapFactory.decodeStream(fi);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
return defautBitmap;
}

saving file on sd card, writes file size 0

I have trouble with saving images on sd card. I can see the file on the sd card but file is empty (size 0). I tried saving it on the phone memory and it works fine. Here is my code.
Imagewritter {
public static boolean writeAsJPG(Context context, Bitmap bitmap,
String filename) {
filename = filename + ".jpg";
File path = Environment.getExternalStorageDirectory();
File f = new File(path, filename);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
fos = context.openFileOutput(filename, Context.MODE_WORLD_READABLE);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d(TAG, "file not found");
return false;
}
bitmap.compress(CompressFormat.JPEG, quality, fos);
try {
fos.flush();
fos.close();
} catch (IOException e) {
Log.d(TAG, "error closing");
e.printStackTrace();
}
}
here is the code where the bitmap comes from.
DrawingView = (drawing) findViewById(R.id.drawing_view);
drawingBitmap = (Bitmap) DrawingView.getBitmap();
String idName = timeStampText;
//save Image as JPG
ImageWritter.writeAsJPG(getApplicationContext(), drawingBitmap, idName);
i think u have to add these permission in manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Get the image in byte array & check the size of that byte array.. I think you getting 0 size byte array.....
The only problem here is that I have two lines for writing the file. Using FileOutputStream and OpenFileOutput. Just remove these lines and it'll be fine.
try {
fos = context.openFileOutput(filename, Context.MODE_WORLD_READABLE);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d(TAG, "file not found");
return false;
}

Categories

Resources