0

我想从 android.my 中的指定路径共享图像。我的代码正在运行,但图像不能像 facebook、google drive 等那样共享!,我的共享意图不会将图像发送到其他应用程序!。我的代码有什么问题可以帮助我任何人都非常感谢!

这是我的代码:

                File wallpaperDirectory = new File("/sdcard/myimage/");
                // have the object build the directory structure, if needed.
                wallpaperDirectory.mkdirs();

                File file = new File(wallpaperDirectory, "Share.jpg");
                try {
                    fOut = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                    fOut.flush();
                    fOut.close();
                    MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), e.toString(),5000).show();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            Intent sharingIntent = new Intent(Intent.ACTION_SEND);
            Uri screenshotUri = Uri.parse(file.getAbsolutePath());

            sharingIntent.setType("image/jpg");
            sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
            startActivity(Intent.createChooser(sharingIntent, "Share image using"));

这是我的输出:

在此处输入图像描述

单击选项后不会将图像共享给其他应用程序!

4

2 回答 2

0

我在下面的链接中找到了很好的图像共享教程:

http://collegewires.com/android/2012/06/android-implementing-a-share-intent/

我的问题是文件路径不正确之前添加路径file://

像下面的代码:

                Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                Uri screenshotUri = Uri.parse("file://" + file.getAbsolutePath());

                sharingIntent.setType("image/jpg");
                sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
                startActivity(Intent.createChooser(sharingIntent, "Share image using"));

现在我的问题解决了!

于 2012-08-13T08:15:44.653 回答
0

如果您已经在bitmap变量中加载了位图,请尝试以下操作:

                Uri U = null;
                File folderShare = new File(btnShare.getContext().getExternalFilesDir(null) + "/download/");
                folderShare.mkdirs();
                File fileShare = new File(folderShare, "Image-android.jpg");
                fileShare.delete();
                fileShare = new File(folderShare, "Image-android.jpg");
                if (fileShare.isFile())
                        try {    
                            FileOutputStream out = new FileOutputStream(fileShare);
                            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
                             U = Uri.fromFile(fileShare);
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                       } catch (FileNotFoundException e) {
                           e.printStackTrace();
                       } catch (IOException e) {
                           e.printStackTrace();                       

                String email_subject = getString(R.string.email_subject);
                String email_content = getString(R.string.email_content);
                String email_chooser = getString(R.string.email_chooser);

                final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

                emailIntent.setType("text/plain");
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, email_subject);
                emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, U);
                emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, email_content);

                v.getContext().startActivity(Intent.createChooser(emailIntent, email_chooser));
于 2012-08-13T08:58:03.650 回答