在共享文件夹中绘制一个PNG图像 [英] Sharing a png image in drawable folder

查看:125
本文介绍了在共享文件夹中绘制一个PNG图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整合共享与以下code的应用程序。

 私人无效socialShare()
    {
        开放的我们的uri = Uri.parse(android.resource://com.example.myproject/drawable/appicon);
        意图shareIntent =新意图();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM,URI);
        shareIntent.putExtra(Intent.EXTRA_TEXT,共享的myapp);
        shareIntent.setType(为image / jpeg);
        startActivity(Intent.createChooser(shareIntent,从共享));
    }
 

如上code,我试图把 png图片这是在绘制文件夹。但图像不能被发送。那是因为在的setType,它的为image / jpeg ?我不能使用JPEG格式,因为它失去了透明度。可有一个人请建议我如何与图片分享?

下面是我用从绘制的图像复制到SD卡的code:

 字符串文件版本= Environment.getExternalStorageDirectory()的toString()+/ MyAppFolder。
        文件直接=新的文件(文件版本);

        如果(!direct.exists())
        {
            如果(direct.mkdir())
              {
                Log.d(目录下创建,标签,);
               //目录中创建;
              }

        }

        位图BM = BitmapFactory.de codeResource(getResources(),R.drawable.sharingimage);
        OutputStream的outStream = NULL;
           文件savingFile =新的文件(文件版本,shareImage.png);
           如果(!savingFile.exists())
           {
               Log.d(标记,创建文件);

           尝试 {
                savingFile.createNewFile();
                outStream =新的FileOutputStream(savingFile);
                bm.com preSS(Bitmap.Com pressFormat.PNG,100,outStream);
                outStream.flush();
                outStream.close();

                Log.d(标签,拯救);

               }赶上(FileNotFoundException异常E){
                // TODO自动生成的catch块
                e.printStackTrace();

               }赶上(IOException异常E){
                // TODO自动生成的catch块
                e.printStackTrace();

               }

            }
 

解决方案

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

 尝试{
        乌里imageUri = NULL;
        尝试 {
            imageUri = Uri.parse(MediaStore.Images.Media.insertImage(this.getContentResolver(),
                    BitmapFactory.de codeResource(getResources(),R.drawable.fragment_menu_logo),NULL,NULL));
        }赶上(NullPointerException异常E){
        }
        字符串文本=的getString(R.string.share_text);
        //启动与归属的Google+分享对话框,您的应用程序。
        意图shareIntent =新PlusShare.Builder(mActivity)
                .setType(图像/ *)
                .setText(文本)
                .addStream(imageUri)
                .getIntent();
        startActivity(shareIntent);
    }赶上(android.content.ActivityNotFoundException前){
        Toast.makeText(mActivity,mActivity.getString(R.string.share_google_plus_not_installed),Toast.LENGTH_LONG).show();
    }
 

I am integrating share with the following code for the app.

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

As in the above code, I am trying to put png image which is in drawable folder. But the image is unable to be sent. Is that because in setType, it's image/jpeg? I can't use jpeg, because it loses transparency. Can some one please suggest me how to share with image?

Here is the code that I use to copy the image from drawable to 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();

               }

            }

解决方案

I found solution where I should not copy image on sd card. Here it is:

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

这篇关于在共享文件夹中绘制一个PNG图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆