[英]Picture coming from camera or gallery?
I have an intent chooser that allows me to pick image from gallery or camera like this: 我有一个意图选择器,允许我从画廊或相机中选择图像,如下所示:
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT,null);
galleryIntent.setType("image/*");
galleryIntent.addCategory(Intent.CATEGORY_OPENABLE);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Intent chooser = new Intent(Intent.ACTION_CHOOSER);
chooser.putExtra(Intent.EXTRA_INTENT, galleryIntent);
chooser.putExtra(Intent.EXTRA_TITLE, "title");
Intent[] intentArray = {cameraIntent};
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooser,REQUEST_CODE);
I want my onActivityResult
method to be like this: 我想我的
onActivityResult
方法是这样的:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(Condition == picture_coming_from_gallery)
{
//my code here
}
else if(condition == picture_coming_from_camera)
{
//another code here
}
}
What is the condition that allows me to know which source my image is coming from? 是什么条件让我知道我的图像来自哪个来源?
Updated: 更新:
Now it's working and here is the solution: 现在它正在运行,这是解决方案:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
{
if(data.getData()!=null)
{
try
{
if (bitmap != null)
{
bitmap.recycle();
}
InputStream stream = getContentResolver().openInputStream(data.getData());
bitmap = BitmapFactory.decodeStream(stream);
stream.close();
imageView.setImageBitmap(bitmap);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
else
{
bitmap=(Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(bitmap);
}
super.onActivityResult(requestCode, resultCode, data);
}
}
Thank you for you help! 谢谢你的帮助!
Although the current piece of code is a neat way of presenting options to choose from, I found that it was severely difficult to manage. 虽然目前的一段代码是一种简洁的方式来呈现可供选择的选项,但我发现管理起来非常困难。 At least in my use case it was.
至少在我的用例中是这样的。 I need to store images taken off the Camera to process further in the Aviary SDK ( if the user so chooses ).
我需要存储从相机拍摄的图像以进一步处理Aviary SDK( 如果用户选择的话 )。
To that end, I would like to propose a workaround. 为此,我想提出一个解决方法。
This does not address your question per se. 这本身并不能解决您的问题。 But offers an alternative considering you need to know where the Image is coming from (Camera / Gallery) .
但考虑到您需要知道图像的来源(相机/图库),可提供另一种选择 。
AlertDialog.Builder builder = new AlertDialog.Builder(StatusUpdate.this);
builder.setTitle("Choose Image Source");
builder.setItems(new CharSequence[] {"Gallery", "Camera"},
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
// GET IMAGE FROM THE GALLERY
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
Intent chooser = Intent.createChooser(intent, "Choose a Picture");
startActivityForResult(chooser, ACTION_REQUEST_GALLERY);
break;
case 1:
Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE");
File cameraFolder;
if (android.os.Environment.getExternalStorageState().equals
(android.os.Environment.MEDIA_MOUNTED))
cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),
"some_directory_to_save_images/");
else
cameraFolder= StatusUpdate.this.getCacheDir();
if(!cameraFolder.exists())
cameraFolder.mkdirs();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
String timeStamp = dateFormat.format(new Date());
String imageFileName = "picture_" + timeStamp + ".jpg";
File photo = new File(Environment.getExternalStorageDirectory(),
"some_directory_to_save_images/" + imageFileName);
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
initialURI = Uri.fromFile(photo);
startActivityForResult(getCameraImage, ACTION_REQUEST_CAMERA);
break;
default:
break;
}
}
});
builder.show();
This is the result ( I still maintain that you code gives a much better selection set, but again, not the simplest thing in your use case or in mine either ): 这是结果( 我仍然认为你的代码提供了一个更好的选择集,但同样,不是你的用例或我的最简单的东西 ):
Now you can process the result based on the source of the selection: 现在,您可以根据选择的来源处理结果:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case ACTION_REQUEST_GALLERY:
break;
case ACTION_REQUEST_CAMERA:
break;
}
}
};
UPDATED: 更新:
Found it!! 找到了!! There is an answer here on SO that addresses exactly what you need.
这里有一个答案可以解决你所需要的问题。 It is still a * workaround of sorts*.
它仍然是各种*的解决方法。 In the sense that it does not rely on different
requestCodes
. 从某种意义上说它不依赖于不同的
requestCodes
。 But works nonetheless. 但仍然有效。
Strange I missed it when I was stuck with this. 奇怪的是,当我被困在这里时我错过了它。 :-(
:-(
NOTE: I am not posting the code here and am linking to it instead. 注意: 我不是在这里发布代码而是链接到它。 All props go to the original author.
所有道具都归原作者所有。 :-)
:-)
You can distinguish by using REQUEST_CODE 您可以使用REQUEST_CODE进行区分
private static final int PICK_FROM_CAMERA = 1;
private static final int PICK_FROM_GALLARY = 2;
/* For Image capture from camera */
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, PICK_FROM_CAMERA);
/* For Image capture from Gallary*/
startActivityForResult(new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI),
PICK_FROM_GALLARY);
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode) {
case PICK_FROM_CAMERA:
if (resultCode == Activity.RESULT_OK) {
Bitmap bitmapImage = (Bitmap) intent.getExtras().get("data");
}
break;
case PICK_FROM_GALLARY:
if (resultCode == Activity.RESULT_OK) {
Uri mImageCaptureUri = intent.getData();
}
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.