我跟着这个
Android - Share on Facebook, Twitter, Mail, ecc
但是我无法在android中使用action_send intent共享图像。请查看我的代码并指导我错误的地方。
saveFileName = "";
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
Bitmap shareImage = null;
shareImage = Bitmap.createBitmap(pc.getDrawingCache());
OutputStream OS = null;
File f1 = new File(Environment.getExternalStorageDirectory()
+ "/Free Hand");
if (f1.isDirectory() == false)
new File(Environment.getExternalStorageDirectory()
+ "/Free Hand").mkdir();
SimpleDateFormat filename = new SimpleDateFormat("yyyyMMddHmmss");
java.util.Date today1 = new java.util.Date();
saveFileName = filename.format(new java.sql.Timestamp(today1
.getTime())) + ".png";
if (saveFileName.length() == 0)
saveFileName = "Free Hand";
file = new File(Environment.getExternalStorageDirectory()
+ "/Free Hand", saveFileName);
try {
outStream = new FileOutputStream(file);
shareImage.compress(Bitmap.CompressFormat.PNG, 100, outStream);
share.putExtra(android.content.Intent.ACTION_SEND, Uri.parse(Environment.getExternalStorageDirectory()
+ "/Free Hand/"+saveFileName+".png"));
Toast.makeText(
getApplicationContext(),
"Saved in " + Environment.getExternalStorageDirectory()
+ "/Free Hand" + "\nFile: " + saveFileName
+ ".png", Toast.LENGTH_LONG).show();
Log.v("Before image", "Before image");
startActivity(Intent.createChooser(share, "Share Image"));
Log.v("After image", "After image");
outStream.flush();
outStream.close();
saveFileName = "";
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"Exception : " + e.getMessage(), Toast.LENGTH_LONG)
.show();
}
save = false;
我得到的Logcat输出是:
06-18 16:08:16.274: D/StatusBarPolicy(1378): [BRIGHTHY] 3. Show 3G indicator
06-18 16:08:17.791: W/ActivityManager(1378): Launch timeout has expired, giving up wake lock!
06-18 16:08:17.804: W/ActivityManager(1378): Activity idle timeout for HistoryRecord{454dabf8 com.facebook.katana/.ComposerActivity}
06-18 16:08:18.004: D/LGE_data(1463): LGE_Fast_Dormancy) new_state(2) i=(0) lastPDPstate=(2)
06-18 16:08:18.124: W/ResponseProcessCookies(15476): Invalid cookie header: "Set-Cookie: datr=HgbfT5h8NEIMcQRt7EurBx16; expires=Wed, 18-Jun-2014 10:42:38 GMT; path=/; domain=.facebook.com; httponly". Unable to parse expires attribute: Wed
06-18 16:08:18.644: D/ACRA(15476): #checkAndSendReports - finish
06-18 16:08:18.684: E/AndroidRuntime(15476): FATAL EXCEPTION: main
06-18 16:08:18.684: E/AndroidRuntime(15476): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.facebook.katana/com.facebook.katana.ComposerActivity}: java.lang.NullPointerException
06-18 16:08:18.684: E/AndroidRuntime(15476): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-18 16:08:18.684: E/AndroidRuntime(15476): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-18 16:08:18.684: E/AndroidRuntime(15476): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-18 16:08:18.684: E/AndroidRuntime(15476): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-18 16:08:18.684: E/AndroidRuntime(15476): at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 16:08:18.684: E/AndroidRuntime(15476): at android.os.Looper.loop(Looper.java:123)
06-18 16:08:18.684: E/AndroidRuntime(15476): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-18 16:08:18.684: E/AndroidRuntime(15476): at java.lang.reflect.Method.invokeNative(Native Method)
06-18 16:08:18.684: E/AndroidRuntime(15476): at java.lang.reflect.Method.invoke(Method.java:521)
06-18 16:08:18.684: E/AndroidRuntime(15476): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-18 16:08:18.684: E/AndroidRuntime(15476): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-18 16:08:18.684: E/AndroidRuntime(15476): at dalvik.system.NativeStart.main(Native Method)
06-18 16:08:18.684: E/AndroidRuntime(15476): Caused by: java.lang.NullPointerException
06-18 16:08:18.684: E/AndroidRuntime(15476): at com.facebook.katana.activity.media.Storage.a(Storage.java:140)
06-18 16:08:18.684: E/AndroidRuntime(15476): at com.facebook.katana.ComposerActivity.a(ComposerActivity.java:742)
06-18 16:08:18.684: E/AndroidRuntime(15476): at com.facebook.orca.activity.FbFragmentActivity.onCreate(FbFragmentActivity.java:48)
06-18 16:08:18.684: E/AndroidRuntime(15476): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-18 16:08:18.684: E/AndroidRuntime(15476): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-18 16:08:18.684: E/AndroidRuntime(15476): ... 11 more
06-18 16:08:18.714: W/ActivityManager(1378): Force finishing activity com.facebook.katana/.ComposerActivity
06-18 16:08:19.214: W/ActivityManager(1378): Activity pause timeout for HistoryRecord{454dabf8 com.facebook.katana/.ComposerActivity}
06-18 16:08:19.574: W/Flex(1378): getString FLEX_COUNTRY_CODE COM
06-18 16:08:19.574: W/Flex(1378): getString FLEX_OPERATOR_CODE OPEN
06-18 16:08:19.574: W/Flex(1378): getString FLEX_OPERATOR_CODE OPEN
06-18 16:08:19.574: W/Flex(1378): getString FLEX_COUNTRY_CODE COM
06-18 16:08:19.574: W/Flex(1378): getString FLEX_OPERATOR_CODE OPEN
06-18 16:08:19.574: D/StatusBarPolicy(1378): [BRIGHTHY] 0. mDataNetType: 8
06-18 16:08:19.574: D/StatusBarPolicy(1378): [BRIGHTHY] curNetwork=40420 curHPLMN=40420
我正在通过USB调试在真实设备上进行测试。
答案 0 :(得分:3)
只需使用以下代码段:
try {
File myFile = new File("/mnt/sdcard/images/2.png");
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext=myFile.getName().substring(myFile.getName().lastIndexOf(".")+1);
String type = mime.getMimeTypeFromExtension(ext);
Intent sharingIntent = new Intent("android.intent.action.SEND");
sharingIntent.setType(type);
sharingIntent.putExtra("android.intent.extra.STREAM",Uri.fromFile(myFile));
startActivity(Intent.createChooser(sharingIntent,"Share using"));
}
catch(Exception e){
Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
}