Save to internal storage large android bitmap - android

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

Related

how to save multiple bitmaps fastly in android studio

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.

getWindow() not exist in Service class

I use this code in a service class to take screenshot in the service but getWindow() is not available here. what should i do?
private void takeScreenshot() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
} catch (Throwable e) {
// Several error may come out with file handling or DOM
e.printStackTrace();
}
}
Try this approach:
Pass a reference to the activity when you create the class, and when calling relevant methods and use it. But there can be memory leaks with this approach
void MethodThatUsesActivity(Activity myActivityReference) {
myActivityReference.getWindow().getDecorView().getRootView();
}

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.

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 paints aswell as that background into sdcars

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.

Categories

Resources