I am working now on application that start the camera and take a photo, I didn't use camera activity, but I wrote my camera app and I want to save the taken image in the internal phone storage in folder called "temPic"
The following code generate the folder and the image, but when I checked the tempPic I found an image called image1.jpg and it's size is 461115 ( I tried to store the image in SDcard directory and it is the same size), but when I double clicked it a black image appeared, not the taken one although in SDcard I opened it !!!
FileManipulator fileFormat = new FileManipulator(
getApplicationContext());
String path = fileFormat.createFolder_PStrg("tempPic") + "/image1.jpg";
File file = new File(path);
Uri outputFileUri = Uri.fromFile(file);
OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(outputFileUri);
imageFileOS.write(arg0);
imageFileOS.flush();
imageFileOS.close();
Toast.makeText(AndroidCamera.this,
file.length()+"",
Toast.LENGTH_LONG).show();
Toast.makeText(AndroidCamera.this,
"Image saved: " + outputFileUri.toString(),
Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
camera.startPreview();
I don't know if this can help you but this is how I save a file to the SD card and it works.
public void saveToSD(Bitmap outputImage){
File storagePath = new File(Environment.getExternalStorageDirectory() + "/MyPhotos/");
storagePath.mkdirs();
File myImage = new File(storagePath, Long.toString(System.currentTimeMillis()) + ".jpg");
try {
FileOutputStream out = new FileOutputStream(myImage);
outputImage.compress(Bitmap.CompressFormat.JPEG, 80, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Why don't you use the camera activity and save images to internal storage by creating temp folder that has writable permission which allow other application to write to this folder and take the image to its path and then after the camera activity return you can move the image to another internal storage folder but with private permission as follow
//the temp folder to start the camera activity with
String path = getDir("images", Context.MODE_WORLD_WRITEABLE).getPath() + "/test.jpg";
//start the camera activity
File file = new File(path);
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, CAMERA_ACTIVITY);
and in onActivityResult method move it to folder with private permission it will work fine as I tested it
Related
I am a beginner android programmer. I create a project for hiding image. But, I have a problem in my project. Is that:
I use a method to move a photo from the folder A to folder .B(Here is how I hid it from image gallery) . I am sure that the picture in the folder A was deleted and moved to folder .B. However, when I open image gallery application I still see this picture is displayed at the folder A.
This is method copy picture to folder .B:
public static String copyFile(String path) {
//TO DO: create folder .B
File pathFrom = new File(path);
File pathTo = new File(Environment.getExternalStorageDirectory() + "/.B");
File file = new File(pathTo, fileToName);
while (file.exists()) {
fileToName = String.valueOf(System.currentTimeMillis());
file = new File(pathTo, fileToName);
}
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(pathFrom);
out = new FileOutputStream(file);
byte[] data = new byte[in.available()];
in.read(data);
out.write(data);
in.close();
out.close();
return file.getPath();
} catch (FileNotFoundException e) {
Log.e(TAG, e.getMessage());
return "error:" + e.getMessage();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
return "error:" + e.getMessage();
}
}
After copy picture to folder .B, I delete this picture in folder A:
new File(path).delete();
So Is there any suggestion for notify for all image gallery know that this picture was moved to another folder or another URI?
**UPDATE: The suggestion for me work fine is:
Before 4.4,you can call this:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://"+Environment.getExternalStorageDirectory())));
After 4.4,try this:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file)));
//the file is new image's path
THank FireSun and everyonce
After change imgae path,you should notify the gallery to update,so you should send a broadcast to make it.
Before 4.4,you can call this:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://"+Environment.getExternalStorageDirectory())));
After 4.4,try this:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file)));
//the file is new image's path
Why don't you use
pathFrom.delete();
This question already has answers here:
Deleting a gallery image after camera intent photo taken
(13 answers)
Closed 9 years ago.
I am working on a project where I need to save images and videos in application internal memory only (not in SDCard or device Gallery). I am using below code. But this also save the image/video to device gallery. Please advice.
Bitmap bm;
View v=imageview;
v.setDrawingCacheEnabled(true);
bm=Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false);
String fileName="image.png";
try
{
FileOutputStream fOut=openFileOutput(fileName, MODE_PRIVATE);
bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
}
catch (Exception e)
{
e.printStackTrace();
}
I want to make the image and video private and secured. So I want to save them in apps internal memory. So no other app can access it. Please suggest.
Storing the images in internal memory, it will store the image on internal images folder.
Uri uriSavedImage;
// the temp folder to start the camera activity with
String fileName = "image_" + String.valueOf(imageNum)+ ".png";
path = getDir("images", Context.MODE_WORLD_WRITEABLE).getPath() + "/" + fileName;
// start the camera activity
file = new File(path);
while (file.exists()) {
imageNum++;
fileName = "image_" + String.valueOf(imageNum)+ ".png";
path = getDir("images_pho",Context.MODE_WORLD_WRITEABLE).getPath()+ "/" + fileName;
file = new File(path);
}
Uri ur = Uri.parse(file.toString());
uriSavedImage = Uri.fromFile(file);
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(uriSavedImage);
imageFileOS.write(data);
imageFileOS.flush();
imageFileOS.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
hope its helps
As I told you, you can store the picture taken from the camera in the internal storage and immediately you can delete the taken image from the gallery with getContentResolver().delete(uri, null, null); or you can use this :
if (takenpicturefile.exists())
if (takenpicturefile.delete())
Log.d("tag","filedeleted");
Fore more details, you might need to refer this. Hope it helps.
I created a class openPDF which takes a byte array as input and displays the PDF file with Adobe Reader. Code:
private void openPDF(byte[] PDFByteArray) {
try {
// create temp file that will hold byte array
File tempPDF = File.createTempFile("temp", ".pdf", getCacheDir());
tempPDF.deleteOnExit();
FileOutputStream fos = new FileOutputStream(tempPDF);
fos.write(PDFByteArray);
fos.close();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(tempPDF);
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} catch (IOException ex) {
String s = ex.toString();
ex.printStackTrace();
}
}
When I pass the intend , the error from adobe reader is "Invalid file path". I read all other posts related to downloading and viewing PDF in android but dint help much. Any suggestions?
I think the issue is that other apps have no access to the files in your app's private data area (like the cache dir).
Candidate solutions:
changing the file's mode to MODE_WORLD_READABLE so that it can be read by other apps
...
String fn = "temp.pdf";
Context c = v.getContext();
FileOutputStream fos = null;
try {
fos = c.openFileOutput(fn, Context.MODE_WORLD_READABLE);
fos.write(PDFByteArray);
} catch (FileNotFoundException e) {
// do something
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fos!=null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
String filename = c.getFilesDir() + File.separator + fn;
File file = new File(filename);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
...
or write the pdf file to the /sdcard partition.
you can use android.os.Environment API to get the path, and remember to add the permission to your app's AndroidManifest.xml file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Regards
Ziteng Chen
I made this code to open an especific .pdf file existing in Dowloads folder with Adobe's application
File folder = new File(Environment.getExternalStorageDirectory(), "Download");
File pdf = new File(folder, "Test.pdf");
Uri uri = Uri.fromFile(pdf);
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage("com.adobe.reader");
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);
It works for me. So i guess your problem can be the temprorary file. Try to write the file to sdcard. To do this you will need add android.permission.WRITE_EXTERNAL_STORAGE to your AndroidManifest.xml.
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();
}
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.