V
V
VitGun2019-08-07 20:08:12
Java
VitGun, 2019-08-07 20:08:12

Android. Full size camera image and file name prompt. How to get the?

The task was to make an application for android that takes a photo, asks for a file name and saves it all to the gallery. In agony gave birth to this code

public class MainActivity extends AppCompatActivity {
    private static final int REQUEST_ID_READ_WRITE_PERMISSION = 99;
    private static final int REQUEST_ID_IMAGE_CAPTURE = 100;
    File file = null;
    String m_Text = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Button btnStart;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnStart = (Button)findViewById(R.id.btnStart);
        btnStart.setOnClickListener(oclStart);

    }

    View.OnClickListener oclStart = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Capture();
        }
    };

    void Capture()
    {
        if (Build.VERSION.SDK_INT>=23){

            int readPermission = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
            int writePermission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
            if (writePermission != PackageManager.PERMISSION_GRANTED || readPermission != PackageManager.PERMISSION_GRANTED){
                this.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_ID_READ_WRITE_PERMISSION);
            }

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent,1);

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        final String filename = "";

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final EditText input = new EditText(this);

        input.setInputType(InputType.TYPE_CLASS_TEXT);
        builder.setView(input);

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                m_Text = input.getText().toString();
                String file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)+"/CAMERA/"+m_Text+".jpg";
                Bitmap bp = (Bitmap) data.getExtras().get("data");
                try {
                    FileOutputStream out = new FileOutputStream(file);
                    bp.compress(Bitmap.CompressFormat.JPEG,100, out);
                } catch (FileNotFoundException ex){

                }
            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.cancel();
            }
        });
        builder.show();
    }

}

It works and works as it should. One problem - he saves the preview, ie. extremely low resolution image. Googling, I realized that in order to pull out the full image, you need to pass intent.putExtra(MediaStore.EXTRA_OUTPUT,filename) before calling startActivityForResult . I changed the code so that the file name is requested BEFORE calling intent, but for some reason it does not wait for a response from the user (entering the file name), but tries to open the camera and crashes as expected.
I am not an android programmer. And for the first time I write something meaningful in java. Can anyone give me a working example of how to implement this?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question