Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create subdirectories in android internal storage for images

I am writing an application to store the user profile information and contact information. User profile and contact information also have an image/bitmap. I would like to store images in internal storage probably and display the image using Picasso library. I want to application to create a profile directory to store the profile image and similarly for contacts. I want to use Picasso to retrieve the image from file and display it on the ImvageView as shown below.

Picasso.with(context)
       .placeholder(R.drawable.user_placeholder)
       .error(R.drawable.user_placeholder_error)
       .load("file:///somepath/profile.png")
       .into(imageView);

I am not sure how the application will create sub-directories and store the images. What would be the path for images that can be provided to the Picasso to load the image in Imageview?

Edit1

I think, I can use getApplicationContext().getFilesDir().getAbsolutePath() to get the path, but I am still not sure how to create the subdirectories for profile and contacts?

like image 555
Kumari Sweta Avatar asked Oct 18 '25 13:10

Kumari Sweta


1 Answers

In Android there is a directory in the internal storage where your app can store any kind of file; you can use Context.getFilesDir() to retrieve it.

You can create subdirectories too, using File.mkdir(). I don't know if Picasso takes care of that, but to be sure you can create the directory beforehand. The code to determine the path look like this:

File makeAndGetProfileDirectory(String profileName) {
    // determine the profile directory
    File profileDirectory = new File(context.getFilesDir(), profileName);

    // creates the directory if not present yet
    profileDirectory.mkdir();

    return profileDirectory;
}

Now you can use this method to obtain a directory where to put the profile data, including the picture. Let's say every profile will have a file named picture.jpg; the code for Picasso would look like this:

String profileName = "foo"; // replace with the profile you want to show
File profileDir = makeAndGetProfileDirectory(profileName);
File profilePictureFile = new File(profileDir, "picture.jpg");

// now use Picasso to read it
Picasso.with(context)
       .placeholder(R.drawable.user_placeholder)
       .error(R.drawable.user_placeholder_error)
       .load(profilePictureFile)
       .into(imageView);

Hope this helps.

like image 77
ris8_allo_zen0 Avatar answered Oct 22 '25 06:10

ris8_allo_zen0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!