2017-01-10 70 views
0

所以我有一個位圖,編輯後我希望用戶可以保存它並在圖庫中看到它。我在谷歌搜索和發現很多想法,但我所嘗試的都沒有工作。這是我的代碼:如何在圖庫中保存位圖?

 ((ImageButton) findViewById(R.id. imageButton)).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString(); 
      File myDir = new File(root + "/saved_images"); 
      myDir.mkdirs(); 
      Random generator = new Random(); 
      int n = 10000; 
      n = generator.nextInt(n); 
      String fname = "Image-" + n + ".jpg"; 
      File file = new File(myDir, fname); 
      if (file.exists()) 
       file.delete(); 
      try { 
       FileOutputStream out = new FileOutputStream(file); 
       mutableBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); 
       out.flush(); 
       out.close(); 
      } 
      catch (Exception e) { 
       e.printStackTrace(); 
      } 


      // Tell the media scanner about the new file so that it is 
      // immediately available to the user. 
      MediaScannerConnection.scanFile(MainActivity7.this, new String[] { file.toString() }, null, 
        new MediaScannerConnection.OnScanCompletedListener() { 
         public void onScanCompleted(String path, Uri uri) { 
          Log.i("ExternalStorage", "Scanned " + path + ":"); 
          Log.i("ExternalStorage", "-> uri=" + uri); 
         } 
        }); 
     } 
    }); 

所以,什麼是錯,還是你有任何其他的解決方案如何我能救我的位圖在畫廊?

+0

當您檢查LogCat時,您是否看到來自應用程序的堆棧跟蹤? – CommonsWare

回答

0

進入設置>應用程序,找到您的應用程序是否允許存儲權限。如果沒有,請允許並重試。