Rename Imageview to Upload

Asked

Viewed 56 times

0

Good morning,

I would like to ask a question, my APP so far the guy takes a photo of the phone and is in an Imageview (Ex: img1), I need to be created a name defined by me in an example variable: 04102018_Foto1.jpg, would have some form so that I can send to the BD 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);


}
}

1 answer

1


First of all, you’re just getting the image Thumb, which I think in your case wouldn’t be the best case.

In order to save the real image of the camera, it is necessary 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;
}

I’ll 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 take the File returned on createImageFile() and copy it to the desired path, and to the desired name (after saving) using the method

FileUtils.copyFile(File origem, File dest);

  • Thank you so much for your help, I’ll be testing and I’ll be all right !

  • I have a question, if I want to pass it along with the other Strings to send via Json as I would ex: String contract = txtContract.gettext(). toString(); String data = txtHorario.gettext(). toString(); String remarks = txtObservation.gettext(). toString();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.