Android - Creating Intents: File Storage
Hello all,
I really enjoyed the Udacity Android Basics Course by Google lesson Practice with Intents so I am taking some time to fully explore each of the intents in the Android Common Intents documentation. The one I have most recently looked at is File Storage.
First I just created a simple button to activate the intent and an empty ImageView to populate with a retrieved file, which in this example is a photo. The app looks like this at first:
![Android App showing a single FILE STORAGE button.](https://static.wixstatic.com/media/b86045_0e42e45100b147fa94e339b08f9e827e~mv2.png/v1/fill/w_303,h_500,al_c,q_85,enc_auto/b86045_0e42e45100b147fa94e339b08f9e827e~mv2.png)
And the code in activity_main.xml looks like this:
<Button
android:id="@+id/fileStorage"
android:layout_width="@dimen/button_width"
android:layout_height="@dimen/button_height"
android:layout_gravity="center"
android:layout_margin="@dimen/margin"
android:onClick="fileStorage"
android:text="File Storage"/>
<ImageView
android:id="@+id/photoFromStorage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Then I created an activity in MainActivity.java to call the intent when the button is clicked. To launch a file app that focuses on photos all you need to do is include this code.
static final int REQUEST_IMAGE_GET = 3;
public void fileStorage(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_IMAGE_GET);
}
}
Notice that the line: static final int REQUEST_IMAGE_GET = 3; is added above the intent and that this variable is then used in this line startActivityForResult(intent, REQUEST_IMAGE_GET); at the end of the intent. This variable is important when doing startActivityForResult because with these intents something is expected to be returned to your app and it needs a unique id number. Because it can only be a number it is helpful to turn it into a static variable so you can give it a more meaningful name.
Also notice the line intent.setType("image/*");. This ensures that the only file types you can select for your app are photos. To choose other file types you would need to select a different keyword. The last two lines of this code just check to make sure that your phone has an app that can handle the task before running it to prevent your app from crashing if the phone doesn’t have the right app.
In order to access and use the file (in this example a photo) the user selected from file storage, you need to write another block of code, shown below with comments:
/**
* This method uses the image data sent from file storage and sends it to an ImageView
* @param requestCode checks that REQUEST_IMAGE_GET has been returned
* @param resultCode checks that the User did select some information
* (here a photo image file) to be returned
* @param data is the photo image file itself
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// This line checks that the user did select a file to send to the app
if (requestCode == REQUEST_IMAGE_GET && resultCode == RESULT_OK) {
// This line gets the photo image file and stores it in a variable
Uri fullPhotoUri = data.getData();
// This line take the file and puts it into an ImageView with an id
photoFromStorage photoFromStorage.setImageURI(fullPhotoUri);
}
You need to do two more things before the phone number can be seen on your app screen. Add a line of code to the onCreate method and add a new Global variable. This will make the top of your Java code look like this:
private ImageView photoFromStorage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
photoFromStorage = (ImageView) findViewById(R.id.photoFromStorage);
}
The key changes that have been made here are the additions of the lines photoFromStorage = (ImageView) findViewById(R.id.photoFromStorage); inside the onCreate method and this line private ImageView photoFromStorage; outside of the onCreate method. These two lines get the empty ImageView photoFromStorage from the the activity_main.xml file then turns it into a global variable so that the onActivityResult method can use it to put the photo in.
All of this should allow you to use your phone’s File Storage from within your app and show a photo image file from it on your app screen. The screen should look like this when choosing an image:
![An example of what the photo section of File Storage should look like.](https://static.wixstatic.com/media/b86045_7474fa43bd41454a83383595e96f5ba8~mv2.jpg/v1/fill/w_281,h_500,al_c,q_80,enc_auto/b86045_7474fa43bd41454a83383595e96f5ba8~mv2.jpg)
And at the end your screen should look something like this:
![Android App with the photo retrieved from File Storage visible in a ImageView under the FILE STORAGE button.](https://static.wixstatic.com/media/b86045_00ebc65abda64a3ca32a1d8a4e1a4d56~mv2.jpg/v1/fill/w_281,h_500,al_c,q_80,enc_auto/b86045_00ebc65abda64a3ca32a1d8a4e1a4d56~mv2.jpg)
This article is only really scratching the surface of what you can do with the File Storage Intent. You can retrieve and display other types of information from phone and internet files as well. You can also choose multiple files of different types and much more.
Please feel free to question, correct or further explain anything I have covered here. I am by no means an expert on this topic and would love anything that will help me learn more.
Links: