1

I want to implement a behavior similar to Whatsapp, where when the user can upload an image. I tried opening the images in my app, but if the image is too large, I will have an out of memory error.

To solve this, I'm opening forwarding the images to be open in the phone's native image viewer using the platformRequest() method.

However, I want to know how is it Whatsapp modifies the phone's native image viewer to add a "Select" button, with which the user selects the image he wants to upload. How is that information sent back to the J2ME application and how is the image resized?


Edit: I tried this in two different ways, both of which gave me the OOME.

At first, I tried the more direct method:

FileConnection fc = (FileConnection) Connector.open("file://localhost/" + currDirName + fileName);
if (!fc.exists()) {
    throw new IOException("File does not exists");
}
InputStream fis = fc.openInputStream();
Image im = Image.createImage(fis);
fis.close();

When that didn't work, I tried a more "manual" approach, but that gave me an error as well.

FileConnection fc = (FileConnection) Connector.open("file://localhost/" + currDirName + fileName);
if (!fc.exists()) {
    throw new IOException("File does not exists");
}
InputStream fis = fc.openInputStream();

ByteArrayOutputStream file = new ByteArrayOutputStream();
int c;

byte[] data = new byte[1024];

while ((c = fis.read(data)) != -1) {
    file.write(data, 0, c);

}

byte[] fileData = null;
fileData = file.toByteArray();

fis.close();
fc.close();
file.close();
Image im = Image.createImage(fileData, 0, fileData.length);

When I call the createImage method, the out of memory error occurs in both cases. This varies with the devices. An E72 gives me the error with 3MB images, while a newer device will give me the error with images larger than 10MBs.

4

1 に答える 1

1

MIDP 2 (JSR 118) にはそのためのAPIがありません。大きな画像を処理するには別の方法を見つける必要があります。

WhatsAppに関しては、この機能をサポートする際にMIDPに依存していないようです. ウィキペディアのページを確認すると、サポートされているプラ​​ットフォームとして一般的な Java ME を主張していないことがわかりますが、代わりに、Symbian、S40、Blackberry などのより狭いプラットフォームをリストしています。

これは、特定のターゲット デバイスのプラットフォーム固有の API の使用について質問しているような「問題のある機能」を実装し、リストされているプラ​​ットフォームごとに本質的に個別のプロジェクト/リリースを持っていることを意味する可能性が最も高いです。

この機能がアプリケーションで本当に必要な場合は、おそらく次のようなことを行う必要があります。

この場合、別のプラットフォーム用にビルドするときにソース コードの一部だけを簡単に切り替えられるように、問題のある機能をカプセル化することも検討してください。たとえば、Class.forName(String)を使用して、ターゲット プラットフォームに応じてプラットフォーム固有の実装をロードできます。

//...
Image getImage(String resourceName) {
   // ImageUtil is an interface with method getImage
   ImageUtil imageUtil = (ImageUtil) Class.forName(
           // get platform-specific implementation, eg
           //   "mypackage.platformspecific.s40.S40ImageUtil"
           //   "mypackage.platformspecific.bb.BBImageUtil"
           //   "mypackage.platformspecific.symbian.SymbialImageUtil"
           "mypackage.platformspecific.s40.S40ImageUtil");
   return imageUtil.getImage(resourceName);
}
//...
于 2012-09-29T13:24:55.563 に答える