图片来源相机或画廊来了吗? [英] 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);
我希望我的onActivityResult方法是这样的:
i want my onActivityResult method to be like this:
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?
更新:
现在它的工作,这里是解决方案:
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);
}
}
谢谢您的帮助!
推荐答案
虽然目前一张code是一个整洁的方式presenting选择从,我发现,这是严重的难以管理的选择。至少在我使用的情况下,它是。我需要存储在鸟舍SDK进一步处理带下摄像机图像(如果用户选择这样做的)。
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).
为此,我愿提出一个解决方法。
To that end, I would like to propose a workaround.
这不会解决你的问题本身。但是提供了一种替代考虑你需要知道的图像是从(摄像机/图库)的。
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();
这是结果(我仍然认为你code给出一个更好的选择集,但同样,在您的使用情况不是简单的事情,或在我的任的):
现在,您可以根据选择的源处理结果:
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;
}
}
};
更新:
找到它!有一个回答这里SO ,解决正是你需要的。它仍然是各种各样的*解决方法*。在某种意义上说,它不依赖于不同的请求codeS
。但尽管如此工作。
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
. But works nonetheless.
奇怪我错过了它,当我坚持这一点。 : - (
Strange I missed it when I was stuck with this. :-(
注意: 我不张贴code在这里和我链接到它,而不是。所有的道具去原作者。 : - )的
这篇关于图片来源相机或画廊来了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!