receive the size of a folder in textview

Asked

Viewed 69 times

1

I’m an Android development intern, as a challenge I need to create an App to receive/discover the size of the folder/ directory of images Whatsapp...

I researched and I couldn’t find anything, I saw some technologies that could be used, I have no idea how to apply.

I am grateful from now on.

  • I think this link will help http://respostas.guj.com.br/13318-calcular-tamanho-de-umapasta-comjava

1 answer

0


Here’s an example of how to get information from a folder.

This is just one example!

Where we list the folder Download !

It’s up to you to find the folder you want:

Follow the example:

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;

public class MainActivity extends AppCompatActivity {

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


        /**
         * Botão que irá acionar o método para verificação da pasta
         */
        button = Button.class.cast(findViewById(R.id.button));
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                /**
                 * Verifica se há permissão par Ler o diretório externo
                 */
                if(isStoragePermissionGranted()){
                    /**
                     * Se possui permissão irá verificar...
                     */
                    verificarPasta();
                }
            }
        });
    }

    /**
     * Este metodo retorna informações da Pasta Download
     * Cabe a você, encontrar a pasta onde são armazenadas as informações do WhatsApp
     *
     */
    private void verificarPasta(){
        /**
         * Pegamos o diretório externo
         */
        File externalStorageDirectory = android.os.Environment.getExternalStorageDirectory();
        /**
         * Pegamos a pasta Download. (Aqui você deverá informar o caminho que deseja)
         */
        File downloadFolder = new File(externalStorageDirectory.getAbsolutePath()+"/Download");
        /**
         * Se existe o diretório
         */
        if(downloadFolder.exists()){
            /**
             * Toast com as informações da pasta
             */
            Toast.makeText(getApplicationContext(),  downloadFolder.getUsableSpace()+" bytes usados", Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(),  downloadFolder.getFreeSpace()+" bytes livres", Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(),  downloadFolder.getTotalSpace()+" bytes total ", Toast.LENGTH_SHORT).show();

            if(null != downloadFolder.listFiles() ){
                Toast.makeText(getApplicationContext(),  downloadFolder.listFiles().length+" arquivos ", Toast.LENGTH_SHORT).show();
            }

        }else{
            Toast.makeText(getApplicationContext(),  "Diretorio nao existe", Toast.LENGTH_SHORT).show();

        }
    }

    /**
     * Caso não haja permissão, será solicitado (em um pop-up), e volta a este método. 
     * Em caso positivo (se deu permissão) chama o método verificarPasta();
     * @param requestCode
     * @param permissions
     * @param grantResults
     */

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(null != permissions ){
            if(null != permissions[0]) {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    verificarPasta();
                }
            }
        }
    }

    /**
     * Verifica se a aplicação tem permissão para ler a pasta!
     * Se não tem permissão, pede através de ActivityCompat.requestPermissions
     * @return
     */
    public  boolean isStoragePermissionGranted() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                Log.v("PERMISSION","Permission is granted");
                return true;
            } else {
                Log.v("PERMISSION","Permission is revoked");
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
                return false;
            }
        }
        else { //permission is automatically granted on sdk<23 upon installation
            Log.v("PERMISSION","Permission is granted");
            return true;
        }
    }
}

Browser other questions tagged

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