2011-02-01 26 views
1

私は、壁紙や着メロを保存できるマルチメディアアプリケーションを作成しています。私はそれらを保存するために必要なパスが "SDCard/BlackBerry/ringtones/file.mp3"(または壁紙の場合は "/ pictures")であることを知っています。私は数日間フォーラムと投稿を検索しましたが、唯一見つかったのはテキストファイルの書き方でした。今のところ、着信音と画像はプロジェクトのリソースフォルダに保存されているものとします。あなたがどんな入力を提供することができたら、私はそれを高く評価します。メディアファイルをBlackberry SDカードに保存

答えて

2

おかげで自分のデータです。ここでは、SDカードへのリソースのmp3ファイルを保存するためのコードは次のとおりです。

byte[] audioFile = null; 
try { 
    Class cl = Class.forName("com.mycompany.myproject.myclass"); 
    InputStream is = cl.getResourceAsStream("/" + audioClip); 
    audioFile = IOUtilities.streamToBytes(is); 

    try { 
     // Create folder if not already created 
     FileConnection fc = (FileConnection)Connector.open("file:///SDCard/BlackBerry/ringtones/"); 
     if (!fc.exists()) 
      fc.mkdir(); 
     fc.close(); 

     // Create file 
     fc = (FileConnection)Connector.open("file:///SDCard/BlackBerry/ringtones/" + audioClip, Connector.READ_WRITE); 
     if (!fc.exists()) 
      fc.create(); 
     OutputStream outStream = fc.openOutputStream(); 
     outStream.write(audioFile); 
     outStream.close(); 
     fc.close(); 

     Dialog.alert("Ringtone saved to BlackBerry SDcard."); 
    } catch (IOException ioe) { 
     Dialog.alert(ioe.toString()); 
    } 
} catch (Exception e) { 
    Dialog.alert(e.toString()); 
} 

CJPが指摘したように、ここでSDカードに画像リソースを節約するためにどのようにされて:返信用

EncodedImage encImage = EncodedImage.getEncodedImageResource(file.jpg"); 
byte[] image = encImage.getData(); 
try { 
// create folder as above (just change directory) 
// create file as above (just change directory) 
} catch(Exception e){} 
4

何も保存する必要はありません。このような何か試してみてください:

FileConnection fc; 

    try { 
     String fullFile = usedir + filename; 
     fc = (FileConnection) Connector.open(fullFile, Connector.READ_WRITE); 
     if (fc.exists()) { 
      Dialog.alert("file exists"); 
     } else { 
      fc.create(); 
      fileOS = fc.openOutputStream(); 
      fileOS.write(raw_media_bytes, raw_offset, raw_length); 
     } 
    } catch (Exception x) { 
     Dialog.alert("file save error); 
    } finally { 
     try { 
      if (fileOS != null) { 
       fileOS.close(); 
      } 
      if (fc != null) { 
       fc.close(); 
      } 
     } catch (Exception y) { 
     } 
    } 

usedirとファイル名がパスのコンポーネントであるが、raw_media_bytesはなどなど、あなたの助けのためのCJP

+0

感謝。私はその一部を理解しています。しかし、私は自分のファイルでwriteステートメントを使う方法を知らない。着メロと画像が「res」フォルダに保存されているとします。リソースメディアファイルをバイトに変換するにはどうすればよいですか(write文を使用できるようになります)。画像は.jpg、着信音は.mp3です。再度、感謝します。 – Brian

+0

イメージリソースからバイト[]を取得する方法を知りました:EncodedImage encImage = EncodedImage.getEncodedImageResource(file.jpg "); byte [] image = encImage.getData();しかし、 – Brian

+0

私はこれをやったことがわかりませんが、このURLはgetClass()です。getResourceAsStream(filename)はあなたの最善の策です: http://docs.blackberry.com/en/developers/deliverables/11942/Create_BB_app_that_plays_media_from_input_stream_739580_11.jsp – cjp

関連する問題