Fixing the action provider demo

1. Fixed a bug the the way the URI was set to the
   intent.

2. Added code to copy a private raw resource to a
   publicly readable file that is to be shared.

Change-Id: Ib7ee172b2a4a890ce77911d0ef1c3ea3b24e69c9
This commit is contained in:
Svetoslav Ganov
2011-07-19 13:19:43 -07:00
parent 1213665dee
commit 575c0c3e5c

View File

@@ -17,6 +17,7 @@
package com.example.android.apis.app; package com.example.android.apis.app;
import android.app.Activity; import android.app.Activity;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
@@ -24,10 +25,13 @@ import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.ShareActionProvider; import android.widget.ShareActionProvider;
import java.io.File;
import com.example.android.apis.R; import com.example.android.apis.R;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/** /**
* This activity demonstrates how to use an {@link android.view.ActionProvider} * This activity demonstrates how to use an {@link android.view.ActionProvider}
* for adding functionality to the Action Bar. In particular this demo is adding * for adding functionality to the Action Bar. In particular this demo is adding
@@ -36,9 +40,12 @@ import com.example.android.apis.R;
*/ */
public class ActionBarActionProviderActivity extends Activity { public class ActionBarActionProviderActivity extends Activity {
private static final String SHARED_FILE_NAME = "shared.png";
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
copyPrivateRawResuorceToPubliclyAccessibleFile();
} }
@Override @Override
@@ -50,6 +57,8 @@ public class ActionBarActionProviderActivity extends Activity {
MenuItem actionItem = menu.findItem(R.id.menu_item_share_action_provider_action_bar); MenuItem actionItem = menu.findItem(R.id.menu_item_share_action_provider_action_bar);
ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider(); ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
actionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME); actionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
// Note that you can set/change the intent any time,
// say when the user has selected an image.
actionProvider.setShareIntent(createShareIntent()); actionProvider.setShareIntent(createShareIntent());
// Set file with share history to the provider and set the share intent. // Set file with share history to the provider and set the share intent.
@@ -58,6 +67,8 @@ public class ActionBarActionProviderActivity extends Activity {
(ShareActionProvider) overflowItem.getActionProvider(); (ShareActionProvider) overflowItem.getActionProvider();
overflowProvider.setShareHistoryFileName( overflowProvider.setShareHistoryFileName(
ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME); ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
// Note that you can set/change the intent any time,
// say when the user has selected an image.
overflowProvider.setShareIntent(createShareIntent()); overflowProvider.setShareIntent(createShareIntent());
return true; return true;
@@ -71,8 +82,44 @@ public class ActionBarActionProviderActivity extends Activity {
private Intent createShareIntent() { private Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND); Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*"); shareIntent.setType("image/*");
Uri uri = Uri.fromFile(new File(getFilesDir(), "SomeFileToShare")); Uri uri = Uri.fromFile(getFileStreamPath("shared.png"));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri.toString()); shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
return shareIntent; return shareIntent;
} }
/**
* Copies a private raw resource content to a publicly readable
* file such that the latter can be shared with other applications.
*/
private void copyPrivateRawResuorceToPubliclyAccessibleFile() {
InputStream inputStream = null;
FileOutputStream outputStream = null;
try {
inputStream = getResources().openRawResource(R.raw.robot);
outputStream = openFileOutput(SHARED_FILE_NAME,
Context.MODE_WORLD_READABLE | Context.MODE_APPEND);
byte[] buffer = new byte[1024];
int length = 0;
try {
while ((length = inputStream.read(buffer)) > 0){
outputStream.write(buffer, 0, length);
}
} catch (IOException ioe) {
/* ignore */
}
} catch (FileNotFoundException fnfe) {
/* ignore */
} finally {
try {
inputStream.close();
} catch (IOException ioe) {
/* ignore */
}
try {
outputStream.close();
} catch (IOException ioe) {
/* ignore */
}
}
}
} }