Android: Camera and local storage

Android to take pictures and to achieve local storage, and can be viewed through the ListView.

use tools

  • android studio (ver. 3.5.1)
  • android(sdk 29)
  • java (ver.1.8.0)
  • gradle(ver. 5.4.1)

Function to achieve

As the picture using a network picture, so to add a camera permissions and file permissions in AndroidManifest.xml read:

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Permissions flash back should check application settings when debugging.

pc1

Design a ListView to view photos taken locally.

Pic2

Monitor camera button click:

        @Override
    public void onClick(View v) {
        Intent intent=new Intent();
        switch (v.getId())
        {
            case R.id.btn_photo:
               DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                name=path+ df.format(new Date(System.currentTimeMillis())) +".jpg";
                imagefile=new File(name);
                try {
                    if(imagefile.exists())
                    {
                        imagefile.delete();
                    }
                    imagefile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                imageUri= FileProvider.getUriForFile(this, "com.cqjtu.fileapplication.PhotoActivity.fileprovider",imagefile);
                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.addCategory("android.intent.category.DEFAULT");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(intent, CAMER);
                break;
        }
    }

Photos show the local compression in the ListView:

private Bitmap readImage(String content)
    {
        Bitmap pic=null;
        String filepath=path+content;
        pic=zoomBitmap2(filepath,250,250);
        return pic;
    }

Show

pic3pic4

Download Code

FileApplication

Guess you like

Origin www.cnblogs.com/esllovesn/p/12208995.html