Javascript 访问SD卡中的图像
我已使用上述代码访问sd卡中的数据。但现在我需要做的是,获取图像的路径,并将其放入一个diff对象中,该对象可以访问路径并显示这些图像。我不知道该怎么做。Javascript 访问SD卡中的图像,javascript,cordova,Javascript,Cordova,我已使用上述代码访问sd卡中的数据。但现在我需要做的是,获取图像的路径,并将其放入一个diff对象中,该对象可以访问路径并显示这些图像。我不知道该怎么做。 非常感谢您的帮助。您所能做的就是为您正在开发的平台编写一个phonegap插件。我假设它是安卓系统 当您调用Phonegap.exec来调用插件时,插件会获取sd卡路径 var pictureSource; // picture source var destinationType; // sets the format
非常感谢您的帮助。您所能做的就是为您正在开发的平台编写一个phonegap插件。我假设它是安卓系统
当您调用Phonegap.exec来调用插件时,插件会获取sd卡路径
var pictureSource; // picture source
var destinationType; // sets the format of returned value
var photoid=window.localStorage.getItem("photoid");
var photoData=null;
// Wait for PhoneGap to connect with the device
//
document.addEventListener("deviceready",onDeviceReady,false);
// PhoneGap is ready to be used!
//
function onDeviceReady() {
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.DestinationType;
}
// A button will call this function
//
function getPhoto(source) {
alert("Entered sd card");
// Retrieve image file location from specified source
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
destinationType: destinationType.FILE_URI,
sourceType: source });
}
function onPhotoDataSuccess(imageData) {
console.log(imageData);
// Get image handle
var smallImage = document.getElementById('photos');
// Unhide image elements
//
smallImage.style.display = 'block';
// Show the captured photo
// The inline CSS rules are used to resize the image
//
smallImage.src = "data:image/jpeg;base64," + imageData;
alert(imageData);
photoData = imageData;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
fileSystem.root.getFile("/sdcard/external_sd/"+photoid+".jpg", null, gotFileEntry, fail);
}
function gotFileWriter(writer) {
writer.onwrite = function(evt) {
alert("write success");
};
writer.write(photoData);
}
function fail(error) {
alert(error.code);
}
/* function onPhotoURISuccess(imageURI) {
// Uncomment to view the image file URI
console.log(imageURI);
alert("photo captured");
uploadPhoto(imageURI);
} */
/* function getPhoto(source) {
// Retrieve image file location from specified source
navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
destinationType: destinationType,
sourceType: source });
} */
// Called if something bad happens.
//
function onFail(message) {
alert('Failed because: ' + message);
}
然后进行基本搜索以获取所有.jpg和.png文件,并返回所有文件路径的json。您需要用户选择的图像路径,还是所有图像的路径?不清楚您想要实现什么。请编辑,让它更清楚一点。一个用例示例会很好。我需要创建一个对象,该对象将包含sd卡中所有图像的路径。i、 e.我需要能够通过另一个具有图像路径的对象访问所有sd卡图像,并且应该能够打开所有图像。是否要创建sd卡中所有图像的库或其他内容?这可能是选项之一。但是我现在要找的是我能不能得到SD卡上的图像列表??我希望android和IOS都能使用相同的代码。为了完成这项工作,你必须为iphone n android编写单独的插件。你可以用我描述的方式很容易地获得图像列表。我没有其他选项为sd卡图像制作单独的图库并显示它们吗?你可以查看phonegap的文件api,你可以使用它来迭代文件目录。
Environment.getExternalStorageDirectory().getAbsolutePath()