微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 在drawable文件夹中共享一个png图像

我正在使用以下代码为应用程序集成share.

private void socialShare()
    {
        Uri uri = Uri.parse("android.resource://com.example.myproject/drawable/appicon");
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
        shareIntent.putExtra(Intent.EXTRA_TEXT, "sharing myapp");
        shareIntent.setType("image/jpeg");
        startActivity(Intent.createChooser(shareIntent, "Share from"));
    }

如上面的代码,我试图把png图像放在drawable文件夹中.但图像无法发送.是因为在setType中,它的图像/ jpeg?我不能使用jpeg,因为它失去了透明度.有人可以建议我如何与图像分享

这是我用来将图像从drawable复制到sdcard的代码

String commonPath = Environment.getExternalStorageDirectory().toString() + "/MyAppFolder"; 
        File direct = new File(commonPath);

        if(!direct.exists())
        {
            if(direct.mkdir()) 
              {
                Log.d("tag","directory created");
               //directory is created;
              }

        }

        Bitmap bm = BitmapFactory.decodeResource( getResources(), R.drawable.sharingimage);
        OutputStream outStream = null;
           File savingFile = new File(commonPath, "shareImage.png");
           if(!savingFile.exists())
           {
               Log.d("tag","file is created");

           try {
                savingFile.createNewFile();
                outStream = new FileOutputStream(savingFile);
                bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
                outStream.flush();
                outStream.close();

                Log.d("tag","Saved");

               } catch (FileNotFoundException e) {
                // Todo Auto-generated catch block
                e.printstacktrace();

               } catch (IOException e) {
                // Todo Auto-generated catch block
                e.printstacktrace();

               }

            }

解决方法:

我找到了解决方案,我不应该在SD卡上复制图像.这里是:

    try {
        Uri imageUri = null;
        try {
            imageUri = Uri.parse(MediaStore.Images.Media.insertimage(this.getContentResolver(),
                    BitmapFactory.decodeResource(getResources(), R.drawable.fragment_menu_logo), null, null));
        } catch (NullPointerException e) {
        }
        String text = getString(R.string.share_text);
        // Launch the Google+ share dialog with attribution to your app.
        Intent shareIntent = new PlusShare.Builder(mActivity)
                .setType("image/*")
                .setText(text)
                .addStream(imageUri)
                .getIntent();
        startActivity(shareIntent);
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(mActivity, mActivity.getString(R.string.share_google_plus_not_installed), Toast.LENGTH_LONG).show();
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐