package ar.camera;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import static android.provider.MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE;
public class MainActivity extends AppCompatActivity {
static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 1;
private Uri fileUri;
//create the file URI
private static Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
private static File getOutputMediaFile(int type){
final File mediaStorageDir;
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "EMFDetectingApp");
} else{
mediaStorageDir = new File ("/storage/sdcard0/EMFDetectingApp");
}
if( !mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("EMFDectectingApp", "failed to create directory");
return null;
}
}
//create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile = null;
if(type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
return mediaFile;
} else{
return null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
//create the uri of a file to save the image
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
//specifying the path and file name for teh received image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE){
if(resultCode == RESULT_OK){
//image cpatured and saved to fileUri specifies in the Intent
Toast.makeText(this, "Image successfully saved", Toast.LENGTH_SHORT).show();
}
else if(resultCode == RESULT_CANCELED){
Toast.makeText(this, "Image capture cancelled", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(this, "Image capture failed", Toast.LENGTH_SHORT).show();
}
}
}
}
I've created an app which uses the camera to take and image then save it. However when I run the above code, I can take a picture but the image does not get saved. I cant find it in gallery or SD card. Can anyone find the mistake in this code?
Can anyone find the mistake in this code?
You are calling startActivityForResult()
before you are done setting up the Intent
— call putExtra()
before calling startActivityForResult()
Never hardcode paths (eg, /storage/sdcard0/EMFDetectingApp
), as such paths may not exist on all devices.
You cannot write to /storage/sdcard0/
on Android 4.4+ devices, if that is removable storage .
You cannot use Uri.fromFile()
on Android 7.0+ devices, if your targetSdkVersion
is 24 or higher
Your photo will not be visible to Windows/macOS/Linux, or to most gallery apps, until you have MediaScannerConnection
index the photo .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.