我想分享用户绘制的图片。方法如下:首先保存一个临时文件,然后使用shareintent共享该文件。详情如下:
<标题>保存:private String save_colored_image(String tempName)
{
File f=null;
try
{
colored_image.setDrawingCacheEnabled(true);
colored_image.buildDrawingCache();
Bitmap bimap = colored_image.getDrawingCache();
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
image_file = new File(Environment.getExternalStorageDirectory(), "Abc");
if(!image_file.exists())
{
image_file.mkdirs();
}
if(tempName==null)
{
long seconds = System.currentTimeMillis();
f = new File(image_file.getAbsolutePath()+File.separator+seconds+".jpg");
}
else
f = new File(image_file.getAbsolutePath()+File.separator+tempName+".jpg");
}
FileOutputStream os = new FileOutputStream(f);
bimap.compress(CompressFormat.JPEG, 100, os);
os.close();
if(tempName==null)
{
Toast.makeText(mContext, "Save : "+f.getAbsolutePath(), Toast.LENGTH_SHORT).show();
updateMedia(f.getAbsolutePath());
}
else if (tempName=="shareit")
{
Toast.makeText(mContext, "Sharing...!", Toast.LENGTH_SHORT).show();
}
}
catch (Exception e)
{
if(tempName==null)
Toast.makeText(mContext, "Photo is not saved! Please try again!"+f.getAbsolutePath(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
colored_image.setDrawingCacheEnabled(false);
return f.getAbsolutePath();
}
<标题> Shareit: private void shareit()
{
String filepath = save_colored_image("shareit");
if (filepath!=null && filepath!="")
{
Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri phototUri = Uri.parse(filepath);
File file = new File(phototUri.getPath());
Toast.makeText(mContext, "file path: "+file.getPath(), Toast.LENGTH_SHORT).show();
if(file.exists())
{
// file create success
} else
{
// file create fail
}
shareIntent.setData(phototUri);
shareIntent.setType("image/png");
shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
startActivity(Intent.createChooser(shareIntent, "Share Image Via..."));
}
}
<标题> Logcat: 06-20 01:08:17.655: W/dalvikvm(17362): threadid=30: thread exiting with uncaught exception (group=0x41ea8700)
06-20 01:08:17.665: E/AndroidRuntime(17362): FATAL EXCEPTION: SyncAdapterThread-1
06-20 01:08:17.665: E/AndroidRuntime(17362): java.lang.NullPointerException
06-20 01:08:17.665: E/AndroidRuntime(17362): at com.google.android.gm.provider.E.a(SourceFile:87)
06-20 01:08:17.665: E/AndroidRuntime(17362): at com.google.android.common.a.onPerformSync(SourceFile:37)
06-20 01:08:17.665: E/AndroidRuntime(17362): at com.google.android.gm.provider.E.onPerformSync(SourceFile:77)
06-20 01:08:17.665: E/AndroidRuntime(17362): at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:254)
06-20 01:08:17.670: I/ActivityManager(2416): Notify an ApplicationCrash
06-20 01:08:17.680: I/dumpstate(17500): begin
<标题>问题:以上内容只能通过whatsapp和Line等几个应用程序共享。然而,分享到Gmail会引发NPE崩溃。是什么触发了NPE ?如何解决上述问题?
谢谢! !
标题>标题>标题>标题>似乎颠倒了一些代码行,然后它将工作,如下所示:
private void shareit()
{
String filepath = save_colored_image("shareit");
if (filepath!=null && filepath!="")
{
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
File imageFileToShare = new File(filepath);
if(imageFileToShare.exists())
{
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image!"));
} else
{
Toast.makeText(mContext, "Unable to save!", Toast.LENGTH_SHORT).show();
}
}
}