Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
Posts: 7
Registered: ‎12-01-2010
My Device: BlackBerry Curve 8310

dynamic array from src folder?

Hi all,

 

I'm trying to create an array of 3gp videos that load from the COD file.  I can get one video to play just fine using:

 

 

import javax.microedition.media.Player;
import java.io.InputStream;

//find the file in the src folder and play it
InputStream stream = (InputStream)this.getClass().getResourceAsStream("/abc.3gp");
player = Manager.createPlayer(stream, "video/3gp");
player.start();

 

 

So making one change to the InlineVideoScreen example seemed logical:

 

 

/**
 * Copyright © 1998-2009 Research In Motion Ltd.
 *
 * Note:
 *
 * 1. For the sake of simplicity, this sample application may not leverage
 * resource bundles and resource strings.  However, it is STRONGLY recommended
 * that application developers make use of the localization features available
 * within the BlackBerry development platform to ensure a seamless application
 * experience across a variety of languages and geographies.  For more information
 * on localizing your application, please refer to the BlackBerry Java Development
 * Environment Development Guide associated with this release.
 *
 * 2. The sample serves as a demonstration of principles and is not intended for a
 * full featured application. It makes no guarantees for completeness and is left to
 * the user to use it as sample ONLY.
 */

package net.rim.device.inlineVideo.ui;

import java.util.Enumeration;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import net.rim.device.inlineVideo.model.Video;
import net.rim.device.inlineVideo.ui.component.InlineVideoManager;
import net.rim.device.api.io.FileInfo;
import net.rim.device.api.io.file.ExtendedFileConnection;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.util.Arrays;

/**
 * Fetch video information from a folder
 */
public class InlineVideoScreen extends MainScreen{
    
    public InlineVideoScreen(String url)
	{
		setTitle("InLine Video as List Demo");
		try
		{	
			// add the elements in
			Video[] videos = loadDownloadedFiles("/");//***HERE IS THE CHANGE***

			if (videos != null && videos.length > 0)
			{
				/* This is the alternate way of showing a list of videos */
				for (int i = 0; i < videos.length; i++)
				{
					InlineVideoManager manager = new InlineVideoManager(
							videos[i], 120, 90);
					add(manager);
					add(new SeparatorField());
				}
			}
		}
		catch (Throwable t)
		{
		}
	}
    
    private Video[] loadDownloadedFiles( String directoryUrl) throws IOException
    {
        Video[] videos = new Video[0];
        FileConnection conn = null;
        try {
            conn = (FileConnection) Connector.open( getEncodedFileUrl( directoryUrl ) );
            
            if( conn != null && conn.isDirectory() ) {
                if( conn instanceof ExtendedFileConnection ) {
                    Enumeration itemList = ( (ExtendedFileConnection) conn ).listWithDetails( "*", false );
                    
                    int index = 0;
                    
                    while( itemList.hasMoreElements() ) 
                    {
                        FileInfo fileInfo = (FileInfo) itemList.nextElement();
                        String itemPath = directoryUrl + fileInfo.getFileName();
                        Video videoFile = getVideoFile( itemPath, fileInfo );
                        
                        if( videoFile != null ) {
                            Arrays.add(videos,videoFile);                          
                            
                        }
                    }
                }
            }
            return videos;
        } catch( Throwable ignore ) {
            throw new IOException( "failure of loading files." );
        } finally {
            if( conn != null ) {
                try {
                    conn.close();
                } catch( Throwable ignore ) {
                }
            }
        }
    }
    
    public Video getVideoFile( String fileUrl, FileInfo fileInfo)
    {
		Video videoFile = null;
		try
		{
			videoFile = new Video();
			videoFile.setURL(fileUrl);
			videoFile.setTitle(fileInfo.getFileName());
			videoFile.setFileSize(fileInfo.getFileSize());
		}
		catch (Throwable ignore)
		{
		}
		return videoFile;
	}
    
    private static String getEncodedFileUrl( String fileUrl )
    {
        String encodedFileUrl = "";
        
        String filePath = getPathUrlFromFileUrl(fileUrl);
        filePath = encode( null, filePath,true);
        encodedFileUrl = "" + filePath;//***TOOK OUT REF TO SDCARD***
        
        return encodedFileUrl;
    }

    public static String getPathUrlFromFileUrl( String fileUrl )
    {
        int index = fileUrl.indexOf('/');
        
        index = fileUrl.indexOf('/', index + 1 );
        index = fileUrl.indexOf('/', index + 1 );
        index = fileUrl.indexOf('/', index + 1 );
        
        String path = fileUrl.substring(index);
        
        return path;
    }
    
    public static String encode (StringBuffer outputStringBuffer, String str, boolean encodeBlanks){
        int length = str.length();
        
        if( length == 0 ) {
            return str;
        }
        
        outputStringBuffer = new StringBuffer();
                      
        for (int i = 0; i < length; i++) {
            char character = str.charAt( i );
            
            switch (character) {
            
                // &nbsp;
                case ' ':
                if (encodeBlanks) {
                    outputStringBuffer.append("%20");
                } else {
                    outputStringBuffer.append('+');
                }
                    break;
          
                // Line breaks
                case 0x0A:
                    if (i > 0 && str.charAt( i - 1 ) == 0x0D) {
                        outputStringBuffer.append( "%0A" );
                    } else {
                        outputStringBuffer.append( "%0D%0A" );
                    }
                    break;               
                    
                default :
                    // Alphanumeric characters
                    outputStringBuffer.append((char)character);
              
                } // switch (character)
    
            } // for    
             
        return outputStringBuffer.toString();
    }
} 

 

But gives me a..."Error 104:  StringIndexOutOfBoundsException."

 

Perhaps there is a way to this.getClass().getResourceAsStream("/"); the src folder rather than just a file and pass the folder to the array that way.  Something like this but I don't know the term to get the Resource folder.

 

import net.rim.device.inlineVideo.ui.InlineVideoScreen;
import net.rim.device.api.ui.UiApplication;
import java.io.InputStream;
/**
 * 
 */
class InlineVideoApp extends UiApplication{
    InlineVideoScreen _mainScreen;
    
    InlineVideoApp() {
        InputStream folder = (InputStream)this.getClass().getResourceAsStream("/");
        _mainScreen = new InlineVideoScreen(folder);
        pushScreen(_mainScreen);
    }
        
    public static void main( String[] args )
    {
        InlineVideoApp theApp = new InlineVideoApp();
        // To make the application enter the event thread and start processing
        // messages, we invoke the enterEventDispatcher method
        theApp.enterEventDispatcher();
    }
}

 

 

Then change the 2 lines in InlineVideoScreen class back to the example:  (the lines with the ***NOTE*** above)

 

Video[] videos = loadDownloadedFiles(url);

 

and change this line to:

 

encodedFileUrl = "url" + filePath;

 

 

 

Am I crazy?  Thoughts?  Input much appreciated thanks!

maconbot