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

android– 在drawable文件夹中共享一个png图像,第1张

概述我正在使用以下代码为应用程序集成share.privatevoidsocialShare(){Uriuri=Uri.parse("android.resource://com.example.myproject/drawable/appicon");IntentshareIntent=newIntent();shareIntent.setAction(Intent.ACTION_SEND);

我正在使用以下代码为应用程序集成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();    }
总结

以上是内存溢出为你收集整理的android – 在drawable文件夹中共享一个png图像全部内容,希望文章能够帮你解决android – 在drawable文件夹中共享一个png图像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1103555.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存