Open camera after android 7.0 to take avatar upload

Today, I'm doing android development related to this avatar. I keep looking for data on the Internet and don't know anything. Then I copy it.

What's the problem? Now android can open a camera without simply asking for it. According to the data I found today, the URI of the file can't be used outside of this APP, so a conversion is needed. Then I found a way to get the URI directly and shoot successfully. But at night I don't know what I did, and suddenly I started to report that SQLite has UNIQUE restrictions. I don't understand anything. It was also a check because some SQLIte columns can't allow duplicates. So I want to say that it's not my pictures can't have the same storage path. I should check it out and find it's still unfortunate or the same error. So I looked for another way.

Provider provides a URI to be used by applications other than this APP. Say nothing more, let's go directly to the code below. Actually, I don't know what's wrong with this method at first. To tell the truth, I don't know what's wrong with it now, but I finally got it out after a search.

Reference resources:

1. Choose how to get photos ()

                Log.e("TAG","open camera to get a pic");
                Log.e("TAG", "start camera");
                Toast.makeText(MainActivity2.this, "choose a pic from photo", Toast.LENGTH_SHORT).show();

2. Processing started:

 private void from_camera(int fromCamera) {

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        //Here I'm checking to see if my avatar folder already exists
        //I chose to save it on my phone's SD card, External Storage. Note the following, as some of your configurations will be covered later
        //Picture File
        File mTmpFile = new File(Profile_Dir, "/"+getName()+".png");
        Uri uri ;
        //Check this version, it's very complicated, otherwise it doesn't need much trouble
        if (Build.VERSION.SDK_INT >= 24){
            //Get URI
            uri = FileProvider.getUriForFile(MainActivity2.this,
                    "Advance", //This is optional, but it should correspond to your configuration in time.  
            uri = Uri.fromFile(mTmpFile);
        Log.e("TAG", String.valueOf(uri));
        //Turn on the camera


Below is the corresponding configuration instructions above:

First, register the provider(FileProvider) in Manifest:

<--!  Notice this name Different versions of Kennedy have some differences, and I have had a bit of pain on it, so I can go online to find out what is now-->
            <--!this authorites That's what I said above. I'm taking my own package name here. Remember to be the same-->
<--!  The rest can be the same as mine-->

Android:resource="@xml/file_paths"/>Create a new XML folder under your res filesystem and create a file_inside accordingly Files for paths

<paths xmlns:android="">
    <external-path name="camera" path="APP Head portrait"/>

Remember when I said I saved pictures in my SD card? Here you need to create a label <external-path, which indicates Environment.getExternalStorage() this path, and then write down the folders in the SD card you want to share in the path inside. If it doesn't seem to be sharing all the folders, I don't think so, because I relentlessly say that he made a mistake, "Whoops are always wrong", that name doesn't matter. It's just a virtual folder.

Okay, that's it. If the SD card I save on is this external-path tag, then the rest corresponds to:

<files-path /> = getFilesDir()
<cache-path /> = getCacheDir()
<external-path /> = Environment.getExternalStorageDirectory()
<external-files-path /> = Context#getExternalFilesDir (String) or Context. GetExternal FilesDir (null)
<external-cache-path /> = Context.getExternalCacheDir()
<external-media-path /> = Context.getExternalMediaDirs() This is what I found here, because I didn't understand it at first, then I used the <files-path/>tag above, and then I made a mistake. Oops, I can't find what that means, maybe my shared file doesn't have the same root directory as the one under my picture file. Is that not obvious, my picture is on a SD card, Then the file-path is getFileDir(), and the root is not SD soon, so I'll be ready when I come!

3. Then it comes back after the shooting:

else if (requestCode == FROM_CAMERA) {
                //handle data from camera and set the background of your profile
                //This is the path to the file that was set up at that time and can be read directly from the storage.
                File pic = new File(Profile_Dir,"/"+getName()+ ".png");
                Log.e("TAG", "camera");
                //This is a downsizing of this one. I pasted the code below and found it on the Internet. No problem, I can do it once I try.

public void cropPhoto(Uri uri) {

        Intent intent = new Intent("");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        // aspectX aspectY is the ratio of width to height
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY is the width and height of the cropped picture
        intent.putExtra("outputX", 250);
        intent.putExtra("outputY", 250);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, CROP_PIC);

4. Set the picture as a avatar after cropping, also handle the result of jumping back

else if(requestCode==CROP_PIC){
                //set the pic then
                Bundle b=data.getExtras();
                Bitmap head=b.getParcelable("data");
                //Set up your Avatar
                //This is to save the avatar, and I also saved it in the SD card instead of the photo I added up to take

 public  void Bit2File(Bitmap bitmap){
        //Photo path (which you want to save)
        File file=new File(Profile_Dir, "/"+getName()+".png");
        try {
            FileOutputStream out=new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            //Save above
            //Here's a broadcast to inform the system of album updates
            Uri uri=Uri.fromFile(file);
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
        } catch (FileNotFoundException e) {
            Log.e("TAG", "file_not found !");
        } catch (IOException e) {
            Log.e("TAG", "profile save fail!");


It's true that this has been going on for a long time and you don't understand anything. Just copy the code. Then the pain is that there is too much information on the Internet, and some of it is no longer suitable for the current version.

I'm using Vivo s10, are there any partners working on it too?

android development really has too much inside, so I can only walk slowly.

I hope this project will achieve a good result.

Yes, I'll send out the screenshots:




Finish, it's late.



Keywords: Java Android Android Studio UI

Added by ggkfc on Sun, 06 Mar 2022 20:22:30 +0200