Adam Varhegyi
Adam Varhegyi

Reputation: 9894

How to check a proper file in android phone's memory?

I need a method that downloads a picture and save it in SD card for later use. I got it finally, i just need an operation that checks if the file in already exists or not. If not, the app will download it. If it is exitsts, my image view will use it.

So my question is just simple:

How to check a file in SD?

If i got this place for example:

String imageInSD = "/sdcard/1.png";

Upvotes: 0

Views: 146

Answers (1)

Desert Ice
Desert Ice

Reputation: 4600

You can use the following code

String sdcardState = android.os.Environment.getExternalStorageState();
String fileName = "yourfilename" + ".png";
if (sdcardState.contentEquals(android.os.Environment.MEDIA_MOUNTED))
  {
     String destPath = android.os.Environment.getExternalStorageDirectory().toString()+ File.separator +fileName;
     File output =new File(destPath);

     if(output.exists())
       \\it exists-use Imageview
      else
       \\Download the file accordingly
  }
else
  \\SDCARD not mounted

Upvotes: 1

Related Questions