我正在为Android构建一个phonegap应用程序,需要一种方法来使用javascript从应用程序www目录中包含的.jpg设置壁纸。我将如何构建一个与我的phonegap应用程序www文件夹中的资源配合使用的phonegap插件?
答案 0 :(得分:0)
只需从资产文件夹中读取文件。带插件
import java.io.IOException;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.PluginResult.Status;
import org.json.JSONArray;
import android.app.WallpaperManager;
import android.content.Context;
public class testPlugin extends Plugin {
public final String ACTION_SET_WALLPAPER = "setWallPaper";
@Override
public PluginResult execute(String action, JSONArray arg1, String callbackId) {
PluginResult result = new PluginResult(Status.INVALID_ACTION);
if (action.equals(ACTION_SET_WALLPAPER)) {
WallpaperManager wallpaperManager = WallpaperManager.getInstance((Context) this.ctx);
try {
InputStream bitmap=null;
bitmap=getAssets().open("www/img/" + arg1.getString(0));//reference to image folder
Bitmap bit=BitmapFactory.decodeStream(bitmap);
wallpaperManager.setBitmap(bit);
result = new PluginResult(Status.OK);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result = new PluginResult(Status.ERROR, e.getMessage());
}
}
return result;
}
}
这是javascript文件test.js
var TestPlugin = function () {};
TestPlugin.prototype.set = function (ms, successCallback, failureCallback) {
// navigator.notification.alert("OMG");
return cordova.exec(successCallback, failureCallback, 'testPlugin', "setWallPaper", [ms]);
};
PhoneGap.addConstructor(function() {
PhoneGap.addPlugin("test", new TestPlugin());
})
和主文件调用带有imagefilename的插件
window.plugins.test.set("imageFileName.jpg",
function () {
navigator.notification.alert("Set Success");
},
function (e) {
navigator.notification.alert("Set Fail: " + e);
}
);
;
使用Android设备权限
<uses-permission android:name="android.permission.SET_WALLPAPER" />
和plugin.xml
<plugin name="testPlugin" value="com.android.test.testPlugin"/>