I want to openOrCreate database in SDcard / Media Card. When i run the application in device (BlackBerry Curve 8900), i find only one root i.e "system/" and running application in simulator (9500), i find three roots as shown in comment in code. I am getting error at;
_db = DatabaseFactory.openOrCreate(_uri);
(error: Method "toString" with signature "()Ljava/lang/String;" is not applicable on this object)
And i am not able to understand what is this error about.
Here is the code.
public void getValues() throws Exception
{
boolean sdCardPresent = false;
String root = null;
Enumeration e = FileSystemRegistry.listRoots();
while (e.hasMoreElements())
{
root = (String)e.nextElement();
System.out.println("Value of root::" +root); // value of root = "system/" when run in device and
// value of root = "store/" "SDCard/" "system/" when run in simulator
if(root.equalsIgnoreCase("system/"))
{
sdCardPresent = true;
}
}
System.out.println("--------------------getValues()----------------------------------");
URI _uri = URI.create(Global.DB_PATH + Global.DB_Main);
System.out.println("Valud of uri::" +_uri);
_db = DatabaseFactory.openOrCreate(_uri); //getting error here.
System.out.println("Valud of _db::" +_db);
_db.close();
I tried these three paths, getting output with "/store"(when run in simulator) but error with rest two paths.Even using "/store" in device is showing the same error.
Global.DB_PATH = "/MediaCard/databases/";
Global.DB_PATH = "/SDCard/databases/";
Global.DB_PATH = "/store/databases/";
Is there any way how to get SDCard/Media Card as root so that i can copy the database in there?
My guess is when you are running your app on a real device you have USB cable plugged in to the device. If this is the case, try to unplug the cable and rerun the app. You may use Dialog.inform() to quickly check what roots you get this time.
private ObjectListField getFileList() {
if (fileList == null) {
fileList = new ObjectListField();
String[] roots = new String[3];
Enumeration enum = FileSystemRegistry.listRoots();
int x = 0;
while (enum.hasMoreElements()) {
if (x < 3) {
roots[x] = enum.nextElement().toString();
}
x++;
}
enum = FileSystemRegistry.listRoots();
fileList.set((roots[2] != null) ? roots : new String[]{"system/", "SDCard/", "store/"});
}
return fileList;
}
Try this code.
Related
I am trying to send an email using the method described in this tutorial with a model structure form this tutorial and im partially successfull in doing so. The only issue I am having is the fact that files sent as attachments are damaged. I have tried to get it working in so many ways that I lost count. Obviously haven't been trying hard enough since I didn't find the answer, but decided to ask while I continue looking for an answer.
My controller action is as follows:
public async Task<ActionResult> Index( [Bind(Include = "column names..")] Contact contact, HttpPostedFileBase upload){
if (ModelState.IsValid && status)
{
var message = new MailMessage();
if (upload != null && upload.ContentLength > 0)
{
// 4MB -> 4000 * 1024
const int maxFileSize = 4096000;
if (upload.ContentLength < maxFileSize)
{
var document = new File
{
FileName = System.IO.Path.GetFileName(upload.FileName),
FileType = FileType.Document,
ContentType = upload.ContentType
};
var supportedTypes = new[] {"doc", "docx", "pdf", "jpg"};
var extension = System.IO.Path.GetExtension(document.FileName);
if (extension != null)
{
var fileExt = extension.Substring(1);
if (!supportedTypes.Contains(fileExt))
{
ModelState.AddModelError("document", "Wrong format");
return View();
}
//this is the line that sends damaged attachments,
//I believe I should be using document in some way (using reader bit below),
//but whatever I use the code complains or crashes.
message.Attachments.Add(new Attachment(upload.InputStream, Path.GetFileName(upload.FileName)));
using (var reader = new System.IO.BinaryReader(upload.InputStream))
{
document.Content = reader.ReadBytes(upload.ContentLength);
//message.Attachments.Add(new Attachment(document.Content, document.FileName));
}
contact.Files = new List<File> {document};
}
}else
{
ModelState.AddModelError("document", "File too big. Max 4MB.");
}
}
EDIT: A lot of times the code cannot find the file, how do I make sure I give it correct path each time?
I am having difficulty displaying images after deploying to Azure.
I am currently developing my application on ASP.NET MVC 5.
My methodology is for admin to upload a particular image, which would be accessible by users. The image is to be uploaded into a folder.
The issue is that I am able to get the image upload and displaying to work in my own localhost. However, it failed to work when I publish my app to Azure.
I have tried with many of the suggestions found in stackoverflow.
E.g. Changing the Permission settings in the folder, but up to no avail.
I have attached my code for reference. I appreciate any help! =D
Controller (file upload):
if (file != null && file.ContentLength > 0)
try
{
string path = Path.Combine(Server.MapPath("~/ExerciseImagesDepository"),
Path.GetFileName(file.FileName));
file.SaveAs(path);
ExerciseImage eI = new ExerciseImage();
eI.ExerciseID = ex.ExerciseID;
eI.ImageURL = Path.GetFileName(file.FileName);
db.ExerciseImages.Add(eI);
db.SaveChanges();
}
catch (Exception exc)
{
ViewBag.FileUploadErrorMessage = "ERROR:" + exc.Message.ToString();
}
Controller (Details display)
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
List<ExerciseViewModel> evmLIst = new List<ExerciseViewModel>();
int a = 0;
List<Exercise> exercise = db.Exercises.ToList();
foreach (var item in exercise)
{
ExerciseViewModel evm = new ExerciseViewModel();
a = a + 1;
evm.ExerciseViewModelID = a;
evm.ExerciseRegion = db.ExerciseRegions.Find(item.ExerciseRegionID);
evm.ExerciseDescription = item.Description;
evm.ExerciseType = db.ExerciseTypes.Find(item.ExerciseTypeID);
int ExerciseID = item.ExerciseID;
ExerciseVideo ev = db.ExerciseVideos.Where(m => m.ExerciseID == ExerciseID).SingleOrDefault();
evm.VideoURL = ev.VideoURL;
ExerciseImage ei = db.ExerciseImages.Where(m => m.ExerciseID == id).SingleOrDefault();
if (ei != null)
{
evm.ImageURL = ei.ImageURL;
}
else
{
evm.ImageURL = "No Image Is Available For This Exercise";
}
evm.Exercise = item.Name;
evmLIst.Add(evm);
}
View
<div>
<p> TESTING HERE #Html.Raw(Model.ImageURL) </p>
<img src="~/ExerciseImagesDepository/#Html.Raw(Model.ImageURL)"/>
Folder Structure:
I solve it! I figured that perhaps folder that are not default in the MVC 5 framework might be causing the issue. So instead, I created a new folder within the content directory and added a folder inside. Next, I tried changing that particular folder permission settings by adding a group or usernames "IUSR" and allowing full control. I then published the particular folder specifically to Azure and it works. Although I have no idea why I manage to do it.
With OS.File I am able to open a file with lock on it:
let options = {
winShare: 0 // Exclusive lock on Windows
};
if (OS.Constants.libc.O_EXLOCK) {
// Exclusive lock on *nix
options.unixFlags = OS.Constants.libc.O_EXLOCK;
}
let file = yield OS.File.open(..., options);
Is it possible to test if the path is locked though. I'm looking for alternative to nsiToolkitProfile.lockProfile
This is copy paste to scratchpad code. The top block uses nsitoolkitprofile to test if locked. And it works fine. The second part uses OS.File.open and it always throws error.
Cu.import('resource://gre/modules/osfile.jsm');
Cu.import('resource://gre/modules/FileUtils.jsm');
var tps = Cc['#mozilla.org/toolkit/profile-service;1'].createInstance(Ci.nsIToolkitProfileService); //toolkitProfileService
var folderOfProfile = 'k46wtieb.clean'; //folder names of relative profiles found here: %APPDATA%\Mozilla\Firefox\Profiles
var rootPathDefault = FileUtils.getFile('DefProfRt', []).path;
var localPathDefault = FileUtils.getFile('DefProfLRt', []).path;
var aDirect = new FileUtils.File(OS.Path.join(rootPathDefault, folderOfProfile));
var aTemp = new FileUtils.File(OS.Path.join(localPathDefault, folderOfProfile));
try {
var locker = tps.lockProfilePath(aDirect, aTemp)
Services.ww.activeWindow.alert('NOT open');
locker.unlock();
} catch (ex) {
if (ex.result == Cr.NS_ERROR_FILE_ACCESS_DENIED) {
Services.ww.activeWindow.alert('its in use');
} else {
throw ex;
}
}
var promise = OS.File.open(aDirect.path)
promise.then(
function(aVal) {
Services.ww.activeWindow.alert('promise success, aVal = ' + aVal);
aVal.close();
},
function(aReason) {
Services.ww.activeWindow.alert('promise rejected, aReason = ' + uneval(aReason));
}
)
The promise is always rejected with aReason.becauseAccessDenied every time :(
Just try to open it... If you cannot because of permissions, then the file is probably locked in another location.
I am using following code to create xml file -
void createxml(){
Document d = new Document();
Element root = d.createElement("","company");
Element employee = d.createElement("","employee");
employee.setAttribute("","id","1");
Element fname = d.createElement("","fname");
fname.addChild(Node.TEXT,"Vasudev");
Element lname = d.createElement("","lname");
lname.addChild(Node.TEXT,"Kamath");
Element address = d.createElement(Node.TEXT+"","address");
address.addChild(Node.TEXT,"Karkala");
employee.addChild(Node.ELEMENT,fname);
employee.addChild(Node.ELEMENT,lname);
employee.addChild(Node.ELEMENT,address);
root.addChild(Node.ELEMENT,employee);
d.addChild(Node.ELEMENT,root);
String fileName = "file:///SDCard/Blackberry/company.xml";
DataOutputStream os = null;
FileConnection fc = null;
try
{
fc = (FileConnection)Connector.open(fileName,Connector.READ_WRITE);
if (! fc.exists())
fc.create();
os = fc.openDataOutputStream();
KXmlSerializer serializer = new KXmlSerializer();
serializer.setOutput(os, "UTF-8");
d.write(serializer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
But when I write this method, my program is not going to be load on the simulator, if I comment It loads easily. How do i solve this problem?
Do u simulate your app?
If you running in simulator, create a filder SDCard in your system and then create a sub folder Blackberry. And when you run your app take take the 'Simulate' menu > Change Sd card> Add Directory> and browse for the folder SDcard .... then run you app
In my balckberry application i am using the Persistance,List and Threads to execute the code.
The code is given below:
enter code here
UiApplication.getUiApplication().invokeLater(new Runnable(){
public void run(){
CategoryListScreen.getInstance(
UiApplication.getUiApplication())
.setCategoryListContent();
}
});
public void setCategoryListContent() {
categoryList.delete(0);
CategoryListScreen categoryListScreen = CategoryListScreen
.getInstance(UiApplication.getUiApplication());
PersistentObject categoryListPersistObject = PersistentStore
.getPersistentObject(0x73c8d3592648fea5L);
PersistentObject datePersistObject = PersistentStore
.getPersistentObject(0xe453c1c0c14b9aebL);
categoryListPersistObject.setContents(Communicator.categoryDatas);
datePersistObject.setContents(new Date());
categoryListScreen.setCategoryListVector(new Vector());
categoryDataList = Communicator.categoryDatas;
System.out.println("-------------------- " + Communicator.categoryDatas.length);
for (int i = 0; i < Communicator.categoryDatas.length; i++) {
if (Communicator.categoryDatas[i] != null) {
if (Communicator.categoryDatas[i].getName() != null) {
categoryListScreen.getCategoryListVector().addElement(
Communicator.categoryDatas[i].getName());
}
}
}
testListCallback = new ListCallback();
categoryList.setCallback(testListCallback);
int i = categoryListScreen.getCategoryListVector().size();
categoryListScreen.getCategoryList().setSize(i);
System.out.println("---------------------->" + categoryListScreen.getCategoryListVector().size());
//
categoryListScreen.getCategoryList().setRowHeight(40);
// categoryListScreen.invalidate();
invalidate();
System.out.println("End.........................");
}
The application is Using the Threads to execute the persistance and also setting the size of the list.The application is running fine and exiting successfully.
But at the time of unistalling the application the device get being restarted and also after restarting the application ,there is no effect on the application.The application still remains there.
what is the problem in uinstalling the application which uses Threads,persistance and list?
why is it being restarted without any user confirmation or alert?
why is it not get being uninsall after restart?
please give the resolution for the given problem or any useful code snippet ,which would be appreciated.
Thanks,
Mishal Shah
Is this on a device simulator or a real device? As far as I know, if you reset the simulator, it loads back all the installed apps onto to simulator.