Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] java – How to delete internal storage file in android app using android studio?

Posted on November 14, 2022

Solution 1 :

You are missing the .getCanonicalFile() part in your coe.

To fix the issue, just write yourr code like this:

file.getCanonicalFile().delete();

And you will be able to delete the file.

Problem :

I am using internal storage for fetch all pdf file in my android app. so now I implement, when I click long in one pdf file then show a popup layout and this popup layout contains the delete button. so when I click the delete button then this file will be deleted which file I click long. I am already designing this popup. But the main problem is when I click delete button then no action occurs. Here is Mainactivity code.

public class MainActivity extends AppCompatActivity {

private boolean isDoubleClicked = false;

ListView lv_pdf;
public static ArrayList<File>filelist=new ArrayList<>();
PDFAdapter obj_adapter;
public static int REQUEST_PERMISSION=1;
boolean boolean_permission;
File dir;
private AdapterView.OnItemLongClickListener listener;

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

    lv_pdf=(ListView)findViewById(R.id.listView_pdf);

    dir=new File(Environment.getExternalStorageDirectory().toString());

    permission_fn();

    lv_pdf.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent intent=new Intent(getApplicationContext(),ViewPDFFiles.class);
            intent.putExtra("Position",i);
            startActivity(intent);
        }
    });

lv_pdf.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {

            Dialog dialog=new Dialog(MainActivity.this);

            dialog.setContentView(R.layout.arart_dialog);
            dialog.setCanceledOnTouchOutside(false);
            dialog.getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            dialog.getWindow().getAttributes().windowAnimations=android.R.style.Animation_Dialog;

            Button btnTryAgain=dialog.findViewById(R.id.btn_delete);
            btnTryAgain.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    File dir=getFilesDir();
                    File file=new File(dir,Environment.getExternalStorageDirectory().toString());
                    boolean deleted=file.delete();

                }
            });
            dialog.show();

            return true;
        }
    });
}





private void permission_fn() {

    if((ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED)){
        if((ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE))){

        }else{
            ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},REQUEST_PERMISSION);
        }
    }else{
        boolean_permission=true;
        getfile(dir);
        obj_adapter=new PDFAdapter(getApplicationContext(),filelist);
        lv_pdf.setAdapter(obj_adapter);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode,  String[] permissions,  int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);


    if(requestCode==REQUEST_PERMISSION){
        if(grantResults.length >0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
            boolean_permission=true;
            getfile(dir);
            obj_adapter=new PDFAdapter(getApplicationContext(),filelist);
            lv_pdf.setAdapter(obj_adapter);
        }else {
            Toast.makeText(this, "Please allow the permission.", Toast.LENGTH_SHORT).show();
        }
    }
}

public ArrayList<File>getfile(File dir){
    File listFile[]=dir.listFiles();
    if(listFile!=null && listFile.length>0){
        for(int i=0;i<listFile.length;i++){
            if(listFile[i].isDirectory()){
                getfile(listFile[i]);
            }else {
                boolean booleanpdf=false;
                if(listFile[i].getName().endsWith(".pdf")){
                    for (int j=0;j<filelist.size();j++){
                        if(filelist.get(j).getName().equals(listFile[i].getName())){
                            booleanpdf=true;
                        }else {

                        }
                    }
                    if(booleanpdf){
                        booleanpdf=false;
                    }else {
                        filelist.add(listFile[i]);
                    }
                }
            }
        }
    }

    return filelist;
}

@SuppressLint("NewApi")
@Override
public void onBackPressed() {


        if(isDoubleClicked){
            super.onBackPressed();
            finishAffinity();
        }

        this.isDoubleClicked = true;
        Toast.makeText(this, "Press back again to exit.", Toast.LENGTH_SHORT).show();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                isDoubleClicked = false;
            }
        }, 2000);
    }
}

Comments

Comment posted by blackapps

so when I click the delete button then this file will be deleted which file I click long.

Comment posted by blackapps

Start with using a Toast in onClick that displays

READ  [ANSWERED] android - How to Retrieve Image/ Screenshot from Application Document Directory Flutter?
Powered by Inline Related Posts

Comment posted by blackapps

Please remove

Comment posted by mahedi hassan

i use this code:

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress