Search code examples
androidandroid-image-capture

How to configure ACTION_IMAGE_CAPTURE to store the photo in public external storage?


The Taking Photos Simply Documentation recommends to store image taken with device camera as "should be saved on the device in the public external storage". But how to do this? The provided example shows rather how to save to a private folder!?


Solution

  • see this code it will help you definitely:

    private void selectImage() {

        final CharSequence[] options = {"Take Photo", "Choose from Gallery"};
    
        AlertDialog.Builder builder = new AlertDialog.Builder(Maintenance.this);
        builder.setTitle("Add Photo!");
        builder.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
    
            @Override
            public void onClick(DialogInterface dialog, int which) {
    
                dialog.dismiss();
            }
        });
        builder.setItems(options, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                if (options[item].equals("Take Photo")) {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                    startActivityForResult(intent, 1);
                } else if (options[item].equals("Choose from Gallery")) {
                    Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(intent, 2);
    
                } /*else if (options[item].equals("Cancel")) {
                    dialog.dismiss();
                }*/
            }
        });
        builder.show();
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                File f = new File(Environment.getExternalStorageDirectory().toString());
    
                for (File temp : f.listFiles()) {
                    if (temp.getName().equals("temp.jpg")) {
                        f = temp;
                        break;
                    }
                }
                try {
                    Bitmap bitmap;
                    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    
                    bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
                            bitmapOptions);
                    bitmap = Bitmap.createScaledBitmap(bitmap, 450, 450, false);
                    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                    // byte[] imageBytes = bytes.toByteArray();
                    // encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    
                    Camera.setImageBitmap(bitmap);
    
                    String path = android.os.Environment
                            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath();
                    f.delete();
                    OutputStream outFile = null;
                    file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
                    //file.getPath();
                    // String str = FileUtils.readFileToString(file, "UTF-8");
                    image = file.getPath();
                    String[] parts = image.split("/");
                    encodedImage = parts[parts.length - 1];
                    try {
                        outFile = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
                        outFile.flush();
                        outFile.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (requestCode == 2) {
    
                Uri selectedImage = data.getData();
                String[] filePath = {MediaStore.Images.Media.DATA};
                Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
                c.moveToFirst();
                int columnIndex = c.getColumnIndex(filePath[0]);
                picturePath = c.getString(columnIndex);
                c.close();
                Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
                thumbnail = Bitmap.createScaledBitmap(thumbnail, 450, 450, false);
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                thumbnail.compress(Bitmap.CompressFormat.JPEG, 85, bytes);
                //  byte[] imageBytes = bytes.toByteArray();
                // encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
                image = picturePath;
                String[] parts = picturePath.split("/");
                encodedImage = parts[parts.length - 1];
                Camera.setImageBitmap(thumbnail);
            }
        }
    }