2012-01-23 26 views
0
if (action.equals("saveToGallery")) { 

         JSONObject obj = args.getJSONObject(0); 
         String imageSource = obj.has("imageSrc") ? obj.getString("imageSrc") : null; 
         String imageName = obj.has("imageName") ? obj.getString("imageName") : null; 
         String savedImgSrc = saveImageToGallery(imageSource, imageName); 
         Log.v("SAve To Gallery ", "saved file url: " + savedImgSrc); 

         return new PluginResult(PluginResult.Status.OK); 
        } 
        return new PluginResult(PluginResult.Status.INVALID_ACTION); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
        return new PluginResult(PluginResult.Status.JSON_EXCEPTION); 
       } 

public String saveImageToGallery(String imgSrc, String imgName) { 
     Log.v("Save To Gallery ", "image SRc: " + imgSrc + " , image Name:" 
       + imgName); 

     Context ctx = this.ctx; 
     AssetManager assmgr = ctx.getAssets(); 
     File tempDir = new File("/sdcard/HarmonyDayApp/wallpapers/"); 
     tempDir.mkdirs(); 
     File file = new File(tempDir, imgName); 
     try { 
      InputStream is = null; 
      is = assmgr.open("www/" + imgSrc); 
      OutputStream os = new FileOutputStream(file); 
      byte[] data = new byte[is.available()]; 
      is.read(data); 
      os.write(data); 
      sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+Environment.getExternalStorageDirectory()))); 
      is.close(); 
      os.close(); 
     } catch (IOException ex) { 
      Log.w("ExternalStorage", "Error writing " + file, ex); 
     } 
     return file.getAbsolutePath(); 

    } 

這是我用於將圖像保存到設備庫的代碼。但是,保存圖像後,如果我imeediit檢查畫廊,圖像不存在。它是在我重新加載應用程序或某個時間之後的圖庫時發生的。對這個問題的任何建議將會有所幫助。保存後圖片中未反映的圖片

回答

0

試試這個:

在畫廊中添加圖像後,你需要廣播

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse(「file://」+ 
Environment.getExternalStorageDirectory()))); 

希望它能爲你工作。 :)

+0

我需要爲此創建sendBroadcat mehtod。你能否告訴我需要什麼方法的內容,我應該把它放在哪裏? – Khush

+0

將圖像保存到圖庫後,只需要粘貼:Log.v(「SAVE To Gallery」,「saved file url:」+ savedImgSrc); sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse(「file://」+ Environment.getExternalStorageDirectory())));而已。無需創建任何廣播。 – Andy

+0

但它給出了一個錯誤sayin「Sendbroadcast不是一個函數,創建一個。」 – Khush

0

您需要手動掃描介質之後,你的形象已被保存,通過下面的代碼:

// Tell the media scanner about the new file so that it is 
    // immediately available to the user. 
    MediaScannerConnection.scanFile(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

它說scanFile不能用於我的WebIntent插件。 – Khush