简体   繁体   中英

Get the path of a file saved to the android SD card

In the question Take screensot and save android someone informed of how to save an image created in my app to the SD card. Now I'm trying to upload the picture, but first I need to find how to get the path. The entire code that I used is in the last question, I need to know how to get the path to the image that I saved. Any suggestions?

Check these lines:

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/MyDraw");    
myDir.mkdirs();
file = new File (myDir, name+".png");

file is the File object that points to the location where the Bitmap is saved.

When onActivityResult() gets called back in your application, the intent contains the uri of where the file was stored. Pull it out of there and save it

First of all, you need the name that the user entered to save the filename with. You can save the filename the user inputted by saving to a pref (or they just entered it, then you should still have it). Once you have the filename, you can simply get the file by:

String fileName = //get the file name
File f = new File(Environment.getExternalStorageDirectory(), "/MyDraw");
f = new File(f, fileName);

If you don't know the filename, you can loop through the file directory:

File dir = new File(Environment.getExternalStorageDirector(), "/MyDraw");
for (File f : dir.listFiles()) {
    //Do something with this file
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM