Androidphonegap应用程序:无法使用我自己的代码(不是phonegap的)检索相机拍摄的图像

发布于 2024-12-27 18:39:53 字数 3111 浏览 2 评论 0原文

注意-即使我使用phonegap,问题也不在于其中的某些问题。

你好,我正在开发一个 Android 应用程序。我的应用程序使用的是phonegap 1.3。

我的问题...
我使用phonegap api 拍照并将其显示在我的应用程序中。但发生的事情是由于某种原因,操作系统在相机启动后杀死了我的应用程序,因此在单击照片后,我的应用程序重新启动,并且它没有获取有关所拍摄照片的信息。
作为此问题的解决方法(问题< /a>),我设计了一个phonegap插件,它在应用程序启动时检查应用程序在拍照时是否崩溃(代码中的一些标志),如果它在崩溃后重新启动,它会检索相机拍摄的Pic.jpg并尝试来显示它。 问题是它无法获取正确的图像,甚至无法获取正确的 .jpg 文件。

mi 代码...< br> Phonegap 发出启动相机的意图,并将其创建的 Pic.jpg 的 uri 传递到该意图中,然后再传递给 startActivityForResult。

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = new File(DirectoryManager.getTempDirectoryPath(ctx),  "Pic.jpg");
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
this.imageUri = Uri.fromFile(photo);
this.ctx.startActivityForResult((Plugin) this, intent, 0);

注意-上述代码来自文件 Phonegap 的 CameraLauncher.java
现在,我假设 startActivityForResult 将用户捕获的图片存储在上面代码中由“photo”创建的文件中。因此,即使在相机打开时操作系统关闭应用程序(请参阅 - 问题)照片将存储在那里。如果这个假设是错误的,或者照片可能保存在其他地方,请纠正我。
因此,考虑到这个假设,我编写了一个插件,它使用 Phonegap 在 CameraLauncher.java


Uri imageUri;
ExifHelper exif = new ExifHelper();
exif.createInFile(getTempDirectoryPath(ctx) + "/Pic.jpg");
exif.readExifData();
File photo = new File(getTempDirectoryPath(ctx),  "Pic.jpg");
imageUri = Uri.fromFile(photo);

Bitmap bitmap = null;

bitmap = android.provider.MediaStore.Images.Media.getBitmap(this.ctx.getContentResolver(), imageUri);


ContentValues values = new ContentValues();
values.put(android.provider.MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
Uri uri = null;
try {
     uri = this.ctx.getContentResolver().insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} catch (UnsupportedOperationException e) {
     uri = this.ctx.getContentResolver().insert(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, values);
}

// Add compressed version of captured image to returned media store Uri

OutputStream os = this.ctx.getContentResolver().openOutputStream(uri);
bitmap.compress(Bitmap.CompressFormat.JPEG, 20, os);
os.close();

exif.createOutFile(getRealPathFromURI(uri, this.ctx));
exif.writeExifData();

bitmap.recycle();
bitmap = null;
System.gc();
result = new PluginResult(Status.OK, uri.toString());   

这与 Phonegap 使用的逻辑大致相同。 (我已经删除了所有用于发布的异常处理。所以假设没有编译错误。)所以,我基本上试图检索 Pic.jpg 并将其返回到我的phonegap应用程序。
但是发生的事情是我收到了一个 abt 150kb 的损坏文件,它甚至不是 jpg(打不开)。


请告诉我在启动相机的活动终止后是否可以以这种方式检索图像。如果可能的话,我做错了什么。请帮忙!

Note- even though im using phonegap, the question is not about some issue in that.

Hi, im developing a android app. my app is using phonegap 1.3.

mi problem...
Im using phonegap apis to take a picture and display it in my app. But whats happening is due to some reason the os kills my app after the camera is launched, so that after the photo is clicked, my app is relaunched and it doesnt get the info abt the taken picture.
As a workaround to this problem (the problem), i designed a phonegap plugin which on app start checks if the app had crashed while taking a picture (some flags in code), and if it is restarting after crash, it retrieves the Pic.jpg taken by the camera and tries to displays it. The problem is that its not able to get the right image, or even a proper .jpg file for that matter.

mi code...
Phonegap makes an intent to start the camera and passes the uri for the Pic.jpg that it creates into that intent which it passes to startActivityForResult.

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = new File(DirectoryManager.getTempDirectoryPath(ctx),  "Pic.jpg");
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
this.imageUri = Uri.fromFile(photo);
this.ctx.startActivityForResult((Plugin) this, intent, 0);

Note- The above code is from the file CameraLauncher.java of Phonegap.
Now, im assuming that the startActivityForResult stores the picture the user captures at the file which was created by 'photo' in the above code. So even if after the os closes the app while the camera is open (refer - the problem) the photo will be stored there. PLEASE correct me if this assumption is wrong, or if the photo might be being saved somewhere else.
So taking into account this assumption i wrote a plugin which retrieves this image using the same logic which Phonegap uses in CameraLauncher.java

Uri imageUri;
ExifHelper exif = new ExifHelper();
exif.createInFile(getTempDirectoryPath(ctx) + "/Pic.jpg");
exif.readExifData();
File photo = new File(getTempDirectoryPath(ctx),  "Pic.jpg");
imageUri = Uri.fromFile(photo);

Bitmap bitmap = null;

bitmap = android.provider.MediaStore.Images.Media.getBitmap(this.ctx.getContentResolver(), imageUri);


ContentValues values = new ContentValues();
values.put(android.provider.MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
Uri uri = null;
try {
     uri = this.ctx.getContentResolver().insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} catch (UnsupportedOperationException e) {
     uri = this.ctx.getContentResolver().insert(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, values);
}

// Add compressed version of captured image to returned media store Uri

OutputStream os = this.ctx.getContentResolver().openOutputStream(uri);
bitmap.compress(Bitmap.CompressFormat.JPEG, 20, os);
os.close();

exif.createOutFile(getRealPathFromURI(uri, this.ctx));
exif.writeExifData();

bitmap.recycle();
bitmap = null;
System.gc();
result = new PluginResult(Status.OK, uri.toString());   

This is roughly the same logic as Phonegap uses. (I've removed all exception handling n all for posting. So assume there are no compile errors n all.) So, im basically trying to retrieve the Pic.jpg and return it to my phonegap app.
BUT whats happening is that im getting a corrupted file of abt 150kb that isn't even a jpg (doesnt open).

Please tell me if its even possible to retrieve images in this manner after the activity that started the camera as died. And if its possible then, what am i doing wrong. Please help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯了 2025-01-03 18:39:53

正如我在您的其他问题中提到的,

由于打开相机时内存不足,phonegap android 应用程序崩溃

问题很可能是索尼对相机意图的实现。您应该尝试通过添加第三方相机应用程序进行测试,然后在拍照时选择该应用程序并查看它是否仍然崩溃。可能不会。

As I mentioned over in your other SO question,

phonegap android app crashing due to low memory on opening camera

the problem is most probably the Sony implementation of the camera intent. You should try testing by adding a third party camera app and when you take a picture select that app and see if it still crashes. It probably won't.

燕归巢 2025-01-03 18:39:53

这里的问题是由于 Phonegap 的 FileTransfer 插件中的一些代码发生了变化。
仅使用上述代码在崩溃后重新启动我的应用程序后,我能够检索相机拍摄的图像。 :)

The issue here was because of some changed code in the FileTransfer plugin of Phonegap.
I was able to retrieve an image taken by the camera, after my app restarted after the crash using the above code only. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文