file path not working in J2ME - java-me

I'm using a image path in my test mobile (Samsung-Duos) for platformRequest("file:///E:/image.jpg") . it seems that the file path is invalid. I have the image in the SD card. Please anyone help me to make it work !
Thanks in advance :)

Just make sure you are passing the correct path. Using below code you will able to know which are the avalable paths you have. (Music folder is you app specific folder. you can remove it if not needed.)
private String getFullMusicPath() {
Enumeration drives = FileSystemRegistry.listRoots();
String root = null;
while (drives.hasMoreElements()) {
root = (String) drives.nextElement();
if (root.equalsIgnoreCase("sdcard/")) {
return "file:///" + root + musicFolder + "/";
}
}
return (root == null ? null : "file:///" + root + musicFolder + "/");
}

Related

Uploading PDF file to Google Drive folder and overwriting existing file

I am developing an app that uploads PDF files to a specific Google Drive folder. The file name includes the current date. The following code is for my DriveServiceHelper.class that is used to create a folder in Google Drive and then upload the PDF files into that folder using its folderID:
public class DriveServiceHelper {
Calendar c = Calendar.getInstance();
Date d = c.getTime();
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = df.format(d);
String ps_FolderKey;
private final Executor mExecutor = Executors.newSingleThreadExecutor();
private Drive mDriveService;
public DriveServiceHelper(Drive mDriveService) {
this.mDriveService = mDriveService;
}
public Task<String> createFolder() {
return Tasks.call(mExecutor, () -> {
File folderMetadata = new File();
folderMetadata.setName("Covid Assessment Sheets");
folderMetadata.setMimeType("application/vnd.google-apps.folder");
File myFolder = null;
try {
myFolder = mDriveService.files().create(folderMetadata)
.setFields("id")
.execute();
System.out.println("Folder ID: " + myFolder.getId());
} catch (Exception e) {
e.printStackTrace();
}
if (myFolder == null) {
throw new IOException("Null result when requesting file creation");
}
ps_FolderKey = myFolder.getId();
return ps_FolderKey;
});
}
public Task<String> createFilePDF(String filePath, String folderId) {
return Tasks.call(mExecutor, () -> {
File fileMetaData = new File();
fileMetaData.setName("Covid Assessment # " + currentDate);
fileMetaData.setParents(Collections.singletonList(folderId));
java.io.File file = new java.io.File(filePath);
FileContent mediaContent = new FileContent("application/pdf", file);
File myFile = null;
try {
myFile = mDriveService.files().create(fileMetaData, mediaContent).execute();
} catch (Exception e) {
e.printStackTrace();
}
if (myFile == null) {
throw new IOException("Null result when requesting file creation");
}
return myFile.getId();
});
}
}
When uploading the same PDF to a Google Drive folder, I want to overwrite files with the same name, but instead duplicate files are created in the folder as the fileID assigned is different even if file name is the same.
Please help me understand how I should go about this, to automatically overwrite/replace files that already exist with the same name (each file is differentiated by date), and a new PDF file is created if the PDF file does not exist in the folder.
I understand that I might be using the deprecated Drive API, but I was unable to find other solutions online to help me implement what I need. I also came across solutions that include queries to search for existing Google Drive files, but I am not sure I understand how to use it to make it work for me.
Thank you
Google Drive supports multiple files with the same name
Thus, by creating a file with an already existing name, you will not automatically overwrite the old file.
Instead you should do the following:
Use the method Files:list with the query name = 'Covid Assessment Sheets' to find the already existing file(s) with the same name
If desired, you can narrow down the results by also specifying the mimeType and the parent folder (parents)
Retrieve the id of the list result(s)
Use the method Files:delete to delete the existing file
Proceed to create a new file as you are already doing
In Java this would look as following:
FileList result = DriveService.files().list()
.setQ("name = 'Covid Assessment Sheets'");
.setFields("files(id)")
.execute();
List<File> files = result.getFiles();
for (File file : files) {
DriveService.files().delete(file.getId()).execute();
}
An alternative approach would be to update the contents of the already existing file instead of creating a new one.

Hybris prettyURL showing PK instead of the real file name

The file-name of any image is appearing like
/de-de/medias/sys_master/images/images/h9c/h5f/8796178743326/8796178743326.jpg in the url.
Instead of 8796178743326.jpg there should be file-name.jpg
I have already set media.legacy.prettyURL=true
8796178743326 is the PK of the image.
Any help!
With the prettyURL, if there is no realfilename value in media instance then URL will end with PK instead real file name.
/medias/sys_master/images/images/h9c/h5f/8796178743326/8796178743326.jpg
If you really want the file name in the URL then you have to edit respective media from the backoffice/impex and assign value to the realFileName attribute.
Have a look into assembleLegacyURL method of LocalMediaWebURLStrategy class
String realFileName = this.getRealFileNameForMedia(mediaSource);
if (realFileName == null) {
basePath = mediaSource.getLocation().substring(0, lastDotIdx);
lastDotIndexForRealFileName = StringUtils.lastIndexOf(basePath, '/');
String fileName = basePath.substring(lastDotIndexForRealFileName + 1);
sb.append(basePath).append("/").append(fileName).append('.').append(fileExtension);
} else {
basePath = location.substring(0, lastDotIdx);
lastDotIndexForRealFileName = realFileName.lastIndexOf(46);
if (lastDotIndexForRealFileName != -1) {
realFileName = realFileName.substring(0, lastDotIndexForRealFileName);
}
sb.append(basePath).append("/").append(realFileName).append('.').append(fileExtension);
}

String.Replace for Path and Url

I have a problem about path string data. I have a string columb in Database and keep path in this columb.Like this;
D:\SCANNEDDOCUMENTS\2\3C9DED0628F44190ABF92D3C14F14A14-2-D.pdf
In my project i'll reach documents on IIS.I config all settings for IIS. And I need an Url like this;
HTTP://LOCALHOST/SCANNEDDOCUMENTS/2/3C9DED0628F44190ABF92D3C14F14A14-2-D.pdf
And i don't want to change paths in database.They're original local path.Only i need change on viewmodel. So need replace first path to second Url. And when i get path value from database i saw that:
D:\\SCANNEDDOCUMENTS\\2\\3C9DED0628F44190ABF92D3C14F14A14-2-D.pdf
There is double backslash? Why? And how can replace this like my wanted Url?
I tried several thing like;
private static string ReplaceDocumentPathLocalToWeb(string path)
{
if (!string.IsNullOrEmpty(path))
{
SArchiveModule archivingModule = ContainerManager.Container.GetInstance<SArchiveModule>();
SbtGeneralParameterModel parameter = archivingModule.GetGeneralParameters();
string docDirectory = parameter.DocumentDirectoryPath;
string docWebDirectory = parameter.DocumentWebRootPath + #"/" + parameter.DocumentWebFolderPath;
path.Replace(docDirectory, docWebDirectory);
path.Replace("\"", "/");
if (path.StartsWith(#"/"))
{
path.TrimStart(new char[] { '/' });
}
if (path.EndsWith(#"/"))
{
path.TrimEnd(new char[] { '/' });
}
}
return path;
}
path="D:\SCANNEDDOCUMENTS\2\3C9DED0628F44190ABF92D3C14F14A14-2-D.pdf";
parameters from database too;
docDirectory="D:\SCANNEDDOCUMENTS"
docWebDirectory="HTTP://LOCALHOST/SCANNEDDOCUMENTS"
But this code is not working too:
path.Replace(docDirectory, docWebDirectory);
What should i do for replace? And why my data came with double backslash? In database it's with one backslash?
So sorry my wrong. Now i got the problem. I couldn't see the string.replace result 'cause i need use code like:
path=path.Replace(docDirectory, docWebDirectory);
path=path.Replace("\"", "/");
if (path.StartsWith(#"/"))
{
path=path.TrimStart(new char[] { '/' });
}
if (path.EndsWith(#"/"))
{
path=enter code here`path.TrimEnd(new char[] { '/' });
}
It's really silly mistake...

How to play video in video player using platformRequest(URL) in J2ME

In my app I'm trying to play a video from SD card using platformRequest(URL). Here is the code i used to do it - platformRequest("file:///E:/bega.3gp") . I don't know this is the correct way to do it. Is the path format is correct ? please suggest me a way to play a video in video player from my app.
Just make sure you are passing the correct path. Using below code you will able to know which are the avalable paths you have.
private String getFullMusicPath() {
Enumeration drives = FileSystemRegistry.listRoots();
String root = null;
while (drives.hasMoreElements()) {
root = (String) drives.nextElement();
if (root.equalsIgnoreCase("sdcard/")) {
return "file:///" + root + musicFolder + "/";
}
}
return (root == null ? null : "file:///" + root + musicFolder + "/");
}

downloading files as a corrupted files in kohana

hi guys i'm facing problem with file upload and download in kohana
my controller is like this:
class Controller_Test extends Controller
{
public function action_display()
{
$type = $_FILES['file']['type'];
switch ($type)
{
case 'image/gif':
$otype='.gif'; break;
case 'image/jpeg':
case 'image/pjpeg':
$otype= '.jpg'; break;
case 'image/png':
$otype= '.png'; break;
case 'application/octet-stream':
$otype='.doc'; break;
case 'txt': $otype='.txt'; break;
case 'application/pdf': $otype='.pdf'; break;
}
//rename the file
$name = time() . '_' . mt_rand(1000,9999).$otype;
$directory = $_SERVER['DOCUMENT_ROOT'].URL::base().'media';
//uploading a file
$filename = Upload::save($_FILES['file'], $name, $directory);
$this->auto_render = false;
$this->response->send_file($filename);
}//action
}//controller
when i call this function file uploaded fine
but downloading file as a corrupted file
help me how to solve this..
thanks in advance.
You shouldn't add URL::base() inside the path name as that could add something like "http://..." inside the file path. Try removing URL::base() and try again.
To start, there's some simple debug checks you can do here.
Is $directory valid?
is $filename a valid file path, or is it FALSE? (See http://kohanaframework.org/3.2/guide/api/Upload#save)
I'm going to assume $directory is invalid.
You want to use the absolute path constants to build directory paths. Instead of using $_SERVER['DOCUMENT_ROOT'].URL::base() (which is wrong in any case)
Rather use APPPATH or DOCROOT, eg $directory = APPPATH.'media'; see https://github.com/kohana/kohana/blob/3.2/master/index.php#L57-74

Resources