在Android上选择/单击图像的正确解决方案 [英] Proper solution to pick/click an image on Android

查看:114
本文介绍了在Android上选择/单击图像的正确解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所示,我可以选择在应用程序中上传图像. 我想有两个选择:单击新图片&从图库中选择. 使用以下代码,在所有设备上的图库选择都可以正常工作:

As the title suggests, i'm having an option to upload an image in my app. I would like to have two options: Click a new picture & Select from gallery. Gallery selection is working fine on all devices using this code:

Intent in = new Intent();
in.setType("image/*");
in.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(in, getString(R.string.selectpicture)), 100);

问题出在Click a new picture.

我想使用设备上安装的其他相机应用程序来获取图像. 此代码应将图像用户的点击保存在指定的路径上.

I want to use other camera apps installed on the device to get the image. This code should save the image user clicks at the specified path.

Intent m_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageUri = getImageUri();
m_intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(m_intent, REQUEST_IMAGE_CAPTURE);

但是发生的是,并非所有相机应用程序都尊重EXTRA_OUTPUT.

But what happens is, the EXTRA_OUTPUT is not respected by all camera apps.

如果内存不足,我的应用也会被系统杀死,这会使事情变得更加复杂.

Also if memory is low, my app is killed by the system which makes things more complicated.

那么,使用户能够单击新图片并在我的应用程序中获取图像路径的最佳方法是什么? 如果使用第三方库更好,那么哪个可靠?

So, what's the best way to enable user to click a new picture and get the image path in my app? If using third party libraries is better, which one's are reliable?

推荐答案

并非所有相机应用程序都尊重EXTRA_OUTPUT.

the EXTRA_OUTPUT is not respected by all camera apps.

每次将任何内容委派给第三方应用程序时,都会遇到问题.

Any time you delegate anything to a third party app, you can run into problems.

如果内存不足,我的应用也会被系统杀死,这会使事情变得更加复杂.

Also if memory is low, my app is killed by the system which makes things more complicated.

您仍然需要处理此问题.将在EXTRA_OUTPUT中输入的值保存为已保存实例状态Bundle.

You need to handle this anyway. Save the value you put in EXTRA_OUTPUT in the saved instance state Bundle.

让用户点击新图片并在我的应用中获取图片路径的最佳方法是什么?

what's the best way to enable user to click a new picture and get the image path in my app?

如果您想坚持使用ACTION_IMAGE_CAPTURE,我将使用此分类:

If you wish to stick with ACTION_IMAGE_CAPTURE, I'd go with this triage:

  • 如果您提供给EXTRA_OUTPUT的位置中有图像,请使用它

  • If there is an image in the location you provided to EXTRA_OUTPUT, use it

如果不是,并且onActivityResult()中提供给您的Intent上的getData()返回Uri(而不是null),请使用该值(如果您确实需要File ,将ContentResolveropenInputStream()Uri一起使用,并将内容复制到您自己的文件中)

If not, and getData() on the Intent given to you in onActivityResult() returns a Uri (and not null), use that (and if you truly need a File, use ContentResolver and openInputStream() with the Uri and copy the contents into your own file)

如果都不为真,并且getParcelableExtra("data")返回一个值,则将该Bitmap保存到文件中

If neither are true, and getParcelableExtra("data") returns a value, save that Bitmap to a file

如果这些都不是真的,建议用户使用其他相机应用程序,也许将其指向您尝试过并知道有效的相机应用程序

If none of those are true, suggest to the user to get a different camera app, perhaps pointing them to one that you have tried and know works

或者直接或使用我的之类的图片库自己拍摄照片.

Or, take the picture yourself, directly or using a library like mine.

这篇关于在Android上选择/单击图像的正确解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆