Java 图像不';保存后无法添加到库中

Java 图像不';保存后无法添加到库中,java,android,Java,Android,我使用answer中的两个命令将位图保存在SD卡上,然后通过intent共享它 这是我最后的代码: View u = findViewById(R.id.mainL); u.setDrawingCacheEnabled(true); LinearLayout z = (LinearL

我使用answer中的两个命令将位图保存在SD卡上,然后通过intent共享它

这是我最后的代码:

                    View u = findViewById(R.id.mainL);
                    u.setDrawingCacheEnabled(true);                                                
                    LinearLayout z = (LinearLayout) findViewById(R.id.mainL);
                    int totalHeight = z.getHeight();
                    int totalWidth = z.getWidth();
                    u.layout(0, 0, totalWidth, totalHeight);    
                    u.buildDrawingCache(true);
                    Bitmap b = Bitmap.createBitmap(u.getDrawingCache()); 
                    u.setDrawingCacheEnabled(false);
                    String filePath = Environment.getExternalStorageDirectory()
                            + File.separator + "pics/screenshot.jpeg";
                    File imagePath = new File(filePath);
                    FileOutputStream fos;
                    try {
                        fos = new FileOutputStream(imagePath);
                        b.compress(CompressFormat.JPEG, 100, fos);
                        fos.flush();
                        fos.close();
                    } catch (FileNotFoundException e) {
                        Log.e("GREC", e.getMessage(), e);
                    } catch (IOException e) {
                        Log.e("GREC", e.getMessage(), e);
                    }
                    Uri bmpUri = Uri.parse(filePath);
                    Intent shareIntent = new Intent(Intent.ACTION_SEND);
                    shareIntent.setType("image/jpeg");
                    shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
                    shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(Intent.createChooser(shareIntent, "Share"));
但现在我有两个问题

1) mobile gallery无法访问结果图像(screenshot.png)(但sd卡的pics文件夹中有一个图像文件)

2) 当我试图通过intent共享它时,它不会发送,例如,当我通过蓝牙发送它时,接收器小工具会中断发送操作


谢谢。

哦,只要在图库中添加任何图片后粘贴这一行,它就会刷新你的图库

它对我起了作用,希望能帮助你:)

试试这个

                //save image into sdcard
                FrameLayout f=(FrameLayout)findViewById(R.id.framelayout);                                                               
                f.setDrawingCacheEnabled(true);
                Bitmap bm = f.getDrawingCache();
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bm.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                byte[] imageInByte1 = stream.toByteArray();

                String  path = Environment.getExternalStorageDirectory().toString();
                File  imgDirectory = new File(Environment.getExternalStorageDirectory()+"/images/");
                imgDirectory.mkdirs();
                OutputStream fOut = null;
                File file = null;
                file = new File(path,"/images/"+etcardname.getText().toString()+ ".png");
                Toast.makeText(getBaseContext(), "saved at: " + file.getAbsolutePath(), Toast.LENGTH_LONG).show();
                fOut = new FileOutputStream(file);
                bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                fOut.flush();
                fOut.close();

              //share  image
               Intent share = new Intent(android.content.Intent.ACTION_SEND);
               share.setType("image/*");
               share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file));
               startActivity(Intent.createChooser(share, "Share image using"));                                                                                    

你的图像显示正确吗?是的,这很有帮助。但在此之前,我应该将目录更改为“imagename.jpg”。谢谢
                //save image into sdcard
                FrameLayout f=(FrameLayout)findViewById(R.id.framelayout);                                                               
                f.setDrawingCacheEnabled(true);
                Bitmap bm = f.getDrawingCache();
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bm.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                byte[] imageInByte1 = stream.toByteArray();

                String  path = Environment.getExternalStorageDirectory().toString();
                File  imgDirectory = new File(Environment.getExternalStorageDirectory()+"/images/");
                imgDirectory.mkdirs();
                OutputStream fOut = null;
                File file = null;
                file = new File(path,"/images/"+etcardname.getText().toString()+ ".png");
                Toast.makeText(getBaseContext(), "saved at: " + file.getAbsolutePath(), Toast.LENGTH_LONG).show();
                fOut = new FileOutputStream(file);
                bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                fOut.flush();
                fOut.close();

              //share  image
               Intent share = new Intent(android.content.Intent.ACTION_SEND);
               share.setType("image/*");
               share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file));
               startActivity(Intent.createChooser(share, "Share image using"));