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檢查畫廊,圖像不存在。它是在我重新加載應用程序或某個時間之後的圖庫時發生的。對這個問題的任何建議將會有所幫助。保存後圖片中未反映的圖片
我需要爲此創建sendBroadcat mehtod。你能否告訴我需要什麼方法的內容,我應該把它放在哪裏? – Khush
將圖像保存到圖庫後,只需要粘貼:Log.v(「SAVE To Gallery」,「saved file url:」+ savedImgSrc); sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse(「file://」+ Environment.getExternalStorageDirectory())));而已。無需創建任何廣播。 – Andy
但它給出了一個錯誤sayin「Sendbroadcast不是一個函數,創建一個。」 – Khush