Changes

Jump to: navigation, search

An Android Storage Access Framework Example

2,045 bytes removed, 15:45, 17 January 2014
no edit summary
}
</pre>
 
== Creating a New Storage File ==
 
When the New button is selected, the application will need to trigger an ACTION_CREATE_DOCUMENT intent configured to create a file with a plain text MIME type. When the user interface was designed, the New button was configured to call a method named newFile(). It is within this method that the appropriate intent needs to be launched.
Remaining in the StorageDemoActivity.java file, implement this method as follows:
 
<pre>
package com.example.storagedemo;
 
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
 
public class StorageDemoActivity extends Activity {
 
private static final int CREATE_REQUEST_CODE = 40;
private static final int OPEN_REQUEST_CODE = 41;
private static final int SAVE_REQUEST_CODE = 42;
.
.
public void newFile(View view)
{
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
 
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TITLE, "newfile.txt");
startActivityForResult(intent, CREATE_REQUEST_CODE);
}
.
.
}
</pre>
 
This code creates a new ACTION_CREATE_INTENT Intent object. This intent is then configured so that only files that can be opened with a file descriptor are returned (via the Intent.CATEGORY_OPENABLE category setting).
 
Next the code specifies that the file to be opened is to have a plain text MIME type and a placeholder filename is provided (which can be changed by the user in the picker interface). Finally, the intent is started, passing through the previously declared CREATE_REQUEST_CODE.
 
When this method is executed and the intent has completed the assigned task, a call will be made to the application’s onActivityResult() method and passed, amongst other arguments, the Uri of the newly created document and the request code that was used when the intent was started. Now is an ideal opportunity to begin to implement this method.
== Creating a New Storage File ==
[[Image:android_storage_access_framework_pickerandroid_storage_access_framework_picker2.png|The Android Storage Access Framework picker]]
Figure 43-2

Navigation menu