私は Android 用の phonegap アプリを構築していますが、javascript を使用してアプリの www ディレクトリに含まれる .jpg から壁紙を設定する方法が必要です。phonegap アプリの www フォルダー内のリソースで動作する phonegap プラグインを構築するにはどうすればよいですか?
2550 次
1 に答える
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());
})
およびメインファイル呼び出し Plugin with 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"/>
于 2012-07-24T02:55:12.330 に答える