在从Gallery中选择的ImageView中放置图片

时间:2016-09-08 09:43:29

标签: java android

我想从图库中选择一张图片,并将所选图片放在ImageView中。我还有一个Intent,它会通过相机拍摄并将其放在ImageView中并且效果很好。但是,图库Intent只会打开选择器然后选择图片,但它不会放在ImageView

在日志中出现此错误

  

E / OpenGLRenderer:SFEffectCache:clear(),mSize = 0

打开图库方法

 private void openGallery() {
    tvGallery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplicationContext(), "Opening Gallery, Please wait..", Toast.LENGTH_SHORT).show();
            private int PICK_IMAGE_REQUEST = 1;
            Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            galleryIntent.setType("image/*");
            startActivityForResult(galleryIntent,PICK_IMAGE_REQUEST);
            Log.e("Status:", "Photopicker canceled");


        }
    });
}

onActivityResult

  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    /*camera preview*/
    if (resultCode == RESULT_OK) {
        Bitmap bp = (Bitmap) data.getExtras().get("data");
        previewView.setImageBitmap(bp);
    }
    /*gallery preview*/
   else if (requestCode == PICK_IMAGE_REQUEST) {
        if (resultCode == RESULT_OK) {

            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            previewView = (ImageView) findViewById(R.id.imgPostIssue);
            previewView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }

    }
}

LOG ERROR

09-08 15:40:02.355 30860-30860/bluecoppertech.com.taskmeld E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method bluecoppertech.com.taskmeld.Activity.activity.utils.PostNewIssue.access$super
09-08 15:40:03.836 30860-30860/bluecoppertech.com.taskmeld E/Status:: Photopicker canceled
09-08 15:40:07.460 30860-30860/bluecoppertech.com.taskmeld E/OpenGLRenderer: SFEffectCache:clear(), mSize = 0
09-08 15:40:21.754 30860-30860/bluecoppertech.com.taskmeld E/OpenGLRenderer: SFEffectCache:clear(), mSize = 0
09-08 15:40:32.885 32246-32246/bluecoppertech.com.taskmeld E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method bluecoppertech.com.taskmeld.Activity.activity.utils.PostNewIssue.access$super
09-08 15:40:32.885 32246-32246/bluecoppertech.com.taskmeld E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method bluecoppertech.com.taskmeld.Activity.activity.utils.PostNewIssue.access$super
09-08 15:40:32.885 32246-32246/bluecoppertech.com.taskmeld E/dalvikvm: Could not find class 'android.media.session.MediaController', referenced from method bluecoppertech.com.taskmeld.Activity.activity.utils.PostNewIssue.access$super
09-08 15:40:32.895 32246-32246/bluecoppertech.com.taskmeld E/dalvikvm: Could not find class 'android.widget.Toolbar', referenced from method bluecoppertech.com.taskmeld.Activity.activity.utils.PostNewIssue.access$super
09-08 15:40:32.905 32246-32246/bluecoppertech.com.taskmeld E/dalvikvm: Could not find class 'android.app.ActivityManager$TaskDescription', referenced from method bluecoppertech.com.taskmeld.Activity.activity.utils.PostNewIssue.access$super
09-08 15:40:32.915 32246-32246/bluecoppertech.com.taskmeld E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method bluecoppertech.com.taskmeld.Activity.activity.utils.PostNewIssue.access$super
09-08 15:40:32.915 32246-32246/bluecoppertech.com.taskmeld E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method bluecoppertech.com.taskmeld.Activity.activity.utils.PostNewIssue.access$super
09-08 15:40:32.945 32246-32246/bluecoppertech.com.taskmeld E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method bluecoppertech.com.taskmeld.Activity.activity.utils.PostNewIssue.access$super
09-08 15:40:32.965 32246-32246/bluecoppertech.com.taskmeld E/dalvikvm: Could not find class 'android.app.assist.AssistContent', referenced from method bluecoppertech.com.taskmeld.Activity.activity.utils.PostNewIssue.access$super
09-08 15:40:32.975 32246-32246/bluecoppertech.com.taskmeld E/dalvikvm: Could not find class 'android.view.SearchEvent', referenced from method bluecoppertech.com.taskmeld.Activity.activity.utils.PostNewIssue.access$super
09-08 15:40:32.995 32246-32246/bluecoppertech.com.taskmeld E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method bluecoppertech.com.taskmeld.Activity.activity.utils.PostNewIssue.access$super
09-08 15:40:33.225 32246-32246/bluecoppertech.com.taskmeld E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering

4 个答案:

答案 0 :(得分:0)

以下是从库中选择图片或从相机中捕捉图片并在图片视图中使用图片的工作解决方案。

来源:SO

//functions to select image from the device
    private void selectImage() {
        final CharSequence[] items = {"Take Photo","Choose from Library", "Cancel" };
        AlertDialog.Builder builder = new AlertDialog.Builder(signature_new.this);
        builder.setTitle("Add Photo!");
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                if (items[item].equals("Take Photo")) {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(intent, REQUEST_CAMERA);
                } else if (items[item].equals("Choose from Library")) {
                    Intent intent = new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    intent.setType("image/*");
                    startActivityForResult(
                            Intent.createChooser(intent, "Select File"),
                            SELECT_FILE);
                } else if (items[item].equals("Cancel")) {
                    dialog.dismiss();
                }
            }
        });
        builder.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CAMERA) {
                Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

                File destination = new File(Environment.getExternalStorageDirectory(),
                        System.currentTimeMillis() + ".jpg");

                FileOutputStream fo;
                try {
                    destination.createNewFile();
                    fo = new FileOutputStream(destination);
                    fo.write(bytes.toByteArray());
                    fo.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


                imageF.setImage(ImageSource.bitmap(thumbnail));

            } else if (requestCode == SELECT_FILE) {
                Uri selectedImageUri = data.getData();
                try {

                    Bitmap bm=decodeUri(selectedImageUri);
                    imageViewF.setImage(ImageSource.bitmap(bm));
                    //uploadbm=bm;
                    //dialog_dimension();
                }
                catch(FileNotFoundException e) {
                    e.printStackTrace();
                }

            }
        }
    }

答案 1 :(得分:0)

这是打开图库的意图

Intent Gallery_intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(Gallery_intent, constants.RESULT_LOAD_IMAGE);

这是活动结果

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
     if (requestCode == constants.RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null) {
 File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = false;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        b = BitmapFactory.decodeFile(getRealPathFromURI(data.getData()), options);
        int bmpHeight = b.getHeight();
        int bmpWidth = b.getWidth();
        if (bmpHeight > 1500) {
            bmpHeight = bmpHeight / 4;
            bmpWidth = bmpWidth / 4;
        }
        Bitmap out = Bitmap.createScaledBitmap(b, bmpWidth, bmpHeight, false);

        File file = new File(dir, "resize.png");
        FileOutputStream fOut;
        try {
            fOut = new FileOutputStream(file);
            out.compress(Bitmap.CompressFormat.PNG, 100, fOut);
            fOut.flush();
            fOut.close();
            b.recycle();
            out.recycle();
        } catch (Exception e) {
        }

         ImageView.setImageURI(Uri.fromFile(file));
  }

答案 2 :(得分:0)

更正您的代码... onActivityResult

中的条件非常错误
private void openGallery() {
    tvGallery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplicationContext(), "Opening Gallery, Please wait..", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent();
            // Show only images, no videos or anything else
           intent.setType("image/*");
           intent.setAction(Intent.ACTION_GET_CONTENT);
          // Always show the chooser (if there are multiple options available)
          startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);


        }
    });
}

你的OnActivityResult

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

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

        Uri uri = data.getData();

        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
            // Log.d(TAG, String.valueOf(bitmap));


            previewView = (ImageView) findViewById(R.id.imgPostIssue);
            previewView.setImageBitmap(BitmapFactory.decodeFile(picturePath));


            ImageView previewView = (ImageView) findViewById(R.id.imgPostIssue);
            previewView.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

答案 3 :(得分:0)

  

如何将图片放入图库中的http://MY_SERVER_NAME

ImageView

private int PICK_IMAGE_REQUEST = 1;
  

OnActivityResult

private void openGallery() {
        tvGallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                // Show only images, no videos or anything else
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                // Always show the chooser (if there are multiple options available)
                startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);


            }
        });
    }