从图库/相机/投放箱等选择图片 [英] Pick Image from Gallery/Camera/DropBox etc

查看:102
本文介绍了从图库/相机/投放箱等选择图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让用户从摄像机 / 图库 / DropBox的选择图像选项或设备的任何其他文件系统,并显示在一个活动作为的ImageView 对象。

How to give user the option to choose image from Camera/Gallery/DropBox OR any other File systems from the device and show it in an activity as an ImageView object.

推荐答案

有关从摄像头/相册/ DropBox的或设备的任何其他文件系统采摘图像就叫隐含意图...

For picking image from Camera/Gallery/DropBox OR any other File systems from the device just call implicit intent...

随着code可以帮助你......

Following code may helps you...

pickbtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v){
            if (Environment.getExternalStorageState().equals("mounted")){
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_PICK);
                startActivityForResult(Intent.createChooser(intent, "Select Picture:"), Constants.PICK_IMAGE_FROM_LIBRARY);
            }
        }
    });

现在使用OnActivity结果得到的数据...

Now use OnActivity result for getting data...

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(requestCode == Constants.PICK_IMAGE_FROM_LIBRARY)
    {
        if (resultCode == RESULT_OK) {
            Uri selectedImageUri = data.getData();
            String selectedImagePath = getPath(selectedImageUri);
            mImagePath = selectedImagePath;
            Bitmap photo = getPreview(selectedImagePath);
            mImageViewProfileImage.setImageBitmap(photo);
        }
    }
public String getPath(Uri uri)
{
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

public Bitmap getPreview(String fileName)
{
    File image = new File(fileName);

    BitmapFactory.Options bounds = new BitmapFactory.Options();
    bounds.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(image.getPath(), bounds);

    if ((bounds.outWidth == -1) || (bounds.outHeight == -1)) 
    {
        return null;
    }
    int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight : bounds.outWidth;
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = originalSize / 64;
    return BitmapFactory.decodeFile(image.getPath(), opts);
}
}

这篇关于从图库/相机/投放箱等选择图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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