Storage Access Framework

Storage Access Framework (SAF) was introduced with the release of Android 4.4 (API level 19). However the new selector UI provides the user an easy way to access files stored on their device and in the cloud , it doesn’t necessarily give developers the same ease of use.

I am writing this tutorial by digging up the API Guide. I will be writing a step by step coding guide for you so that you can try it out by your own with all your comfort.

The code is basic , so if you’ve worked with Android’s flavor of Java project before you shouldn’t have any problem understanding what’s going on.

Step1: Create a new android project in your android IDE. 

Step2: Add the following permissions to your manifest file:

 

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

 

 

Step3: Write the following into your layout file:

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<ImageView
android:id="@+id/imageViewSelectedImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/saf" />

<Button
android:id="@+id/go_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageViewSelectedImage"
android:layout_centerHorizontal="true"
android:text="SAF" />

</RelativeLayout>

 

 

Step4:  Write the following into main java file:

 

package com.example.safexample;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.media.Image;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button = (Button) findViewById(R.id.go_button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {

performFileSearch();

}
});
}

private static final int READ_REQUEST_CODE = 42;

public void performFileSearch() {

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");

startActivityForResult(intent, READ_REQUEST_CODE);
}

@Override
public void onActivityResult(int requestCode, int resultCode,
Intent resultData) {

if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {

Uri uri = null; // Instantiate a Uri object to hold the image's Uri.

if (resultData != null) {

uri = resultData.getData();

try{
Bitmap the_image = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
int nh = (int) ( the_image.getHeight() * (512.0 / the_image.getWidth()) );
Bitmap scaled = Bitmap.createScaledBitmap(the_image, 512, nh, true);
ImageView thumbnail = (ImageView) findViewById(R.id.imageViewSelectedImage);
thumbnail.setImageBitmap(scaled);

String output_path = Environment.getExternalStorageDirectory()
+ "/MyNewFolder/gdrive_image.jpg";

File oFile = new File(output_path);

if (!oFile.exists()) {
oFile.getParentFile().mkdirs();
oFile.createNewFile();
}

InputStream iStream = this.getContentResolver().openInputStream(uri);

writeFile(inputData,output_path);

} catch (Exception e){
e.printStackTrace();
}
}

}

}

public byte[] getBytes(InputStream inputStream) throws IOException {

ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];

int len = 0;

while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}

return byteBuffer.toByteArray();
}

public void writeFile(byte[] data, String fileName) throws IOException{
FileOutputStream out = new FileOutputStream(fileName);
out.write(data);
out.close();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

}

 

 

Step5: Run for the output like below:

  saf2

saf3

Leave a Comment: