Android (Eclipe) Video View File name path? - java

I'm here with what I think to be an easy doubt, but it's blowing my mind >.<
I'm pretty newbie to Java, and I wanted to make an app for android, but I'm stuck with the video player's coding.
I'm using this code, which I found on the internet (I think it was in stackoverflow, but not sure)
VideoView vv = (VideoView)this.findViewById(R.id.videoView1);
String fileName = "android.resource://" + getPackageName() + "/" + R.raw.reveal;
vv.setVideoURI(Uri.parse(fileName));
vv.start();
what the heck I put on the filename, where it has "android.resource://"
I want to play a video when I open a certain activity. My video is located in the drawable-ldpi's folder.
The video's file name is reveal.wmv
Can you guys help me solving it?
Best regards :D
Sorry about my english :/

You are thinking about it a little wrong. I know that in most tutorials they use "filename" as somewhere that you should insert the name, but in this case it is not so. The line
String fileName = "android.resource://" + getPackageName() + "/" + R.raw.reveal;
is setting the file path and filename of the video. In this case, it is setting a path to the raw folder and to the video "reveal" If you have not already, you should create the raw folder and place it in the res folder. That is where you will store all video and sound media.
In the next line:
vv.setVideoURI(Uri.parse(fileName));
the "filename" is substituting for the enter path, so leave that as it is. In the previous line you named the String that is pointing to the video, "filename" It could also have been programmed as so:
String elmo = "android.resource://" + getPackageName() + "/" + R.raw.reveal;
vv.setVideoURI(Uri.parse(elmo));
I have seen that it is best to use mp4 format files. I know that is just the extension and I am not sure about the actual codec, but that should lead you in a direction. Notice that the extension is not used in the file name itself. Android will simply search for the name and load the file regardless. If it is the wrong format or acceptable extension, it simply will not play.
Hope that helps.

The drawables folder is not for videos. Put it in res/raw/ (you may need to create). You will need to change the resource name (R.raw.yourfilename), but other than that, your example looks correct.

The way you're trying to declare the PATH is wrong ... first you have to put your video file in /asset folder and then use getAssests() method .. it 'll work.

Just move reveal.wmv from drawable-ldpi folder to raw folder, then it will work well. If you don't have raw folder, create it under res folder.

Related

How do I specify file path for Android's getExternalStorageState()?

I'm completely new to Android development and would like to index a file from the Android phone's external storage. I found some old code which used a deprecated method:
String file1 = Environment.getExternalStorageDirectory() + "/folder/file.blah";
I'm trying to rewrite this line and tried the Environment.getExternalFilesdir method but got a "cannot find symbol method" error. The only available method seems to be getExternalStorageState, but I can't figure out how to pass the file path argument in. How do I rewrite the deprecated code, using getExternalStorageState or otherwise? Thanks.
For getting directory of public file. you can use like below
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOCUMENTS);
docFile = new File(storageDir, docFileName);
Actually getExternalFilesDir() is not a part of Environment bundle. You can call it via the application context.
String file2 = getApplicationContext().getExternalFilesDir("folder")
The difference between these two are as follows
getExternalFilesDir():
This will return the path to files folder inside Android/data/data/your_package_name/. It is used to store any required files for your app.
getExternalStorageDirectory():
This returns the root path of you external SD card.
As far as the deprecation warning is concerned, Environment.getExternalStorageDirectory() or Environment.getExternalStoragePublicDirectory(), are deprecated. They still work, but will be removed soon, so you need to stop using those.

open music file from raw folder to play on default app of the device using an intent on android

I'm trying to write an intent to open the default music player of the device to play a music file (mp3) I have on raw folder in my android studio project . thus far I found the code for the intent but I got stack on how to order it to open the specific mp3 file an dplay it on the media player so far I have wrote something like this trying to find te uri also of the file but I cant put them together on how should I do it the file name of the mp3 is "greeceMusic" if it helps at all. the code I have so far is
`#Override
public void onClick(View view) {
Intent musicIntent = new Intent();
//use Action VIEW to launch app
musicIntent.setAction(Intent.ACTION_VIEW);
File file = new File("android.resource://" + getPackageName() + "/" + R.raw.greeceMusic);
musicIntent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(musicIntent);
}`
any help would be gratly appreciated as for I'm really new at this and I need all the help I can get cause when I found a similar post in the forum it don't help me a lot cause I couldn't put together the instructions thanks in advance
File file = new File("android.resource://" + getPackageName() + "/" + R.raw.greeceMusic);
First, a resource is a file on your development machine. It is not a file on the device.
Second, you cannot use a Uri syntax with a scheme, like android.resource://, with File.
Third, do not use wildcard MIME types when creating an Intent like this. You know what the actual MIME type is, as you are the one packaging the media into your app. So, use the actual MIME type.
Fourth, even if you correctly created the Uri (e.g., Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.greeceMusic)), it is unlikely that many devices will have an app capable of playing a music file published in the form of a raw resource.
Your best option is to copy the resource contents to a file (e.g., openRawResource() on Resources, then copy the bytes from the InputStream to a FileOutputStream), then try to play the file (e.g., served via a FileProvider).

Unable to locate/open image inside exported jar

I have explored every single solution related to this problem on Stack Overflow, and the solutions I encountered worked only in my IDE (Eclipse Mars), but when exported they all failed.
I have an image file I simply want to load from inside my exported jar.
As you can see from the project layout, Images is a folder directly inside of the project folder CooldownTrackerJava. To refer to it, I've tried a variety of methods.
//My class' name is AddItemDialog
//Method 1
String filePath = File.separator + "Images" + File.separator + "questionMark.png";
InputStream inputStream = AddItemDialog.class.getResourceAsStream(filePath);
BufferedImage img = ImageIO.read(inputStream);
I tried method 1 in a few ways. I tried it with and without the File.separator at the start of the filePath. I understand that the former refers to an absolute path, while the latter is relative. I also tried moving the Images folder inside of the directory where my class files were.
All of these failed giving me a NullPointerException when exported and tested multiple times.
//Method 2
String saveLocation = File.separator + "Images" + File.separator + "questionMark.png";
Image img = Toolkit.getDefaultToolkit().getImage(getClass().getResource(saveLocation));
As per this post I tried the above method however it still did not work.
I've tried many variations aside from the ones mentioned in this post but none of them have been successful when I exported the jar.
Do I need to add the Images folder to my build path? Should I move it to the src folder? Am I missing one obvious step? All suggestions would be highly appreciated. Also here is the layout of the exported jar file.
The solution was a two-part effort. Firstly, I moved my Images folder into the src directory as shown below. Kudos to Matthew.
I then removed File.separator and replaced it with a forward slash/. Thanks to fge.
Do not use File.separator(). Separators for resource paths on the class path is always /. If you use Windows you will therefore always get the wrong result.
I used a variant of method 1 to test it. Both methods will work though.
String saveLocation = "/Images/questionMark.png";
InputStream inputStream = this.getClass().getResourceAsStream(saveLocation);
try {
BufferedImage img = ImageIO.read(inputStream);
} catch (IOException e) {
e.printStackTrace();
}

Java: How to use AbsolutePath as a Link

I´m writing some Java and one of my task is to convert the Input/Output in to html. Well everything is going good, I can read some files and get the path of those files. Now I´m trying to convert the path in to a normal link so:
"<a href=" + file.getAbsolutePath() + " target=_parent>"
Works pretty good, but:
One, the explorer show an error that it can´t read the file, example:
a jpg or a word file..
Two, if the path have a blank like: "my picture.jpg"
recognize everything after the blank as a normal text...
can someone give me a Tipp, How to fix that, or I just using the wrong method?
You can use toURI() method that turns the file path into a URI:
file.getAbsoluteFile().toURI().toString()

Java retrieving file with /*/ in the path

i am aware i am asking somethink that cannot be done without manually looping over the file system. But maybe someone have a better idea then mine.
i have a list of users and just one of them has in his own folder the file aaa.xml
from the linux shell of course if i type
vi /user/*/aaa.xml
i can open the file. I would like to use the same future in java but it seams not to Work
File designFile = new File("/user/*/aaa.xml");
solution would be to try to locate the file in each of the users directory but it seams not to nice. do you guys have a better idea??
cheers,
Ste
This might be related to what you are looking for. Hope it helps.
A FileSet package/class wanted for Java
Edit:
Then how about this?
How to find files that match a wildcard string in Java?
well.. seams not feasible because cannot work over different Operating systems.
String absolutePath = temp.getAbsolutePath();
System.out.println("File path : " + absolutePath);
String filePath = absolutePath.
substring(0,absolutePath.lastIndexOf(File.separator));
System.out.println("File path : " + filePath);

Categories

Resources