【问题标题】:Android: Take image from any sourceAndroid:从任何来源获取图像
【发布时间】:2015-06-28 08:26:11
【问题描述】:

我正在尝试从任何来源(如保管箱、画廊、相机、retrica 等)获取图像,获取他的路径并将其设置在 ImageView 上。通过使用这个意图

  private void cameraIntent() {
      Intent pickIntent = new Intent();
        pickIntent.setType("image/*");
        pickIntent.setAction(Intent.ACTION_GET_CONTENT);

        Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);


        String pickTitle = "Select or take a new Picture";
        Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
        chooserIntent.putExtra
                (
                        Intent.EXTRA_INITIAL_INTENTS,
                        new Intent[]{takePhotoIntent}
                );

        startActivityForResult(chooserIntent, REQUEST_CAMERA);

    }

但现在,我只能处理 Gallery 和 Camera Intent,如何处理其他应用程序?

【问题讨论】:

    标签: android android-intent camera gallery handle


    【解决方案1】:

    要从相机/图库/DropBox 或设备中的任何其他文件系统中选择图像,只需调用隐式意图...

    以下代码可能会对您有所帮助..

    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 结果获取数据...

    @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);
    }
    }
    

    这是一个完整的例子:https://gist.github.com/felixgborrego/7943560

    【讨论】:

    • 这个例子只处理图库
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 2023-03-11
    相关资源
    最近更新 更多