简体   繁体   English

保存后,图像不会添加到图库

[英]Image doesn't add to Gallery after saving

I'm using couple of commands from this answer to save my bitmap on SD card and then share it via intent. 我正在使用这个答案中的几个命令将我的位图保存在SD卡上,然后通过意图共享它。

and here is my final code: 这是我的最终代码:

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

but now I have two problems. 但现在我有两个问题。

1)the result Image (screenshot.png) is not reachable with mobile gallery (there is a image file in pics folder in sd card although). 1)结果Image(screenshot.png)无法通过移动图库访问(虽然在SD卡中的pics文件夹中有图像文件)。

2)when I try to share it via intent, it doesn't send and for example when I send it via Bluetooth the receiver gadget breaks the sending operation. 2)当我尝试通过意图分享它时,它不会发送,例如当我通过蓝牙发送时,接收器小工具会中断发送操作。

thanks. 谢谢。

ohk just paste this line after adding any pic in the gallery it will refresh your gallery 哦,只需粘贴此行,在库中添加任何图片后,它将刷新您的图库

It worked for me hope will help u :) 它对我有用希望能帮助你:)

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(filePath))));

try this 尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM