Welcome to EasyCoding with AmmaraSubscribe My Coding Channel:https://www.youtube.com/channel/UC8UsfNYmbKiRJvI9ZhhApEw?view_as=subscriber
Subscribe my personal YouTube Channel:https://www.youtube.com/channel/UCa93R2oNQMuX0fOKa9eV4zg?view_as=subscriber
Let's StartWatch Complete Camera Video:https://www.youtube.com/watch?v=WL9okRCtwG8&list=PLTR7MDYHvzpNJXRUUn0mbUzCfhoJe7Xc9
Description:
In the previous post, I show you how to implement camera in your app, how to take image using camera and how to show that image to imageview. Now I am going to show you how to store that image to internal storage of mobile. So Let's Start.1) Paste code to Manifest.
2) Paste code to MainActivity.java.
public class MainActivity extends Activity {
File photoFile = null;
ImageView imageView;
static final int REQUEST_TAKE_PHOTO = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=findViewById(R.id.imageView);
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
String currentPhotoPath;
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
Toast.makeText(getApplicationContext(),"path "+imageFileName,Toast.LENGTH_LONG).show();
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
Toast.makeText(getApplicationContext(),"path "+storageDir,Toast.LENGTH_LONG).show();
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
return image;
}
public void OpenImage(View view){
dispatchTakePictureIntent();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
Uri fileUri = Uri.fromFile(photoFile)
imageView.setImageURI(fileUri);
}
}
}
3) Paste Code to MAinActivity.xml.
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
4) Right click on res -> then go to new -> then click on Android Resource Directory -> then select Resource Type = xml -> then click ok.
5) Now right click on xml -> new -> xml resource directory -> write name file_path.
6)Paste code to file_path.
7) write Code to Manifest, Above closing tag of application. (above this ).
android:authorities="com.example.android.fileprovider"
android:enabled="true"
android:grantUriPermissions="true">
android:resource="@xml/file_paths">