Android保存并从SDCard共享图像

时间:2014-05-26 10:24:07

标签: android

您好我正在尝试将drawable作为位图保存到SDCard,然后使用共享意图共享它。但我遇到的问题是它只是不共享图像有没有人知道如何做到这一点或我出错了?

继承人到目前为止我所尝试过的事情

getHelp.setOnClickListener(new View.OnClickListener() {

                        public void onClick(View arg0) {

                            saveBitmapToExternalStorage(this, imageResourceFor Drawable, imageName);
                            File imageFile = new File("/sdcard/myfolder/"+ kitImagePath+".png");
                            Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
                            Intent shareIntent = new Intent();
                            shareIntent.setAction(Intent.ACTION_SEND);
                            shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Title");
                            shareIntent.putExtra(Intent.EXTRA_TEXT, "Subtitle");
                            shareIntent.putExtra(Intent.EXTRA_STREAM, bitmap);  //optional//use this when you want to send an image
                            shareIntent.setType("image/png");
                            shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                            startActivity(Intent.createChooser(shareIntent, "send"));
                        }

                    });

 public static void saveBitmapToExternalStorage(Context context, int imageResource, String imageName){

        Bitmap bitmap=BitmapFactory.decodeResource(context.getResources(),imageResource);

        //generate file
        File dir = new File (Environment.getExternalStorageDirectory(), "/myfolder");
        File f = new File(dir, String.format(imageName + ".png"));

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.PNG, 0 , bos);
        byte[] bitmapdata = bos.toByteArray();
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(f);
            fos.write(bitmapdata);  
            fos.flush();
            fos.close();
            bos.close();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

2 个答案:

答案 0 :(得分:3)

而不是使用Bitmap使用其URI来共享。

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Title");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Subtitle");
Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+ kitImagePath+".png"));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);  //optional//use this when you want to send an image
shareIntent.setType("image/png");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "send"));

另外,正如+ Der Golem所说。使用Environment.getExternalStorageDirectory().getAbsolutePath()获取外部存储路径。

答案 1 :(得分:0)

在清单文件中添加以下权限。

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