Theres a problem I can't seem to fix:
In my OnCreate in the CameraActivity, I delete the picture first if it's there. If there is a situation where this is done, the picture file is created but the picture is blank. (so only creates the picture successfully if the file isn't there in the first place). How do I delete the file and create it successfully?
My CameraActivity is defined as follows:
public class CameraActivity extends Activity
{
final int PICTURE_ACTIVITY = 1;
#Override
public void onCreate(Bundle savedInstanceState)
{
Intent h = getIntent();
String filename = h.getStringExtra("string") + ".jpg";
String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/" + getString(R.string.app_name)+ "/";
File newdir = new File(dir);
try{
newdir.mkdirs();
}
catch(Exception e){}
String file = dir + filename;
File newfile = new File(file);
boolean deleted = newfile.delete();
try {
System.out.println("creating:");
newfile.createNewFile();
} catch (IOException e) {}
Uri outputFileUri = Uri.fromFile(newfile);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
super.onCreate(savedInstanceState);
startActivityForResult(cameraIntent, PICTURE_ACTIVITY);
}
}
I realised the reason it wasn't saving sometimes was because I was taking the SD card out of the phone before it had the chance to save.
Related
int count=1;
if(block=="other(from camera)")
{
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
File newdir = new File(dir);
newdir.mkdirs();
int TAKE_PHOTO_CODE = 0;
count++;
file = dir+count+".jpg";
File newfile = new File(file);
try
{newfile.createNewFile();}
catch (IOException e) {}
Uri outputFileUri = Uri.fromFile(newfile);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
This is a code for taking the picture and saving it to the gallery.and I want to use the images which are getting stored.But unfortunately everytime I close the app and reopen it, the pictures starts again from 2.jpeg.Is there a way such that the pictures are taken in continuation even after reopening the app.
Hi Guys My application quits just after the image is saved, I cannot see why, Please help?
This is the button pressed method, after the picture is taken I press save, The image gets saved where it needs to be saved but the application just quits after I press save, It does not say "Not Responding", it just quits
cam.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
leakerID = leakId.getText().toString();
String direc = "/e3softData/DCIM/";
String fileName = leakerID+".jpg";
// get the path to sdcard
File sdcard = Environment.getExternalStorageDirectory();
// to this path add a new directory path
File dir = new File(sdcard.getAbsolutePath() + direc);
// create this directory if not already created
dir.mkdir();
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File file = new File(dir, fileName);
String f = file.toString();
Uri uriSavedImage = Uri.fromFile(new File(f));
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(intent, 0);
}
});
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File dir = new File(Environment.getExternalStorageDirectory() + "/e3softData/DCIM/");
if (!dir.exists()) {
dir.mkdir();
}
String fileName = leakerID+".jpg";
output = new File(dir.getAbsolutePath(), fileName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
startActivityForResult(intent, 0);
}
I am able to use my camera and take pictures by below:
public class ImportCard extends Activity {
ImageButton importimage;
Button btnglry, btnqr;
boolean taken;
String path;
protected static final String PHOTO_TAKEN = "photo_taken";
static Uri capturedImageUri=null;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_import_card);
path = Environment.getExternalStorageDirectory() + "/MapCards/"+System.currentTimeMillis()+".jpg";
String dir = Environment.getExternalStorageDirectory().getPath();
File imageDirectory = new File(dir);
imageDirectory.mkdirs();
importimage = (ImageButton)findViewById(R.id.importimage);
importimage.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
startCameraActivity();
}
});
}
protected void startCameraActivity()
{
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, 0);
}
protected void onPhotoTaken()
{
// Log.i( "MakeMachine", "onPhotoTaken" );
taken = true;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
}}
This method works. But i want my captured images to save in a default target folder such as "MapCards" (if no folder, then create). It is something to do with,
path = Environment.getExternalStorageDirectory() + "/MapCards/"+System.currentTimeMillis()+".jpg";
But all images are saved in "Camera" folder. Thank you for your helps.
Create your own folder in SD card & then pass the File Uri as extra with camera intent. This code should do the trick.
String SD_CARD_TEMP_DIR = Environment.getExternalStorageDirectory() + File.separator + "tempImage.jpg";
File file =new File(SD_CARD_TEMP_DIR);
takePictureFromCameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
Refer to Android's training-Taking Photos Simply.
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
is the only way to save image to custom file path. so what you should do:
1. check whether you have the correct saving folder
2. try to remove your sdcard to verify whether your code can work
3. Is your ROM customized? the 3rd-party ROM may not work well.
I have a fragment that takes a canvas drawing and saves it to external memory. I go into the device by connecting the USB and searching the file directory. I find it under Android/data/appname/files/img/nameofimage.png. Now I have a 2nd fragment that is saving pictures when the camera takes them but I can't find them.
Camera
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check that request code matches ours:
if (requestCode == CALL_BACK) {
// Check if your application folder exists in the external storage,
// if not create it:
File imageStorageFolder = new File(
Environment.getExternalStorageDirectory() + File.separator
+ "Camera");
if (!imageStorageFolder.exists()) {
imageStorageFolder.mkdirs();
Log.d("FILE",
"Folder created at: " + imageStorageFolder.toString());
}
// Check if data in not null and extract the Bitmap:
if (data != null) {
String filename = "image";
String fileNameExtension = ".jpg";
File sdCard = Environment.getExternalStorageDirectory();
String imageStorageFolderName = File.separator + "Camera"
+ File.separator;
File destinationFile = new File(sdCard, imageStorageFolderName
+ filename + fileNameExtension);
Log.d("FILE", "the destination for image file is: "
+ destinationFile);
if (data.getExtras() != null) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
try {
FileOutputStream out = new FileOutputStream(
destinationFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
Log.e("FILE", "ERROR:" + e.toString());
}
}
}
}
}
Canvas
capSig.setView(sign = new Sign(this.getActivity(), null))
.setMessage(R.string.store_question)
.setPositiveButton(R.string.save,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
try {
sign.setDrawingCacheEnabled(true);
sign.getDrawingCache()
.compress(
Bitmap.CompressFormat.PNG,
10,
new FileOutputStream(
new File(
getActivity()
.getExternalFilesDir(
"img"),
"signature.png")));
} catch (Exception e) {
Log.e("Error ", e.toString());
}
onClick
private class ClickListener implements View.OnClickListener {
#Override
public void onClick(View v) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, CALL_BACK);
}
}
I don't fully understand the code, why is this saving to a different location. Also what would I need to do to get it to save under Android/data/appname/files/Camera/ ?
edit
the logcat tells me its being saved here: 06-08 16:21:49.333: D/FILE(4818): the destination for image file is: /storage/emulated/0/Camera/image.jpg Which I assume is listed as private in the files and that's why I cant find it. This does not tell me why its being saved here though.
2nd Edit
Current Code
private class ClickListener implements View.OnClickListener {
#Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File sdCard = Environment.getExternalStorageDirectory();
String path = sdCard.getAbsolutePath() + "/Camera" ;
File dir = new File(path);
if (!dir.exists()) {
if (dir.mkdirs()) {
}
}
String FileName = "image";
File file = new File(path, FileName + ".jpg");
Uri outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, CALL_BACK);
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check that request code matches ours:
if (requestCode == CALL_BACK) {
Log.v("RESULT", "Picture Taken");
}
}
Try this code
private void TakePhoto() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File sdCard = Environment.getExternalStorageDirectory();
String path = sdCard.getAbsolutePath() + "/Camera" ;
File dir = new File(path);
if (!dir.exists()) {
if (dir.mkdirs()) {
}
}
String FileName = "image";
File file = new File(path, FileName + ".jpg");
Uri outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, TAKE_PICTURE);
}
Remember
You need this Permission in your Manifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I have a very simple APP which at the moment takes a picture and then saves the image. The problem at the moment is that for some reason i cannot find where the image is being saved to on the phone.
The finished outcome with what i am trying to do is when a picture is took the image then gets saved into a new folder that has been created on the SD Card, but if the folder does not already exist it will have to be made (automaticlly) before the image can be saved.
I have tried to use the answer in this question but cannot seem to incoporate it without getting the error imageIntent cannot be resolved
EDIT: Image now saving into SD Card and creating folder but overwriting the pervious image I need it to save multiple images if any one has any suggestions code has been updated
This is a snippet of my code:
PictureCallback myPictureCallback_JPG = new PictureCallback(){
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
/*Bitmap bitmapPicture
= BitmapFactory.decodeByteArray(arg0, 0, arg0.length); */
int imageNum = 0;
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Punch");
imagesFolder.mkdirs(); // <----
String fileName = "image_" + String.valueOf(imageNum) + ".jpg";
File output = new File(imagesFolder, fileName);
while (output.exists()){
imageNum++;
fileName = "image_" + String.valueOf(imageNum) + ".jpg";
output = new File(imagesFolder, fileName);
}
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(uriSavedImage);
imageFileOS.write(arg0);
imageFileOS.flush();
imageFileOS.close();
Toast.makeText(AndroidCamera.this,
"Image saved: ",
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();
}};
EDIT
Code has been updated to now save multiple images in a new folder created on SD Card.
I'm purely GUESSING you forgot the necessary code from the question part of what you linked to.
The question has the following line at the very top:
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
If you just copied the code from the answer, then you would have that error because the code in the answer does not contain the instantiation of imageIntent.
Let me know if you need anything further or if I'm just totally wrong.
UPDATE (regarding image being overwritten):
You are currently using "image_001.jpg" as the string that represents the image name.
Set a variable within your Class int imageNum = 0;
Then you need to use a while loop and increment the image number - or you can create a different name for the image based on the time - that is another way to do it.
String fileName = "image_" + String.valueOf(imageNum) + ".jpg";
File output = new File(imagesFolder, fileName);
while (output.exists()){
imageNum++;
fileName = "image_" + String.valueOf(imageNum) + ".jpg";
output = new File(imagesFolder, fileName);
}
//now save the file to the sdcard using output as the file
The above code - while not tested personally - should work.
try {
super.onCreate(savedInstanceState);
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "Your Floder Name"+ File.separator);
root.mkdirs();
sdImageMainDirectory = new File(root, "myPicName.jpg");
outputFileUri = Uri.fromFile(sdImageMainDirectory);
startCameraActivity();
} catch (Exception e) {
Toast.makeText(this, "Error occured. Please try again later.",
Toast.LENGTH_SHORT).show();
finish();
}
}
protected void startCameraActivity() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, 101);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==101 && resultCode==-1)
{
try
{
Uri outputFileUri= data.getData();
selectedImagePath=getPath(outputFileUri);
}
catch(Exception ex)
{
Log.v("OnCameraCallBack",ex.getMessage());
}
}
You can save multiple image. you did one mistake every time when you are capturing the photo the folder is recreating again and again.
so you have to check whether the folder is already exist or not. you have to add only one line of code
if (!imagesFolder.exists()) {
imagesFolder.mkdirs();
}
else { //do nothing}