Android:无法通过Intent分享图片

时间:2014-06-19 17:05:10

标签: android file android-intent share

我想分享用户绘制的图片。方法如下:首先保存临时文件,然后使用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会发生什么?如何才能解决上述问题?

感谢!!!

1 个答案:

答案 0 :(得分:0)

它似乎正在反转某些代码行,然后它将起作用,如下所示:

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();
        }
    }   
}