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:
@@ -17,6 +17,7 @@
|
||||
package com.example.android.apis.app;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
@@ -24,10 +25,13 @@ import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.widget.ShareActionProvider;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
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}
|
||||
* 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 {
|
||||
|
||||
private static final String SHARED_FILE_NAME = "shared.png";
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
copyPrivateRawResuorceToPubliclyAccessibleFile();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -50,6 +57,8 @@ public class ActionBarActionProviderActivity extends Activity {
|
||||
MenuItem actionItem = menu.findItem(R.id.menu_item_share_action_provider_action_bar);
|
||||
ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
|
||||
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());
|
||||
|
||||
// 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();
|
||||
overflowProvider.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.
|
||||
overflowProvider.setShareIntent(createShareIntent());
|
||||
|
||||
return true;
|
||||
@@ -71,8 +82,44 @@ public class ActionBarActionProviderActivity extends Activity {
|
||||
private Intent createShareIntent() {
|
||||
Intent shareIntent = new Intent(Intent.ACTION_SEND);
|
||||
shareIntent.setType("image/*");
|
||||
Uri uri = Uri.fromFile(new File(getFilesDir(), "SomeFileToShare"));
|
||||
shareIntent.putExtra(Intent.EXTRA_STREAM, uri.toString());
|
||||
Uri uri = Uri.fromFile(getFileStreamPath("shared.png"));
|
||||
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
|
||||
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 */
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user