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();
}
Related
How can change image saving location i have created the folder but how to save image in it. all downloaded images are saved in pictures folder
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
ContentResolver r = contentResolverWeakReference.get();
AlertDialog alertDialog = alertDialogWeakReference.get();
if (r != null)
file = new File(Environment.getExternalStorageDirectory().getPath() + "/CreativeGraphy");
if (!file.exists()) {
file.mkdir();
}
try {
file.createNewFile();
MediaStore.Images.Media.insertImage(r, bitmap, name, desc);
} catch (Exception e) {
e.printStackTrace();
}
alertDialog.dismiss();
Toast.makeText(context, "Download succeed ", Toast.LENGTH_SHORT).show();
}
Use this method
public static void saveBitmap(String path, Bitmap bitmap) {
FileOutputStream out = null;
try {
out = new FileOutputStream(path);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
After saving you can call scanFile method to index your file in the gallery.
MediaScannerConnection.scanFile(context, new String[]{path}, null, null);
thanks everyone This works
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
ContentResolver r = contentResolverWeakReference.get();
AlertDialog alertDialog = alertDialogWeakReference.get();
if (r != null)
file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/CreativeGraphy";
File dir = new File(file_path);
if (!dir.exists()) {
dir.mkdir();
}
File file = new File(dir,name );
FileOutputStream fOut;
try {
MediaStore.Images.Media.insertImage(r, bitmap, name, desc);
fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
alertDialog.dismiss();
Toast.makeText(context, "Download succeed ", Toast.LENGTH_SHORT).show();
}
I have created a file and written into the file using editText. Now I want to write into a file named "note.txt". But the content to be written should be stored in a variable. Can any one help me with the code??
private void writeFile() {
File extStore = Environment.getExternalStorageDirectory();
// ==> /storage/emulated/0/note.txt
String path = extStore.getAbsolutePath() + "/" + fileName;
Log.i("ExternalStorageDemo", "Save to: " + path);
String data = editText.getText().toString();
try {
File myFile = new File(path);
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append(data);
myOutWriter.close();
fOut.close();
Toast.makeText(getApplicationContext(), fileName + " saved", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
Do you mean this?:
private void save(String inputText) {
FileOutputStream out = null;
BufferedWriter writer = null;
try{
out = openFileOutput("note.txt",Context.MODE_PRIVATE);
writer = new BufferedWriter(new OutputStreamWriter(out));
writer.write(inputText);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try{
if (writer!=null);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I want to write string of data in a file in android . That is why I have the following code :
public String DumpFile(String fileName,String data)
{
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_data");
myDir.mkdirs();
if(myDir.exists())
Toast.makeText(this, "Directory exists" ,Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "directory not exists " ,Toast.LENGTH_LONG).show();
// Random generator = new Random();
String fname = fileName;
// showToastOnUiThread("File Name: "+fname+" and Data: "+data,
// Toast.LENGTH_LONG);
File file = new File(myDir, fname);
Toast.makeText(this, "Link is "+file.getAbsolutePath(),Toast.LENGTH_LONG).show();
// Toast.makeText(this, "Content is "+data,Toast.LENGTH_LONG).show();
OutputStream bos = null;
FileOutputStream out = null;
if (file.exists())
file.delete();
try {
out = new FileOutputStream(file);
bos = new BufferedOutputStream(out);
bos.write(data.getBytes());
bos.flush();
// out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return fname;
}
But when I have run the code , I have got the Toast that directory not exists . Where is the error ? Can you help me ?
Add the permission to your manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
i have a imageview , i am trying to save bitmap from imageview by this method
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
the rgb of saved image is not like that it looks in running app,so i am wondering if there is any way to save image view directly to a sd card rather getting the bitmap and then save it to sd card.
please help me i have tried everything.
You can read and write object using below code :
public static void witeObjectToFile(Context context, Object object, String filename)
{
ObjectOutputStream objectOut = null;
try
{
FileOutputStream fileOut = context.openFileOutput(filename, Activity.MODE_PRIVATE);
objectOut = new ObjectOutputStream(fileOut);
objectOut.writeObject(object);
fileOut.getFD().sync();
} catch (IOException e)
{
e.printStackTrace();
} finally
{
if (objectOut != null)
{
try
{
objectOut.close();
} catch (IOException e)
{
// do nowt
}
}
}
}
public static Object readObjectFromFile(Context context, String filename)
{
ObjectInputStream objectIn = null;
Object object = null;
try
{
FileInputStream fileIn = context.getApplicationContext().openFileInput(filename);
objectIn = new ObjectInputStream(fileIn);
object = objectIn.readObject();
} catch (FileNotFoundException e)
{
// Do nothing
} catch (IOException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} finally
{
if (objectIn != null)
{
try
{
objectIn.close();
} catch (IOException e)
{
// do nowt
}
}
}
return object;
}
For example ArrayList can be saved as :
ImageView abcImage = (ImageView) readObjectFromFile(context, AppConstants.FILE_PATH_TO_DATA);
and write as :
witeObjectToFile(context, abcImage, AppConstants.FILE_PATH_TO_DATA);
Try to use this
public void onClick(View v) {
if (v.getId() == R.id.btnSaveImage) {
imageView.setDrawingCacheEnabled(true);
Bitmap bm = imageView.getDrawingCache();
storeImage(bm);
}
}
private boolean storeImage(Bitmap imageData) {
// get path to external storage (SD card)
String iconsStoragePath = Environment.getExternalStorageDirectory() + "/yourappname/";
File sdIconStorageDir = new File(iconsStoragePath);
// create storage directories, if they don't exist
sdIconStorageDir.mkdirs();
try {
File file = new File(sdIconStorageDir.toString() + File.separator + "fileName");
FileOutputStream fileOutputStream = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
imageData.compress(CompressFormat.PNG, 100, bos);
bos.flush();
bos.close();
MediaScannerConnection.scanFile(this, new String[] { file.getPath() },
new String[] { "image/jpeg" }, null);
Toast.makeText(this, "Snapshot Saved to " + file, Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
return false;
} catch (IOException e) {
return false;
}
return true;
}
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()