简体   繁体   中英

I want to develop my android code to reduce the image file size capture from camera before save to external storage

I developed a simple camera app for Android. Now I want modify this app to reduced the size of the image capture by Camera and save image to external storage. Herewith I add my MainActivity.java script. How can I modify this code to reduce size of the image and save to external storage?

public class MainActivity extends AppCompatActivity {

private static final int PERMISSION_CODE = 100;
private static final int IMAGE_CAPTURE_CODE = 101;
Button btnCapture;
ImageView imageCapture;

Uri image_uri;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnCapture=findViewById(R.id.btnCapture);
    imageCapture=findViewById(R.id.imgView);

    btnCapture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
                if(checkSelfPermission (Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED
                        || checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED){
                    String[] permission={CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};

                    requestPermissions(permission, PERMISSION_CODE);
                }
                else {
                    openCamera();
                }
            }
            else {
                openCamera();
            }
        }
    });
}
private void openCamera() {
    ContentValues values=new ContentValues();
    image_uri=getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);

    Intent camIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    camIntent.putExtra(MediaStore.EXTRA_OUTPUT,image_uri);
    startActivityForResult(camIntent, IMAGE_CAPTURE_CODE);

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    switch (requestCode){
        case PERMISSION_CODE:{
            if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                openCamera();
            }
            else {
                Toast.makeText(this,"Permission Denied",Toast.LENGTH_SHORT).show();
            }
        }
    }
}
} 

first add the permission in your Manifest then you can write on sd card :

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

and so you need to get your Bitmap .you can try to get it from your ImageView imageCapture such as:

BitmapDrawable drawable = (BitmapDrawable) mImageView1.getDrawable();
Bitmap bitmap = drawable.getBitmap();

you have to get to directory from SD Card

 File sdCardDir = Environment.getExternalStorageDirectory();
  
    // Next, create your specific file for image storage:
    File image = new File(sdCardDir , "myTest.png");
    //After that, you just have to write the Bitmap :
    boolean success = false;

    // Encode the file as a PNG image.
    FileOutputStream outStream;
    try {

        outStream = new FileOutputStream(image);

        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
        // 100 to keep full quality of the image 
        // You can reduce the size of the image if you decrease the value 

        outStream.flush();
        outStream.close();
        success = true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (success) {
        Toast.makeText(getApplicationContext(), "Image saved with success",
                Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(getApplicationContext(),
                "Error during image saving", Toast.LENGTH_LONG).show();
    }

You cannot change the behaviour of the used camera intent and the used camera app.

Always the full image will be saved to the uri you provide.

If you wanna change the file you can only do it afterwards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM