How do I save an image to a newly created folder by my code?

Asked

Viewed 1,887 times

1

I am having problems at the time of storing my photo, I create a folder using mkdir and through a string, I take the contents of an Edittext from my application, to give the name of the folder. After initializing the camera I wanted to save the photo in the same folder created, so I use the same string to save it:

        @Override
        public void onClick(View arg0){

        nome = nomeNovo.getText().toString();      

        File folder = new File("sdcard/" + nome);

        if (!folder.exists()){                
            pasta = folder.mkdir();            
        }             

        if (pasta == false){                
            linear.addView(teste);                
            }
        else {
            Intent intent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);
            startActivity(intent);
        //    Toast.makeText(getApplicationContext(), "Endereço: sdcard/" + nome , Toast.LENGTH_SHORT).show();
            File arquivo = new File("sdcard/" + nome);
        }

The problem is that the photo is still being stored in the camera folder and not in the folder I created.

  • 1

    Okay, but what’s the problem?

  • The photo is being saved in the camera folder, not in the folder I created.

  • The code to do this is a little more complicated. Take a look here and in the official example. But basically the problem is that you are not passing to the application that will respond to your Intent the information about the file in which the photo is to be saved.

2 answers

0


Follow an example to open the camera and save with the name you want.

File file = new File(Environment.getExternalStorageDirectory() + "/arquivo.jpg");
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

After you take the picture he will already save her in the way you pass on new File()

  • I am trying to use this code but the photo is not being saved anywhere. Any idea ?

  • @Lucasmoraesdesouza, in which case the photo is saved in the "external" folder of the device (which is not necessarily a removable media), this location can change from APP to APP, here it usually stays on Armazenamento interno->Android->data->Sua pasta to know if she is being saved or not, make a view entry and pass that same one : intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(outputFileUri ), "image/*");
startActivity(intent);

  • @Lucasmoraesdesouza, in this my example, change the passage /arquivo.jpg for the path you are using, as @Lucasqueirozribeiro said, this path is from the root of the mobile phone (without considering root access), is where are the photo folders, downloads, etc, to change to /minha_pasta/arquivo.jpg and see in the folder if you are creating the file.

0

The routine below creates a folder inside the app directory and saves the images inside:

public void savarImagensToInternalStorage(String fileName, byte[] imagem){
        File dir = new File(getFilesDir().getAbsolutePath() + "/Imagens", fileName);
        if (!dir.exists())
            dir.getParentFile().mkdirs();
        try{
            FileOutputStream fos = new FileOutputStream( dir );
            fos.flush();
            fos.write(imagem);
            fos.close();
        }catch (IOException e) {
            Log.w("InternalStorage", "Error writing", e);
        }
    }

Browser other questions tagged

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