Android:无法通过 Intent 分享图片
Posted
技术标签:
【中文标题】Android:无法通过 Intent 分享图片【英文标题】:Android: Unable to share pictures via Intent 【发布时间】: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自带触发错误的那一行? 【参考方案1】:好像颠倒了一些代码行,然后就可以了,如下:
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();
【讨论】:
以上是关于Android:无法通过 Intent 分享图片的主要内容,如果未能解决你的问题,请参考以下文章