Storing pictures in Android apps - android

It is relatively straightforward to retrieve an image from the phone's camera app using an Intent, where that image is in the form of a Bitmap.
I don't know if this is an appropriate question for SO really but is it common practice to just save the entire bitmap as-is? Or do most people compress / resize it down?

You tend to save it using Bitmap.compress, which will compress it for you. Feel free to use PNG which is a lossless format, so no quality loss will occur when you reinflate it.
Of course if you're using an intent to get it from the camera, its usually saved to the file system already. In which case that file is certainly compressed already.

This is a simple method as below:
private Boolean saveImage(Bitmap bitmap){
ByteArrayOutputStream bao = null;
File file = null, image = null;
Boolean save = false;
try{
bao = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bao);
image = new File(Environment.getExternalStorageDirectory() + "", "/yourSelectedFolder/");
if (!image.exists()) {
if (!image.mkdirs()) {
Toast.makeText(context, "Error: Folder Not Created!\nPlease Try Again.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Folder Successfully Created!", Toast.LENGTH_LONG).show();
}
}
file = new File(Environment.getExternalStorageDirectory().toString() + "/yourSelectedFolder/","filename" + ".jpeg");
save = file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
fos.write(bao.toByteArray());
fos.close();
if (save){
Toast.makeText(context, "Image Successfully Saved", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Image Not Saved", Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(context, "Error: "+e, Toast.LENGTH_LONG).show();
}
return save;
}

Related

Take realtime screenshot while video calling android

I have problem with my android code for taking screenshot while on video calling
I found several method for take screenshot on specific layout and this is what I used :
private void takeScreenshot() {
final FrameLayout container = (FrameLayout) findViewById(R.id.remote_video_view_container);
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
container.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(container.getDrawingCache());
container.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
if(mPath!=null){
Toast savedToast = Toast.makeText(getApplicationContext(),
"Drawing saved to Gallery!", Toast.LENGTH_SHORT);
savedToast.show();
}
else{
Toast unsavedToast = Toast.makeText(getApplicationContext(),
"Oops! Image could not be saved.", Toast.LENGTH_SHORT);
unsavedToast.show();
}
outputStream.flush();
outputStream.close();
openScreenshot(imageFile);
} catch (Throwable e) {
// Several error may come out with file handling or DOM
e.printStackTrace();
}
}
private void openScreenshot(File imageFile) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(imageFile);
intent.setDataAndType(uri, "image/*");
startActivity(intent);
}
i try this code for save my draw painting application that will save the drawing layout and then i try to use the same method for save video call layout but return black screen image.
i found that this method cannot use for real-time when i want to record it in video
source : How to programmatically take a screenshot in Android?
so, anyone help me what the best way to solve my problem ?

What is the phone storage in Android devices?

I can pass sdcard location to my adb command using
file:///sdcard/Android/screen.bmp
What is the equivalent string, if my file is saved in phone memory instead of sdcard, will it be
file:///phone/Android/screen.bmp
That isn't necessarily how you access things saved internally on your phone.
Keep in mind how you save an image to internal storage:
Bitmap bitmap = ______; //get your bitmap however
try {
FileOutputStream fos = context.openFileOutput("desiredFilename.png", Context.MODE_PRIVATE);
image.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
Log.e("saveToInternalStorage()", e.getMessage());
Log.e("Saving the bitmap",e.getMessage());
}
Now, to go read it, we just get the context, and call getFileStreamPath(filename) on it.
Bitmap retrievedImage;
String filename = "desiredFilename.png";
try {
File filePath = context.getFileStreamPath(filename);
FileInputStream fi = new FileInputStream(filePath);
retrievedImage = BitmapFactory.decodeStream(fi);
} catch (Exception e) {
Log.e("Retrieving the image", e.getMessage());
}
You can read more about this here.

how to save the image in sdcard in android

i am working on OpenGl Es in android.given efects on images and when i save the image in sdcard it showing black image.how i solve this problem.
File cacheDir;
Toast.makeText(ImageProcessingActivity.this, "Photo", 500).show();
Bitmap icon;
frame.setDrawingCacheEnabled(true);
icon = Bitmap.createBitmap(frame.getDrawingCache());
Bitmap bitmap = icon;
frame.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(ImageProcessingActivity.this, "Photo Saved Sucessfully", 500)
.show();
image.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(ImageProcessingActivity.this, "Photo Not Saved Sucessfully",500).show();
}
I had this problem once and it was because getDrawingCache is not what it should be. The problem is not the saving, is the way you do it.
From what I understand you want to capture the screen of your app, but this is very tricky and can cause a lot of problems.
Read this topic, since it helped me a lot when I had this problem.
How to programmatically take a screenshot in Android?
Edit:
Also, because you are using GLES and then printing the screen you should go to Settings > Developer Options > check Disable Hardware Overlays and Force GPU Rendering.

saving picture from my app

The idea of my app is to capture image from camera then crop specified area from it.
The problem :
When i save the cropped image in my sd card for the first time to launch the app, it saved properly. but when run my app one more time and take image then crop it. when save it the first image that take and crop at first time appear in the sd card not the current one.
This is my code for save images:
public static void save(Activity activity, Bitmap bm, String name) {
OutputStream outStream = null;
File externalFilesDir = activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File outFile = new File(externalFilesDir, "IDOCR" + File.separator + "Numbers");
if (!outFile.exists())
outFile.mkdirs();
File number = new File(outFile, name + ".PNG");
//if (number.exists())
// number.delete();
try {
//outStream = new FileOutputStream(new File(path));
outStream = new FileOutputStream(number);
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
bm.recycle();
System.gc();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Maybe if you are trying to overwrite the previous version of the file, you should first delete the previous one...
You can add:
if (!outFile.exists())
outFile.mkdirs();
else {
outFile.delete();
outFile.createNewFile();
}

NullpointerException when trying to save the current Android app view as image

Hi StackOverflow community,
in my application I'd like to have a button which allows the user to capture the current view.
For that reason, I've written the following method (oriented towards: Android save view to jpg or png ):
private LinearLayout container;
public void createImageOfCurrentView(View v) {
if (isExternalStoragePresent()) {
container = (LinearLayout) findViewById(R.id.container);
container.setDrawingCacheEnabled(true);
Bitmap b = container.getDrawingCache();
File dir = new File(Environment.getExternalStorageDirectory().getPath()
+ "/" + getPackageName() + "/");
dir.mkdirs();
File file = new File(dir, "image.jpg");
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
b.compress(CompressFormat.JPEG, 95, fos); // NullPointerException!
Toast.makeText(this, "The current view has been succesfully saved "
+ "as image.", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
Toast.makeText(this, "Unfortunatly an error occured and this "
+ "action failed.", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "Bacause of lacking access to the storage "
+ "of this device, the current view couldn't be saved as an image.",
Toast.LENGTH_LONG).show();
}
}
The problem is according to LogCat that there occured a NullPointerException when trying to create the jpeg. So probably the method container.getDrawingCache() is not working. On the phone the file is generated. However with the size of 0 bytes and no visible image. I would appreciate any suggestions what I have to do in order to make it work as I want.
Try it -
public static Bitmap convertViewToBitmap(View view) {
Bitmap result = null;
try {
result = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.RGB_565);
view.draw(new Canvas(result));
}catch(Exception e) {}
return result;
}
Try it -
view.setDrawingCacheEnabled(true);
Bitmap = bm = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);

Categories

Resources