Rename an image before or after saving it

1

I'm doing an app in android studio, and I need to change the name of an image before saving it. I detail more, first I upload an image from the memory of my mobile, the change by default and then save it, but save it with the name that had that image. I would need to change that name of that new image before or after I saved it by calling it the same as the one I had, but in a different folder.

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.carpeta_acciones);

        imagen = (ImageView)findViewById(R.id.abrazar);
        imagen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openGallery();
            }
        });
    }

    private void openGallery() {
        Intent gallery = new Intent(Intent.ACTION_GET_CONTENT);
        gallery.setType("image/*");
        if (gallery.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(gallery, REQUEST_IMAGE_GET);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
            if (resultCode == Activity.RESULT_OK && data != null) {
                imageUri = data.getData();
                dir = new File(getFilesDir(), nombreCarpeta);

            if (!dir.exists()) {
                dir.mkdirs();
            }

            //file = new File(dir, imagen + ".png");
            imagen.setImageURI(imageUri);
            imagen.buildDrawingCache();

            imgUri = imageUri.toString();
            //imagen.buildDrawingCache();
           // bmp = imagen.getDrawingCache();
            cr= this.getContentResolver();
            try {
               //bmap =  MediaStore.Images.Media.getContentUri(imgUri);
                bmap=android.provider.MediaStore.Images.Media.getBitmap(cr,imageUri);

            }catch(IOException ex){
                ex.printStackTrace();
            }
               /* Bitmap imag = ((BitmapDrawable)imagen.getDrawable()).getBitmap();
                String ruta = guardarImagen(getApplicationContext(), "abrazar", imag);
                Toast.makeText(getApplicationContext(), ruta, Toast.LENGTH_LONG).show();*/

            guardarImagen(contexto,"abrazar",bmap);
        }

    }


    private String guardarImagen (Context context,String nombre, Bitmap bmap){

        file = new File(dir,bmap + ".png");
        FileOutputStream fos = null;
        ImageView imageView;
        Bitmap bmp;

        try{

            fos = new FileOutputStream(file);
            bmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
        }catch (FileNotFoundException ex){
            ex.printStackTrace();
            Toast.makeText(contexto, "¡No se ha podido guardar la imagen!", Toast.LENGTH_SHORT).show();
        }catch (IOException ex){
            ex.printStackTrace();
            Toast.makeText(contexto, "¡No se ha podido guardar la imagen!", Toast.LENGTH_SHORT).show();
        }
        return file.getAbsolutePath();
    }

If you could help me, I'd appreciate it, I'm stuck. Thanks in advance.

    
asked by Belen 24.04.2018 в 19:22
source

1 answer

1

You must assign the name here, where you define the path and the name of the file:

private String guardarImagen (Context context,String nombre, Bitmap bmap){

    //file = new File(dir,bmap + ".png"); //*Incorrecto.
      file = new File(dir,nombre + ".png"); //*Correcto.
...
...

You can check this out at documentation for File ()

    
answered by 24.04.2018 в 19:57