How to save paints aswell as that background into sdcars - android

My Android application is supposed to save paints and a background image on my device's SD Card. I am using the method below inside my view class, but for some reason the SD Card is only saving the paints. I can't figure out why the background image isn't saving.
File myDir=new File("/sdcard/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
This is where I'm attempting to save the background, but it isn't working. Any ideas would be greatly appreciated.

Related

Save image and edit text into a gallery

i am trying to create an application which add text on a image like image editor and save it to the gallery. In this my image is coming from gallery or camera and text is added by user but problem is that the text on the image does not store in to the gallery
Could anyone offer any solutions here?
Thanks! :)
Try something like :
1) Create a Linearlayout
LinearLayout layout = (LinearLayout) findViewById(R.id.layout_you_need);
2) Add TextView
TextView tv = new TextView(MainActivity.this);
//tv.setText("Whatever you like");
layout.addView(tv); // add TextView on runtime
layout.setDrawingCacheEnabled(true);
layout.buildDrawingCache(true);
Bitmap saveBm = Bitmap.createBitmap(layout.getDrawingCache());
layout.setDrawingCacheEnabled(false);
SaveImage(savebm);
3) Save the Bitmap
private void SaveImage(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Also please understand the need of your_layout_view.invalidate() , you may need this in future.

Save to internal storage large android bitmap

private String saveToInternalStorage(Bitmap bitmapImage){
ContextWrapper cw = new ContextWrapper(getApplicationContext());
fileName="profile_1";
Date d1 = new Date();
Random r = new Random();
int i1 = r.nextInt(1000-1) + 1;
fileName= "profile-"+i1+"-"+d1.getTime();
final File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
File mypath=new File(directory,""+fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return directory.getAbsolutePath();
}
Its working, but the problem lies that it takes a lot of time once I take the picture from camera. Its giving me black screen, before returning back to activity. How can I resolve it?
You have to perform long-running tasks in separate threads. Read this and this official manual
Also look this manual, it's dedicated to Bitmap processing
Try to do it in asynctask. This will allow to return to your activity instantly.
Try this
private void SaveImage(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/SaveImage");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
I had the same problem, actually custom camera uses its best resolution which can't be in preview mode.
Make resolution low,
like Xiaomi Note 4G has best Resolution of 3600*4400, but if I take picture in this it will take lot of time so I make it down to 1280*720.
Black Screen Problem :
Set parameters to camera, and start preview and after that start preview display.
Eg.
camera.setparameters(params);
camera.startPreview();
camera.setPreviewDisplay(surfaceHolder);

How to save Image(imported from gallery of phone to android app) and save into android app local directory?

I have searched all questions related to this but i did not get solution. My requirement is From my Android app, user can choose image from gallery of phone and set this as his favorite. My question is photo taken from gallery need to be saved to app local directory(This directory is not visible to user because it will store in app).If user remove the image from phone gallery then also app should show his favorite image. So i need to save local directory.Please help me on this. Thanks in Advance. I know following code is used to store to sdcard but i need to save image from sdcard to local folder of app.
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
Bitmap bm = LoadImage(imagePath, bmOptions);
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/MyFolder");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
Using for example:
File path = getExternalFilesDir(Environment.DIRECTORY_DCIM);
File image = new File(path,imageFileName );
Your image will be save in your app directory.

Need to copy image from res folder to gallery/folder

Ok, so i have Gallery Application, with lots of images in it(res/drawable).
On selection you can Set as Wallpaper button and you will have it.
Now i want to save with button SAVE TO PHONE or SD card this image selected. How can i manage that. Copying from res of application folder to Phone or SD card. Dont want to take it from ImageView but just copy the original from res to Phone.
try this code:
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
and add in the manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Follow the steps :-
Create Bitmap using BitmapFactory.decodeResource
Write the contents of Bitmap to an OutputStream using Bitmap.compress
Save the file to anywhere you want.
Code:
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
file = new File(path, "image.jpg");
fOut = new FileOutputStream(file);
Bitmap bitmap = BitmapFactory.decodeResource (getResources(), R.drawable.xyz);
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());

How to save map to sdcard

In my application I need to save map to sdcard. I am able to create directory and file, but in that file nothing is saved.How to save the map which is displayed? Please help me to solve my issue...
File myDir=new File("/sdcard/ma");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
I added permissions in android manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Categories

Resources