Trying to save image to gallery - android

I am new to Android development and are a bit stuck trying to save a image to the gallery. I have followed the guidelines on https://developer.android.com/training/camera/photobasics, and it works fine until I try to save the image to the gallery on the phone using getExternalStoragePublicDirectory.
When I use the getExternalFilesDir() all works, but when i try to use getExternalStoragePublicDirectory to get the picture to the gallery Ii got a NullPointerException.
I have added the WRITE_EXTERNAL_STORAGE permission to the manifest.
I have tried to read other posts about this, but havent found any soulutions to this problem.
Heres my code. If annyone culd help me finding out whats cousing the NullPointerException I would be thankful.
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
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);
}
}
}
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
And heres my error in Logcat:
08-02 20:04:12.920 17455-17455/com.example.android.fishlist E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.fishlist, PID: 17455
java.lang.NullPointerException
at java.io.File.<init>(File.java:282)
at com.example.android.fishlist.NewCatch.galleryAddPic(NewCatch.java:192)
at com.example.android.fishlist.NewCatch.access$200(NewCatch.java:45)
at com.example.android.fishlist.NewCatch$2.onClick(NewCatch.java:101)
at android.view.View.performClick(View.java:6891)
at android.widget.TextView.performClick(TextView.java:12651)
at android.view.View$PerformClick.run(View.java:26083)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

Related

Not able to get taken Photo using default camera of ASUS_X01BDA in my app

In my app I have created a option for taking Photo and Viewing taken Photos.
For taking Photos I am using default camera of mobile.
I am using below code for passing intent
public void intentForTakePicture() {
Intent picIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (picIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
ex.printStackTrace();
Log.e(LOG_TAG,"Exception while trying to create image file!!");
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.app.photo.fileprovider",
photoFile);
picIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(picIntent, TAKE_PHOTO);
Log.d(LOG_TAG,"request code is: "+TAKE_PHOTO);
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir("Pictures/photo/");
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
Log.d(LOG_TAG, "currentPhotoPath: " + currentPhotoPath);
return image;
}
In onActivityResult I am setting imageview whatever photos comes there
But the issue is, I am able to take photos but taken Photo is not getting back in onActivityResult.
Note: Getting this issue only for ASUS_X01BDA mobile.
I also checked the mobile camera permissions for my app. Got camera permission for my app.
I doesn't know the problem.
Please Anybody help me to find the cause and to solve it

How to take photo and save the result into Pictures on Andrdoid 10?

Google introduced Scoped Storage on Android 10, according to the document, we can write to the public directories without requesting permission by using MediaStore API.
Before Android 10, when we need to take a photo and save it to Pictures, code is like below:
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 + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
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;
}
We create the file directly in the Pictures, and put the file uri into the takePhotoIntent extra, while we start the intent and take a photo, original-size photo will be saved into the Pictures directory.
But now we are targeting android 10, according to the doc, it is not nessasary to request the WRITE_EXTERNAL_STORAGE permission to save a photo into the Pictures directory.
Without the permission, we can't create the file and then get the uri of the file, put it into the intent extra. So how can we save the original-size photo?

Load images from folder to ImageView

I am developing an application that captures photo and saves it in internal device folder named "photo".I need to load the photo from "photo" folder to imageview.There is only one photo present in the folder.How to do this?
Please help me.Thank you in advance.
Below is the code that I have tried which loads photo from folder only if name of photo is displayed.
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/GeoPark/final_photo/20180504_002754.jpg";
File imgFile = new File(path);
if (imgFile.exists()) {
imageView.setImageBitmap(decodeFile(imgFile));
}
else
Toast.makeText(ViewActivity.this,"No Image File Present ",Toast.LENGTH_SHORT).show();
Try this method .. in below method give proper file path.
private void loadImageFromStorage(String path)
{
try {
File f=new File(path, "profile.jpg");
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
ImageView img=(ImageView)findViewById(R.id.imgPicker);
img.setImageBitmap(b);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
Second things ..
File file = ....
Uri uri = Uri.fromFile(file);
imageView.setImageURI(uri);
alos i hope you add below permission into android manifest file ..
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
You could simply do a search about this, guaranteed results.
Anyway, Glide will help you with that
EDIT :
Taking picture using camera:
private void dispatchTakePictureIntent(Context context) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(context.getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile(context);
} catch (IOException ex) {
// Error occurred while creating the File
Snackbar.make(btn_open_camera, "Couldn't create a file for your picture!", Snackbar.LENGTH_LONG);
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(context,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, MY_PERMISSIONS_REQUEST_CAMERA);
}
}
}
private File createImageFile(Context context) throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = context.getFilesDir();
Log.i(TAG, "StorageDir : " + storageDir);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
Log.i(TAG, "mCurrentPhotoPath : " + mCurrentPhotoPath);
return image;
}
Now you have the image path stored in mCurrentPhotoPath which is a String declared in current activity. As you said, you will need this in next activity, to do that you can take a look at this answer

App is crashing when taken picture using ACTION_IMAGE_CAPTURE on some devices?

I have facing some strange behaviour with my Application : On Some device like samsung sony Onplus my application work fine but some device like
Lenovo a7000
redmi x
Application is crashing and I am getting this error
My code is :
private void captureImageFromCamera() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(this.getPackageManager()) != null) {
photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException e) {
e.printStackTrace();
}
if (photoFile != null) {
Uri photoURI;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
photoURI = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
} else {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
}
startActivityForResult(takePictureIntent, Constant.REQUEST_TAKE_PHOTO);
}
}
}
Here I am creating image file
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
imageFileName = "IMAGE_" + timeStamp + "_";
// File storageDir = this.getExternalFilesDir(Environment.DIRECTORY_PICTURES + "/PhotoGallery/");
File storageDir = this.getExternalFilesDir(Environment.DIRECTORY_PICTURES );
File image = File.createTempFile(
imageFileName,
".jpg",
storageDir
);
mCurrentPhotoPath = image.getAbsolutePath();
Log.d("imageFileName", imageFileName);
Log.d("imageFile", image.toString());
return image;
}
This is my OnActivityResult:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Constant.REQUEST_TAKE_PHOTO && resultCode == Activity.RESULT_OK) {
try {
setPic();
} catch (IOException e) {
e.printStackTrace();
}
}
}
And Here I am setting Pic by sending to another activity:
private void setPic() throws IOException {
Intent intent = new Intent(MainActivity.this, EditImageActivity.class);
intent.putExtra("mCurrentPhotoPath", compressImage(mCurrentPhotoPath,MainActivity.this));
intent.putExtra("CalledBy","Camera");
startActivity(intent);
}
There are another person who faced the same problem . Have the found the answer yet. I tired out a lot of question on stackoverflow but not of them work for me ,
Please let me know about this issue why in some devices is works fine and some devices that I mention above not,
Any Help is appreciated
I found two solution to your problem 1st One and 2nd One is below
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Create a File reference to access to future access
photoFile = getPhotoFileUri(photoFileName);
// wrap File object into a content provider
// required for API >= 24
// See https://guides.codepath.com/android/Sharing-Content-with-Intents#sharing-files-with-api-24-or-higher
Uri fileProvider = FileProvider.getUriForFile(MyActivity.this, "com.codepath.fileprovider", photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileProvider);
// If you call startActivityForResult() using an intent that no app can handle, your app will crash.
// So as long as the result is not null, it's safe to use the intent.
if (intent.resolveActivity(getPackageManager()) != null) {
// Start the image capture intent to take photo
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
where
public File getPhotoFileUri(String fileName) {
// Get safe storage directory for photos
// Use `getExternalFilesDir` on Context to access package-specific directories.
// This way, we don't need to request external read/write runtime permissions.
File mediaStorageDir = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), APP_TAG);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()){
Log.d(APP_TAG, "failed to create directory");
}
// Return the file target for the photo based on filename
File file = new File(mediaStorageDir.getPath() + File.separator + fileName);
return file;
}
I am also facing this problem. New devices require FLAG_GRANT_READ_URI_PERMISSION and FLAG_GRANT_WRITE_URI_PERMISSION and for devices with Api > 24 you have to implement provider

unfortunately camera has stopped working in Kitkat

I am trying to integrate taking a picture in Android. While running the application I am getting and error "Unfortunately, Camera has stopped", but my application didn't crash. I found this issue in Android "KITKAT". Here is the sample code I am using to take picture,
Using this function I am taking a picture,
private void take_picture_intent() {
Intent takePictureIntent = (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT)
? new Intent(MediaStore.ACTION_IMAGE_CAPTURE_SECURE)
: new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(this.getActivity().getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException e) {
e.printStackTrace();
}
if (photoFile != null) {
photoURI = FileProvider.getUriForFile(this.getActivity(), "com.android.myapplication.fileprovider", photoFile);
Log.e(TAG, "photoFile: "+photoFile+" ,URI : "+photoURI.getPath());
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
getActivity().startActivityForResult(takePictureIntent, SelectMedia.IMAGE_CAPTURE_AT_THE_DOOR);
}
}
}
This function is used to generate Image path,
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
if (!storageDir.exists())
storageDir.mkdirs();
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
Here is the Log,
10-11 12:35:12.691 18815-18815/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sec.android.app.camera, PID: 18815
java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider from
ProcessRecord{42d78c60 18815:com.sec.android.app.camera/u0a84} (pid=18815, uid=10084) that is not exported from uid 10786
at android.os.Parcel.readException(Parcel.java:1472)
at android.os.Parcel.readException(Parcel.java:1426)
at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3201)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:4824)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2532)
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1425)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:906)
at android.content.ContentResolver.openOutputStream(ContentResolver.java:669)
at android.content.ContentResolver.openOutputStream(ContentResolver.java:645)
at com.sec.android.app.camera.Camera$LastContentUriCallback.onCompleted(Camera.java:21369)
at com.sec.android.app.camera.Camera.onLaunchGalleryForImage(Camera.java:12409)
at com.sec.android.app.camera.Camera.onImageStoringCompleted(Camera.java:11140)
at com.sec.android.app.camera.CommonEngine.imageStoringCompleted(CommonEngine.java:6954)
at com.sec.android.app.camera.CeStateInitialized.handleMessage(CeStateInitialized.java:47)
at com.sec.android.app.camera.CommonEngine$StateMessageHandler.handleMessage(CommonEngine.java:957)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5590)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
10-11 12:35:13.021 600-19132/? E/android.os.Debug: !#Dumpstate > sdumpstate -k -t -z -d -m 18815 -o /data/log/dumpstate_app_error
I found this issue is due to conversion of URI. I have resolved this issue by replacing
FileProvider.getUriForFile(getApplicationContext(), "com.android.myapplication.fileprovider", photoFile);
By Adding a condition,
if ((Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT))
photoURI = FileProvider.getUriForFile(getApplicationContext(), "com.android.myapplication.fileprovider", photoFile);
else
photoURI = Uri.fromFile(photoFile);
on take_picture_intent() function.
Also In my Manifest I am have removed permission,
<uses-permission android:name="android.permission.CAMERA" />
Add the permission for pre-lollipop versions of specific package name, after that you can able to access it.
getApplicationContext().grantUriPermission(getCallingPackage(),
contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);

Categories

Resources