Javascript 保存后未反映在库中的图像
这是我用来将图像保存到设备库的代码。但是,在图像保存后,如果我立即查看图库,则图像不在那里。当我重新加载应用程序或某个时间后重新加载库时,它就会出现。对此问题的任何建议都会有帮助。您需要在保存图像后,通过以下代码手动扫描媒体:Javascript 保存后未反映在库中的图像,javascript,android,json,Javascript,Android,Json,这是我用来将图像保存到设备库的代码。但是,在图像保存后,如果我立即查看图库,则图像不在那里。当我重新加载应用程序或某个时间后重新加载库时,它就会出现。对此问题的任何建议都会有帮助。您需要在保存图像后,通过以下代码手动扫描媒体: if (action.equals("saveToGallery")) { JSONObject obj = args.getJSONObject(0); String i
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();
}
试试这个:
在gallery中添加图像后,您需要进行广播
// 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);
}
});
希望它能对你有用。:) 它说scanFile不能用于我的WebIntent插件。我需要为此创建一个sendBroadcat方法。你能告诉我这个方法需要什么内容,我应该把它放在哪里吗?在gallery中保存图像后,只需要粘贴这个:Log.v(“保存到gallery”,“保存的文件url:+savedImgSrc”);sendBroadcast(新的Intent(Intent.ACTION\u MEDIA\u挂载,Uri.parse(“文件:/”+Environment.getExternalStorageDirectory()));就这样。无需创建任何CBroadcast。但它会在“Sendbroadcast不是函数。创建一个”中给出一个错误。请检查此项:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse(“file://”+
Environment.getExternalStorageDirectory())));