在drawable文件夹中共享png图像

时间:2013-08-29 04:58:37

标签: android share social-networking

我正在使用以下代码集成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 image放在drawable文件夹中。但图像无法发送。这是因为在setType中,它是 image / 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();

               }

            }

5 个答案:

答案 0 :(得分:15)

我找到了解决方案,我不应该在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();
    }

答案 1 :(得分:11)

Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.xxxx);
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/LatestShare.jpg";
OutputStream out = null;
File file=new File(path);
try {
    out = new FileOutputStream(file);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
    out.flush();
    out.close();
} catch (Exception e) {
    e.printStackTrace();
}
path=file.getPath();
Uri bmpUri = Uri.parse("file://"+path);
Intent shareIntent = new Intent();
shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/jpg");
startActivity(Intent.createChooser(shareIntent,"Share with"));

这对我很有用,这需要写入权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

将此行添加到Android清单以获取写入权限。

答案 2 :(得分:9)

您可以使用以下方法分享...

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/png");
Uri uri = Uri.parse("android.resource://your package name/"+R.drawable.ic_launcher);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello, This is test Sharing");
startActivity(Intent.createChooser(shareIntent, "Send your image"));

尝试并测试......为我工作

答案 3 :(得分:4)

首先添加权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

使用res中的位图

Bitmap b =BitmapFactory.decodeResource(getResources(),R.drawable.userimage);
            Intent share = new Intent(Intent.ACTION_SEND);
            share.setType("image/jpeg");
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            String path = MediaStore.Images.Media.insertImage(getContentResolver(),
                    b, "Title", null);
            Uri imageUri =  Uri.parse(path);
            share.putExtra(Intent.EXTRA_STREAM, imageUri);
            startActivity(Intent.createChooser(share, "Select"));

通过蓝牙测试。

像魅力一样工作。

答案 4 :(得分:1)

您无法直接与应用内部存储共享uri(当然,应用的资源将始终位于内部存储中)

实现这一目标的方法有两种。

  1. 将图像复制到外部存储,然后从那里共享。请参阅this

  2. 编写内容提供商以共享图像。对于那个,请参考Create and Share a File from Internal Storage