Pass local video to mediametadataretriever throws IllegalArgumentException - android

In my android app I want to extract video frames. I am using MediaMetaDataRetriever for the same.
How I set datasource
Log.d("DEBUG", videoPathUri.getPath());
metadataRetriever.setDataSource(mContext, videoPathUri);
Here is the log output
/storage/emulated/0/Android/data/com.live.hootout/files/HootVideos/10701.mp4
How can I load video stored in android data folder into mediametadataretriever?

Try this...
File file = new File(context.getDataDir(),filename);

Here is how I did it.
File file = new File(videoPathUri.getPath());
try {
FileInputStream inputStream = new FileInputStream(file.getAbsolutePath());
metadataRetriever.setDataSource(inputStream.getFD());
}catch(FileNotFoundException e){
Log.d("DEBUG", "FileNotFoundException", e);
}catch(IOException ea){
Log.d("DEBUG", "IOException", ea);
}

Related

Is there any way to copy byte by byte from a file and play it using videoView in android?

I have tried this but it's not working. It copies all the bytes at once and play it. But i want to copy byte by byte from a file and play this video during/while receiving...
Some Code
tempFile = new File(Environment.getExternalStorageDirectory()+ "/" +"temp.mp4");
int length=0;
byte[] vidBytes = new byte[(int) file.length()];
try {
//while (length<100)
//{
fileInputStream = new FileInputStream(file);
fileInputStream.read(vidBytes,0,vidBytes.length);
fileInputStream.close();
videoPlayer.setVideoPath(tempFile.getAbsolutePath());
Toast.makeText(this,tempFile.getPath(),Toast.LENGTH_LONG).show();
videoPlayer.start();
length++;
//}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
fileOutputStream.write(vidBytes,0,50,vidBytes.length);
fileOutputStream.close();
what the hell are you doing? the fileInputStream is practically useless ! you read the bytes into the byte array vidBytes, but never actually use it anywhere, videoView doesn't require you to open an input stream, all videoView requires is the path to the file and the videoView code takes care of opening the stream and loading the data efficiently.
Also why are you writing the same Byte array that you read just few statements ago not modify it and write it back to the SAME FILE!? What are you trying to accomplish here?
P.S This is all you need to do to play the mp4 file in the videoview
tempFile = new File(Environment.getExternalStorageDirectory()+ "/" +"temp.mp4");
videoPlayer.setVideoPath(tempFile.getAbsolutePath());
Toast.makeText(this,tempFile.getPath(),Toast.LENGTH_LONG).show();
videoPlayer.start();

Save file object to internal storage as image

I am downloading image via a URL using loopj library https://github.com/loopj/android-async-http. In response I get image as File object. I want to store this image as .jpg to my internal storage and get the path of that image. Can anyone please help me regarding this? Or suggest me any other library or the code through which I can achieve this functionality?
Try :
try {
File imgFile = null; //File you received from loopj
FileInputStream fis = new FileInputStream(imgFile);
FileOutputStream fos = new FileOutputStream(
new File("yourPath.jpg"));
byte fileContent[] = new byte[(int) imgFile.length()];
fis.read(fileContent);
fos.write(fileContent);
fis.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}

it works fine on desktop, but not on android. why?

saving socres to highscore.sav file, it works fine on desktop, but not on android. why?
String fileName = "highScores.sav";
file = new File(fileName);
public static void save(){
try{
FileOutputStream fileOut = new FileOutputStream(file);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(gd);
out.close();
}
catch(Exception e){
e.printStackTrace();
System.out.println(e);
Gdx.app.exit();
}
}
public static void load(){
try{
if(!saveFileExists()){
init();
return;
}
FileInputStream fileIn = new FileInputStream(file);
ObjectInputStream in = new ObjectInputStream(fileIn);
gd = (GameData) in.readObject();
in.close();
}
catch(Exception e){
e.printStackTrace();
System.out.println(e);
Gdx.app.exit();
}
}
got error: java.io.FileNotFoundException: /highScores.sav: open failed: EROFS (Read-only file system)
This isn't working because you have not specified a directory to save into. Android has tight restrictions on where an app can write files.
You don't need any permissions to read or write a file to internal memory. But you do need to specify internal memory (called local memory in libgdx).
Libgdx already handles this directly for you so you don't need to differentiate between desktop and Android. This explains exactly how to do it. All you need is the string or bytes you want to write into the file, and the libgdx API's handle the rest.
FileHandle file = Gdx.files.local(filename);
file.writeString(stringToWrite, false);
If you want to continue using your method of writing the file, you can get the path to the file like this:
String fileName = "highScores.sav";
file = new File(Gdx.files.getLocalStoragePath () + "/" + fileName);
Have you added the permission to the android app to allow writing to the storage space?

encode video file at specific time of the video

i have video file in the sdcard. i want to encode the video file at minute 3 to 5. what im trying to say is,
let say, the complete video file is 10 minute. now i need to retrieve the video data at 3 to 5 minutes of the video to be encode and do the next process.
i have done it before but only encoded the complete video file and i dont have any idea to encoded the video file at specific time of the video. please help with this. Below is my encoded complete video file code:
public void loadVideo(){
//convert whole file into byte
File file = new File("/sdcard/videooutput.mp4");
byte[] fileData = new byte[(int) file.length()];
FileInputStream in;
try {
in = new FileInputStream(file);
try {
in.read(fileData);
for(int readNum; (readNum = in.read(fileData)) != -1;){
}
} catch (IOException e) {
Toast.makeText(this, "IO exc READ file", 2500).show();
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
Toast.makeText(this, "IO exc CLOSE file", 2500).show();
e.printStackTrace();
}
String encoded = Base64.encodeToString(fileData, Base64.DEFAULT);
hantar(encoded);
} catch (FileNotFoundException e) {
Toast.makeText(this, "FILE NOT FOUND", 2500).show();
e.printStackTrace();
}
Please guide me with this. Thanks for advance.
You can try to use this class. It crops video from start to end position and saves cropped video to the file in your file system. Or you can use it to implement your own solution.
There is already answered question about cutting or trimming video on Android:
Android sdk cut/trim video file
You can try INDE Media for mobile - https://software.intel.com/en-us/articles/intel-inde-media-pack-for-android-tutorials
It has transcoding\remuxing functionality as MediaComposer class and a possibility to select segments for resulted files

Android FTP download exception (file not found)

I am trying to retrieve a file via FTP but I am getting the following error in LogCat:
java.io.FileNotFoundException :/config.txt (read-only file system)
I have verified that the file exists on the server, and I can read it by double clicking it in a web browser.
Can anyone help please? Here is the code I am using:
FTPClient client = new FTPClient();
FileOutputStream fos = null;
try {
client.connect("xxx.xxx.xxx.xxx");
client.enterLocalPassiveMode();
client.login("user", "pass");
//
// The remote file to be downloaded.
//
String filename = "config.txt";
fos = new FileOutputStream(filename);
//
// Download file from FTP server
//
client.retrieveFile("/" + filename, fos);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
You cannot save files on the root of the phone. Use Environment.getExternalStorageDirectory()to get an file object of the SD card's directory and save the file there. Maybe create a directory for it. To do that you need the permission android.permission.WRITE_EXTERNAL_STORAGE.
Sample code:
try {
File external = Environment.getExternalStorageDirectory();
String pathTofile = external.getAbsolutePath() + "/config.txt";
FileOutputStream file = new FileOutputStream(pathTofile);
} catch (Exception e) {
e.printStackTrace();
}
You are trying to read the file in, but you have created an OutputStream, you need to create an inputStream and then read the file from that input stream.
Here is a great article, with come code that is very helpful. This should get you headed in the right direction.
http://www.roseindia.net/java/beginners/java-read-file-line-by-line.shtml
I hope this helps!
Best of luck

Categories

Resources