Gautam
Gautam

Reputation: 37

Android Camera Image Name

I'm trying to build a simple application which captures images. I had successfully implemented the application till Android M. I faced the FileUriExposedException on Android N but then I could solve it with the help of this link which suggested this code `

capture_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if(camera_intent.resolveActivity(getPackageManager())!=null){
                    ContentValues values = new ContentValues(1);
                    values.put(MediaStore.Images.Media.MIME_TYPE,"image/jpg");
                    fileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);
                    camera_intent.putExtra(MediaStore.EXTRA_OUTPUT,fileUri);
                    camera_intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | 
                                           Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                    startActivityForResult(camera_intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
                }
                else {
                    Toast.makeText(Visitor_Details.this,"ERROR",Toast.LENGTH_LONG).show();
                }
            }
        });

`

This piece of code stores the Image in Pictures Folder with a random 13 digit integer as its image name eg:- 1507922385727.jpg. I tried reading the fileUri via Log.d() but the value returned by the Log and the Image name were not same. How is this image name being generated? How do I store this image name so that I can pass this via a intent to display the image in the next Activity? Lastly, how do I create a sub folder inside the Pictures where I could direct these images? PS :- Previously, I was using SimpleDateFormat to get a TimeStamp as my Image Name and I created the Folder using File class.The getImageName() just returns the timestamp value. Here's the code I used for previous version of Android. `

btnCapture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {



                Intent camIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File imageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"Camera Test");
                String imageName = getImageName();
                File imagePath = new File(imageDir,imageName);
                Uri imageUri = Uri.fromFile(imagePath);
                camIntent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
                startActivityForResult(camIntent,1);

            }

`

Upvotes: 0

Views: 799

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007099

How is this image name being generated?

That is up to the MediaStore. Use your original code, but use FileProvider to serve up access to the file location and get your Uri. See this sample app for how to use FileProvider with ACTION_IMAGE_CAPTURE.

Upvotes: 1

Related Questions