how to save the image in sdcard in android - 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.

Related

Android: Best way to save image at external Storage

When i'm trying to save an image at some devices the app crashes.
After some research i think the problem is the imageRoot, but i'm kind of lost with so many solutions...
I think the problem is about obtaining external storage path.
The device i'm using to test my app doesn't have any sd card, the path is /mnt/sdcard/Pictures and the image is being saved successfully.
I want it to work whether phone has an sd card or not.
Thanks for any help in advance.
imageRoot=new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "AppName");
private void saveImage() {
Bitmap bitmap = sqimage.getDrawingCache();
File image = new File(imageRoot, imageName);
if (image.exists()) {
image.delete();
}
try {
FileOutputStream output = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output);
output.flush();
output.close();
new SingleMediaFileConnector(getActivity().getApplicationContext(), image);
} catch (FileNotFoundException e) {
e.printStackTrace();
success = false;
} catch (IOException e) {
e.printStackTrace();
success = false;
}
}
Sometimes getExternalStoragePublicDirectory path doesn't exist. You need to check for its existence.
try {
imageRoot=new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "AppName");
File image = new File(imageRoot, imageName);
// Make sure the Pictures directory exists.
imageRoot.mkdirs();
// Other stuffs here
} catch (IOException e) {
// Unable to create file, likely because external storage is
// not currently mounted.
Log.w("ExternalStorage", "Error writing " + file, e);
}
See here for more details.
[Edit]
An alternate way can be use:
getExternalFilesDir(Environment.DIRECTORY_PICTURES);

Downloaded image shows in computer but not in mobiles folder

I am working on an image wallpaper application in android when i download the image from url sometime it shows images on mobile but most of time it does't show but when i connect my mobile to computer its right in the specified folder.
Thanks advance looking forward for answer.
Here is my code to download image.
protected Void doInBackground(Void... arg0) {
FileOutputStream fileOutput = null;
try {
// set the path where we want to save the file
// in this case, going to save it on the root directory of the
// sd card.
File dir = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
String filename = _imagePaths[FullScreenImage.position]
.substring(_imagePaths[FullScreenImage.position]
.lastIndexOf("/"));
if (!dir.exists())
dir.mkdirs();
File file = new File(dir, filename);
Log.i("Local filename:", "" + filename);
// this will be used to write the downloaded data into the file
// we created
fileOutput = new FileOutputStream(file);
Bitmap mybitmap = imageLoader.getBitmap(
_imagePaths[FullScreenImage.position], 800, 480);
mybitmap.compress(CompressFormat.JPEG, 100, fileOutput);
// close the output stream when done
// catch some possible errors...
} catch (IOException e) {
Log.e("Download Image catch > ", e.getMessage());
} finally {
if (fileOutput != null) {
try {
fileOutput.flush();
fileOutput.close();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("Download Image catch > ", e.getMessage());
}
}
}
return null;
}
Use MediaScannerConnection.scanFile() to notify the system about new media files.

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

how to format the path to use in uri.parse() for android

I have an image that is in the following location (it's in my android workspace resources),
D:\Android\WorkSpace\myprojectname\res\drawable-hdpi
I have used the following line of code to attach this image to an email but it doesn't seem to be working, it sends the email but it won't have the attachment.
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,Uri.parse("android.resource://com.mywebsite.myprojectname/" + R.drawable.image));
this this wrong?
Resources res = this.getResources();
Bitmap bm = BitmapFactory.decodeResource(res, R.drawable.image);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory(), "image.jpg");
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//write the bytes in file
FileOutputStream fo = null;
try {
fo = new FileOutputStream(f);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fo.write(bytes.toByteArray());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Uri uri = Uri.fromFile(f);
After that,
Where u r sending the email, do the following,
intent.putExtra(Intent.EXTRA_STREAM, uri);
It will work for sure. it worked for completely. Try it.
Well, the first problem is that even if that URI format is right (which I doubt), that file would be inside your application's sandbox, which is not accessible to the E-mail activity (or any other activity, for that matter). In any case, you're gonna have to write that file into the SD card and have the email program read it from there. You can output the bitmap using the following code:
Bitmap image = BitmapFactory.decodeResource(getResources(),R.drawable.image);
File file = new File(Environment.getExternalStorageDirectory(), "forEmail.PNG");
OutputStream outStream = new FileOutputStream(file);
image.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
Then use Uri.fromFile() to generate the URI from the file defined above:
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.fromFile(file));
(Code for resource to file adapted from here)
The other answers might work too (they didn't work for me though)! I got it running using only the one line of code bellow, which is very simple and easy. Thanks everyone.
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.image));
I think my issue was that I was putting the package name wrong, but using the getPackageName() method that issue is solved!

can't save file in android file system

I'm trying to capture a photo with the camera and save it (to be previewed later) and it seems to work with the emulator but when I use it on my GalaxyS - it doesn't save the file (I use RootExplorer to check) and there's no preview.
What am I doing wrong?
Code for saving the file:
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
try {
// Write to SD Card
String filename = "captured_image.jpg";
Log.d("##--File name--##", filename);
outStream = openFileOutput(filename, Context.MODE_WORLD_READABLE); // <9>
outStream.write(data);
outStream.close();
Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
} catch (FileNotFoundException e) { // <10>
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Log.d(TAG, "onPictureTaken - jpeg");
}
Code for displaying:
ImageView imagePrev = (ImageView) findViewById(R.id.image_capturedimagepreview_preview);
Bitmap bmp = null;
try {
bmp = BitmapFactory.decodeStream(openFileInput("captured_image.jpg"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
imagePrev.setImageBitmap(bmp);
i think i found the problem.
instead of outStream = openFileOutput(filename, Context.MODE_WORLD_READABLE); i should use outStream = getApplicationContext().openFileOutput(filename, Context.MODE_WORLD_READABLE);
but now i'm facing a new one - the file seems to be corrupted cause when i open it with the Android's viewer it's just black and its size is always 18474 bytes.
any ideas?
Where are you storing the image? Have you tried using an absolute path? Do you have the read/write external permission in the manifest?
I used something like this in my program to store an image in a directory the same as my package name.
File path = new File(Environment.getExternalStorageDirectory(), context.getPackageName() );
File imagePath = new File(path,"capture_image.jpg");
EDIT:
If its your first time using the sd card for your given package name you will need to create the directory before trying to write to it.

Categories

Resources