Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

java - Placing an Picture in ImageView selected from Gallery

I want to select an image from gallery and place that selected image in the ImageView. I have also an Intent that will take picture through camera and place it in ImageView and it is working well. But, gallery Intent is only opening the chooser and then select the image, but it is not getting placed in the ImageView

In Log this error is coming up

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

Open Gallery Method

 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Below is the working solution of choosing a picture from library or capturing one from camera and using it in a imageview.

Source : 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();
                }

            }
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...