How to save a photo by creating a file with another name instead of replacing the previous file?

Asked

Viewed 23 times

-1

Hello, good afternoon! I’m having trouble saving the photo, because when you save it replaces, instead of creating another with another name:

package com.pesquisa.pesquisacenso.view;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;
import android.os.Environment;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;


import com.pesquisa.pesquisacenso.R;
import com.pesquisa.pesquisacenso.data.DataPesquisaCenso;
import com.pesquisa.pesquisacenso.model.Banco;
import com.pesquisa.pesquisacenso.model.PesquisaCenso;

public class TelaDados extends AppCompatActivity {
    RadioButton rbcavalete, rbpadrao, rbinterno, rbexterno;
    private EditText edtEmail,edtNumero, edtBairro, edtncadastrosaae, edtHidrometro, edtCep, edtLougradorou, edtRua, edtCidade, edtContato,edtTelefone, edtCpf, edtNome;
    static final int REQUEST_IMAGE_CAPTURE = 1;
    private Button btnSalvar,btnAbrir;
    private Context context;
    private DataPesquisaCenso db;

    Spinner spinnerUF;
    String currentPhotoPath;
    String[] ocupacao = {"Comercial", "Industrial", "Residencial", "Lavoura", "Publico", "Passivo Ambiental", "Outros"};
    ArrayAdapter<String> arrayOcupacao;
    Banco banco;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tela_dados);
        // Obtém uma instância da câmera


        arrayOcupacao = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ocupacao);
        spinnerUF = (Spinner) findViewById(R.id.uf);
        spinnerUF.setAdapter(arrayOcupacao);
        setContentView(R.layout.activity_tela_dados);

        spinnerUF = (Spinner) findViewById(R.id.uf);
        context = this;
        edtNumero = findViewById(R.id.edtNumero);
        edtBairro = findViewById(R.id.edtBairro);
        rbcavalete = findViewById(R.id.rbCavalete);
        rbpadrao = findViewById(R.id.rbPadrao);
        rbinterno = findViewById(R.id.rbInterna);
        rbexterno = findViewById(R.id.rbExterna);
        edtCep = findViewById(R.id.edtCep);
        edtLougradorou = findViewById(R.id.edtLougradorou);
        edtRua = findViewById(R.id.edtRua);
        edtContato = findViewById(R.id.edtContato);
        edtEmail = findViewById(R.id.edtEmail);
        edtTelefone = findViewById(R.id.edtTelefone);
        edtCpf = findViewById(R.id.edtCpf);
        edtNome = findViewById(R.id.edtNome);
        edtCidade = findViewById(R.id.edtCidade);
        btnSalvar = findViewById(R.id.btnSalvar);
        btnAbrir = findViewById(R.id.btnAbrir);

        btnAbrir.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String TimeStamp = new SimpleDateFormat("yyyyMMDdd_HHmmss").format(new Date());
                String ImageFile = "JPEG_" + TimeStamp + "_";
                File StorageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                File picsDir = Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_PICTURES);
                File imageFile = new File(picsDir, ".jpg");
                Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
                startActivity(i);

            }
        });

        btnSalvar.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), "Posição: " + spinnerUF.getSelectedItemPosition() +
                        "\n" + spinnerUF.getSelectedItem(), Toast.LENGTH_SHORT).show();
                banco = new Banco(context);
                try {

                    PesquisaCenso notas = new PesquisaCenso();
                    notas.setNome(edtNumero.getText().toString());
                    notas.setBairro(edtBairro.getText().toString());
                    notas.setcavalete(rbcavalete.getText().toString());
                    notas.setpadrao(rbpadrao.getText().toString());
                    notas.setexterno(edtncadastrosaae.getText().toString());
                    notas.setinterno(edtncadastrosaae.getText().toString());
                    notas.setCep(edtCep.getText().toString());
                    notas.setBairro(edtLougradorou.getText().toString());
                    notas.setCidade(edtCidade.getText().toString());
                    notas.setContato(edtContato.getText().toString());
                    notas.setrua(edtRua.getText().toString());
                    notas.setEmail(edtEmail.getText().toString());
                    notas.setTelefone(edtTelefone.getText().toString());
                    notas.setCpf(edtCpf.getText().toString());
                    notas.setNome(edtNome.getText().toString());

                    db.InserirDados(notas);

                } catch (Exception er) {
                    er.printStackTrace();
                }
            }
        });

        }
}

1 answer

0

The problem is in on-click Istener button btnAbrir:

// TimeStamp: 20210728_165608
String TimeStamp = new SimpleDateFormat("yyyyMMDdd_HHmmss").format(new Date());

// ImageFile: JPEG_20210728_165200_
String ImageFile = "JPEG_" + TimeStamp + "_";

// picsDir: File('/Pictures')
File picsDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

// imageFile: File('/Pictures/.jpg')
File imageFile = new File(picsDir, ".jpg");

That is, the value of ImageFile is being ignored. To fix this, just add it when creating the file:

// TimeStamp: 20210728_165200
String TimeStamp = new SimpleDateFormat("yyyyMMDdd_HHmmss").format(new Date());

// ImageFile: JPEG_20210728_165200.jpg
String ImageFile = "JPEG_" + TimeStamp + ".jpg";

// picsDir: File('/Pictures')
File picsDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

// imageFile: File('/Pictures/JPEG_20210728_165200.jpg')
File imageFile = new File(picsDir, ImageFile);

Browser other questions tagged

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