簡體   English   中英

保存后,圖像不會添加到圖庫

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

我正在使用這個答案中的幾個命令將我的位圖保存在SD卡上,然后通過意圖共享它。

這是我的最終代碼:

                    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)結果Image(screenshot.png)無法通過移動圖庫訪問(雖然在SD卡中的pics文件夾中有圖像文件)。

2)當我嘗試通過意圖分享它時,它不會發送,例如當我通過藍牙發送時,接收器小工具會中斷發送操作。

謝謝。

哦,只需粘貼此行,在庫中添加任何圖片后,它將刷新您的圖庫

它對我有用希望能幫助你:)

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

嘗試這個

                //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