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: https://blog.csdn.net/u011418943/article/details/77712662

1. Choose how to get photos ()

case R.id.camera:
                             
                Log.e("TAG","open camera to get a pic");
                from_camera(FROM_CAMERA);
                Log.e("TAG", "start camera");
                break;
case R.id.photo:
                Toast.makeText(MainActivity2.this, "choose a pic from photo", Toast.LENGTH_SHORT).show();
                from_photo(FROM_PHOTO);
                break;

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
        if(!Profile_Dir.exists()){
            Profile_Dir.mkdir();
        }
        //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.  
                    mTmpFile);
        }else{
            uri = Uri.fromFile(mTmpFile);
        }
        Log.e("TAG", String.valueOf(uri));
        //Turn on the camera
        intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
        startActivityForResult(intent,fromCamera);
        //Handle

    }

Below is the corresponding configuration instructions above:

First, register the provider(FileProvider) in Manifest:

<provider
            android:name="androidx.core.content.FileProvider"
<--!  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-->
            android:authorities="Advance"
            <--!this authorites That's what I said above. I'm taking my own package name here. Remember to be the same-->
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"/>
                
        </provider>
<--!  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="http://schemas.android.com/apk/res/android">
    <external-path name="camera" path="APP Head portrait"/>
</paths>

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()

https://www.cnblogs.com/kezhuang/p/8706988.html 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.
                cropPhoto(Uri.fromFile(pic));





public void cropPhoto(Uri uri) {

        Intent intent = new Intent("com.android.camera.action.CROP");
        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
                user_profile.setImageBitmap(head);
                //This is to save the avatar, and I also saved it in the SD card instead of the photo I added up to take
                Bit2File(head);
            }



 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);
            out.flush();
            out.close();
            //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) {
            e.printStackTrace();
            Log.e("TAG", "file_not found !");
        } catch (IOException e) {
            Log.e("TAG", "profile save fail!");
            e.printStackTrace();
        }

    }

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