Android Application quits after saving picture - android

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

Related

How to create separate folder for captured images with losing quality

Hi i am trying to create separate folder for captured images with out losing quality using below code but i am getting exception android.os.FileUriExposedException: file:///storage/emulated/0/myFolder/photo_20180504_102426.png exposed beyond app through ClipData.Item.getUri()
what did do mi-stack can some one correct my code
code:
String folder_main = "myFolder";
File f = new File(Environment.getExternalStorageDirectory(), folder_main);
if (!f.exists()) {
f.mkdirs();
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File file = new File(Environment.getExternalStorageDirectory(), "/myFolder" + "/photo_" + timeStamp + ".png");
imageUri = Uri.fromFile(file);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, Constants.CAMERA_REQUEST_CODE);
private void onCaptureImageResult(Intent data) {
try {
Bitmap thumbnail = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
CircleImageView circleImageView = findViewById(formFields.get(imagePosition).getId());
circleImageView.setImageBitmap(thumbnail);
}
Put This on Your oncreate()
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());

Saving images in a folder

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.

Cannot save the Picture in the given location

I am a beginner in android programming.
Here is a code to take a image and save it in a folder in sdcard. The image is saved in gallery but it is not saved in the location where i want to. Please help...
public class CameraActivity extends Activity {
/** Called when the activity is first created. */
Button button1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1=(Button)findViewById(R.id.button1);
}
public void send(View v)
{
Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(
Environment.getExternalStorageDirectory(),
"MyImages");
imagesFolder.mkdirs(); //
File image = new File(imagesFolder, "image_001.jpg");
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(imageIntent,0);
}
}
Finally I got the solution, the modified code is:
File image = new File("/sdcard/picture.jpg");
Uri uriSavedImage = Uri.fromFile(image);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
You should append a / to your MyImages string. eg,
new File(Environment.getExternalStorageDirectory() + "/MyImages/");
That should create a reference to a directory, not a file as it currently is.

Problems deleting and creating file on SD card

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.

Camera intent android

When I begin the camera intent I give it a file name I would like it to use. When I get this on the phone it uses the phones default file name. Which is no help as I need the image name later in the app.
Camera intent code...
public void onClick(View view) {
String currentDateTimeString = DateFormat.getDateInstance().format(new Date());
System.out.println(currentDateTimeString);
filename = ("/sdcard/" + currentDateTimeString + ".jpg");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(), filename);
outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, TAKE_PICTURE);
filetype = "image/jpeg";
}
James,
Here is the function I use to capture an image and save it to a location of my choosing (sub folder with my app name).
public void imageFromCamera() {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Log.d(TAG, "No SDCARD");
} else {
mImageFile = new File(Environment.getExternalStorageDirectory()+File.separator+"MyApp",
"PIC"+System.currentTimeMillis()+".jpg");
mTempImagePath = mImageFile.getAbsolutePath();
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mImageFile));
startActivityForResult(intent, TAKE_PICTURE);
}
}

Categories

Resources