【发布时间】:2014-08-10 07:58:45
【问题描述】:
我想分享用户绘制的图片。方法如下:先保存一个临时文件,然后使用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();
}
分享:
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,如何解决?
谢谢!!!
【问题讨论】:
-
NPE 是否告诉您是哪一行导致崩溃?
-
添加了logcat,好像Gmail自带触发错误的那一行?
标签: android file android-intent share