Android Storage Use Cases; How to Read/ Write on Internal Storage

Android Knowledge Sharing…

Geno Tech
App Dev Community
4 min readSep 7, 2021

--

Photo by Jonathan Kemper on Unsplash

This article will show you how to implement a small android project that utilizes device storage. We are using these functions for different applications with different purposes. At that time this article will help you so much. These functions also work for android’s latest version(Android 11 (API level 30)). In your applications, sometimes you need to download files into the user’s storage, such as images, PDF, etc. After that, you need to read them. In this approach, you can download them into a hidden folder, But hidden files show in the download category in some versions. That will be a problem in some cases. However, I hope this guide will help you to solve those kinds of issues.

Content of the Tutorial

  1. Download a file into a hidden folder.
  2. Show downloaded files by reading the storage.

Download an image into a hidden folder

First, you want to create an android(Java) application. Then you should complete the main layout by adding two buttons for download and show and ImageView.

Then you need to ask permission to use the storage of the device and for the internet connection. Add the following four permissions in AndroidManifest.xml.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

In the beginning, you should ask for storage permissions as follows.

if (SDK_INT >= Build.VERSION_CODES.R) {
Log.d("myz", ""+SDK_INT);
if (!Environment.isExternalStorageManager()) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.MANAGE_EXTERNAL_STORAGE}, 1);//permission request code is just an int
}
}else {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
}
Image: Asking for reading/write permissions.

Now you are ready to implement the code o download an image. Here I am just showing for image download. But you can use any file type for your purpose.

When you click on the download button, the following code will execute. First, we need to check if the file is existing.

if (fileExist("sample_image2")) {
Toast.makeText(getApplicationContext(),"This Image is Already Available in your storage", Toast.LENGTH_SHORT).show();
} else {
downloadManager();
}public boolean fileExist(String fname){
File file = new File(getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
+ "/.myfolder", fname+".jpg");
return file.exists();
}

If the file has not existed, should download the file. Here I call the method downloadManager(). You can use a dot before the file name when naming the folder, then that folder will not show in the storage. But in some versions, the file is showing under the downloaded files category.

public void downloadManager() {
File path = getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
String folder_main = ".myfolder";
File f = new File(path, folder_main);
if (!f.exists()) {
f.mkdirs();
Log.e("myz", "Directory make");
}else{
Log.e("myz", "Directory already have");
}
File file = new File(f, "sample_image2.jpg");
try {
Log.e("myz", "Directory make");
DownloadManager.Request request = new DownloadManager.Request(Uri.parse("https://www.tompetty.com/sites/g/files/g2000007521/f/Sample-image10-highres.jpg"))
.setTitle("sample_image")// Title of the Download Notification
.setDescription("Downloading")// Description of the Download Notification
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)// Visibility of the download Notification
.setDestinationUri(Uri.fromFile(file))
.setAllowedOverMetered(true)// Set if download is allowed on Mobile network
.setAllowedOverRoaming(true);//
DownloadManager downloadManager = (DownloadManager) getApplicationContext().getSystemService(DOWNLOAD_SERVICE);
long downloadID = Objects.requireNonNull(downloadManager).enqueue(request);
} catch (IllegalArgumentException e) {
Log.e("myz", "downnload error "+e);
}
}

Then you can see the file is downloading and saving successfully.

Image: Downloading the file

After you downloaded the file, the file has in the specific folder.

Image: The file is existing in the folder.

Show downloaded files by reading the storage.

IN our second part, we have to show the downloaded file. Actually here the file rendering method is depending on the file type. Here I downloaded a .jpg image. Therefore I used the Picasso library to load the image into image view.

public void show(View view){
File file = new File(getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)+ "/.myfolder", "sample_image2.jpg");
if(file.exists())
{
ImageView imageView = findViewById(R.id.imageView);
Picasso.get().load(file.getAbsolutePath()).into(imageView);
}
}
Image: The file is reading from the storage.

This story gives you knowledge about Android/Java storage use cases that you have to implement in your applications. Please feel free to ask any question you will face in the response section below.
Happy Coding !!!!
Found this post useful? Kindly tap the 👏 button below! :)

--

--

Geno Tech
App Dev Community

Software Development | Data Science | AI — We write rich & meaningful content on development, technology, digital transformation & life lessons.