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.
Related
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);
I am working on Android for saving multiple image in sd card but its getting save in gallery also and specific folder also...
any one can help on this
thanks..
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
// 2
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
mImage.setImageBitmap(thumbnail);
CharSequence time2 = android.text.format.DateFormat.format(
"yymmddhhmmss", new java.util.Date());
String date1 = time2.toString();
//ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try {
thumbnail .compress(CompressFormat.JPEG, 100, new FileOutputStream(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/mypics/img" + date1 + ".png"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else
Toast.makeText(getBaseContext(), "Please take snap again",
Toast.LENGTH_LONG).show();
}
try like this::
Use this function to save your bitmap in SD card
private void SaveIamge(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();
}
}
and add this permision in manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
to save in gallery just add this one:
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
If you do not want images inside the /mypics/img/ folder to be shown in the gallery. All you need to do is:
Include an empty file named .nomedia in your /mypcis/img/
directory (note the dot prefix in the filename). This will prevent
Android's media scanner from reading your media files and including
them in apps like Gallery or Music.
Note: For some devices, this only works for new folders (not those which are already scanned) So my recommendation would be to create a new folder with the .nomedia file and store your images to that. Now they will not appear in the gallery. Only in the external SD card folder.
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.
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());
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.