android - 从用户的图库

时间:2018-06-05 08:41:33

标签: android

这是我的代码。我在显示和保存从用户画廊中挑选的照片时遇到了问题,而当我从相机中进行拍摄时,它对我来说效果很好。 这是用户如何被引用到他的画廊 -

if (v.getId() == R.id.btnAddDucuments) {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

        alertDialog.setMessage("to add documents");
        alertDialog.setPositiveButton("from camera", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // user choose to take a picture

                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAM_REQUEST);

            }
        });

        alertDialog.setNegativeButton("from gallery", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // user choose to pick a photo from gallery

                Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), GET_FROM_GALLERY);                }
        });


        alertDialog.show();

以下是图片应该如何保存。我需要它作为一个字符串(所以我可以进一步使用它)和在这个avtivity(名为imgTheDocument)的ImageView。

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

    if(requestCode==GET_FROM_GALLERY && resultCode == Activity.RESULT_OK) {
        Uri selectedImage = data.getData();
        Bitmap bitmap = null;
        try {
            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);

            StringImgDocument = BitMapToString(bitmap);

            imgTheDocument.setImageBitmap(bitmap);

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

当我运行此代码时,按下想要的图片后活动停止。我无法理解问题出在哪里。我以前使用过这段代码而且工作得很好。

我会帮助任何帮助!!感谢:)

我的清单权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

1 个答案:

答案 0 :(得分:0)

选择图像的意图(这是免费的,适用于所有Android版本!)

Intent intent;
        if (Build.VERSION.SDK_INT < 19){
            intent = new Intent();
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(intent, GET_FROM_GALLERY);
        } else {
            intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            intent.setType("image/*");
            startActivityForResult(intent, GET_FROM_GALLERY);
        } 

这是针对onActivityResult

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

        if(requestCode==GET_FROM_GALLERY && resultCode == Activity.RESULT_OK) {

                //you can both use the selectedImageUri or yourSelectedImagePath for working the Image you picked,send it somewhere or store it in database or etc..
                Uri selectedImageUri = data.getData();

                String yourSelectedImagePath = selectedImage.tostring;
                //I just use the Uri to display the selected image in an imageview
                yourImageView.setImageUri(selectedImage);

        }
相关问题