Rename ImageView to Upload

0

Good morning,

I would like to ask a question, my APP so far the guy takes a picture of the phone and stays in an ImageView (Ex: img1), I need to create a name defined by me in a variable example: 04102018_Foto1.jpg, would have some way so I can send the DB with that name defined by me?

Code:

  String dataAtualFormatada = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(System.currentTimeMillis());


    btnTirarFoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent((MediaStore.ACTION_IMAGE_CAPTURE));
            startActivityForResult(intent, 0);
            //  Toast.makeText(TelaAtestado.this, "Atestado cadastrado com sucesso! ", Toast.LENGTH_LONG).show();

        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Bitmap bitmap = (Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    // Comprimir Imagem = PNG/JPG ----- QUALITY:
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
    img1.setImageBitmap(bitmap);


}
}
    
asked by anonymous 04.10.2018 / 16:56

1 answer

1

First of all, you're just getting the thumb of the image, which I think in your case would not be the best case.

In order to save the actual camera image, you need to create a file and send the address to it.

(Method to create the temporary file)

String mCurrentPhotoPath;

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
        imageFileName,  /* prefix */
        ".jpg",         /* suffix */ 
        storageDir      /* directory */
    );

    // Guarda o endereço da imagem (para utilizar no imageview, por exemplo)
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

Method to call the camera

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Verifica se existe uma camera para abrir
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Cria o arquivo para salvar a imagem
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Erro criando o arquivo
        }
        // Caso o arquivo seja criado, é chamada a camera
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                                                  "com.example.android.fileprovider",
                                                  photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

With this, we can get the file returned in createImageFile() and copy it to the desired path, and with the desired name (after saving) using the

FileUtils.copyFile(File origem, File dest);

    
04.10.2018 / 19:01