Lalit Jadav
Lalit Jadav

Reputation: 1427

Camera is not saving after taking image

Camera is not saving after taking image in Redmi 1s. This works great in samsung j5-2016. I am trying to take picture and upload on server. Before uploading i am compressing image, so i need filename and file path. How I get fileName is displays in activity result method.

Camera Intent

val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
                        if (takePictureIntent.resolveActivity(activity.packageManager) != null) {
                            startActivityForResult(takePictureIntent, 102)
                        }

In samsung phone this saves picture, thus i am able to get file from uri. But in redmi 1s i am not able to get uri. I think it seems like redmi 1s not saving taken picture, therefore i am not able to get file. OnActivityResult in fragments displays as:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 102) && resultCode == Activity.RESULT_OK && data != null) {
        val uri = data.data

                val mimeType = activity.contentResolver.getType(uri)
                if (mimeType == null) {
                    val path = getPath(uri)
                    if (path == null) {
                        fileName = FilenameUtils.getName(uri.toString())
                    } else {
                        val file = File(path)
                        fileName = file.getName()
                    }
                } else {
                    val returnUri = data.getData()
                    val returnCursor = activity.contentResolver.query(returnUri, null, null, null, null)
                    val nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
                    val sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE)
                    returnCursor.moveToFirst()
                    fileName = returnCursor.getString(nameIndex)
                    val size = Long.toString(returnCursor.getLong(sizeIndex));
                }
}
    }

I am getting error in

val mimeType = activity.contentResolver.getType(uri)

ErrorLog

08-23 17:17:52.039 12008-12008/? W/System.err: java.lang.NullPointerException
08-23 17:17:52.039 12008-12008/? W/System.err:     at android.content.ContentResolver.acquireExistingProvider(ContentResolver.java:1405)
08-23 17:17:52.039 12008-12008/? W/System.err:     at android.content.ContentResolver.getType(ContentResolver.java:304)
08-23 17:17:52.039 12008-12008/? W/System.err:     at in.motiontech.wave.fragment.ReviewOrderFragment.onActivityResult(ReviewOrderFragment.kt:396)
08-23 17:17:52.039 12008-12008/? W/System.err:     at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:164)
08-23 17:17:52.039 12008-12008/? W/System.err:     at android.app.Activity.dispatchActivityResult(Activity.java:5434)
08-23 17:17:52.039 12008-12008/? W/System.err:     at android.app.ActivityThread.deliverResults(ActivityThread.java:3363)
08-23 17:17:52.039 12008-12008/? W/System.err:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3410)
08-23 17:17:52.039 12008-12008/? W/System.err:     at android.app.ActivityThread.access$1300(ActivityThread.java:141)
08-23 17:17:52.039 12008-12008/? W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1260)
08-23 17:17:52.039 12008-12008/? W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
08-23 17:17:52.039 12008-12008/? W/System.err:     at android.os.Looper.loop(Looper.java:136)
08-23 17:17:52.049 12008-12008/? W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5052)
08-23 17:17:52.049 12008-12008/? W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
08-23 17:17:52.049 12008-12008/? W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
08-23 17:17:52.049 12008-12008/? W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
08-23 17:17:52.049 12008-12008/? W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
08-23 17:17:52.049 12008-12008/? W/System.err:     at dalvik.system.NativeStart.main(Native Method)

Help me if I am doing anything wrong please suggest other option.

I have also looked some Stackoverflow posts but not able to find answer :

Camera Intent not saving photo

Camera is not saving after taking picture

Can not save camera's captured photo in application's "files" folder

Edit I have also tried following for Camera Intent

val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
                        val dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
                        var output = File(dir, System.currentTimeMillis().toString() + ".jpeg")
                        if (takePictureIntent.resolveActivity(activity.packageManager) != null) {
                            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, output)
                            startActivityForResult(takePictureIntent, 102)
                        }

Upvotes: 1

Views: 2309

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006819

Camera is not saving after taking image in Redmi 1s.

It will not "save" anything with most camera apps. You specifically set up your ACTION_IMAGE_CAPTURE Intent to not save a photo anywhere.

This works great in samsung j5-2016

Only because you are using a buggy camera app on that device.

ACTION_IMAGE_CAPTURE is not supposed to return a Uri. It it supposed to do one of two things:

  1. If you include EXTRA_OUTPUT on the ACTION_IMAGE_CAPTURE Intent, the camera app is supposed to save a full-resolution photo to the Uri that you provide in that extra

  2. Otherwise, you get a Bitmap back via getParcelableExtra("data") on the returned Intent

Since you do not have EXTRA_OUTPUT, you should not expect a full-resolution photo to be saved anywhere.

This sample app shows how to use EXTRA_OUTPUT.

Upvotes: 4

Related Questions