Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Viewing a Captured Photo

Sahit penmatcha
Sahit penmatcha
4,555 Points

Image wont appear in ImageView

Im trying to access the picture taken from the camera in a dialog fragment. This is my code. There are no errors, but the image isn't loading into the imageView in the dialog

MainActivity

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

    TaskAdapter adapter = new TaskAdapter(this);
    View view = LayoutInflater.from(this).inflate(R.layout.picture_setup_dialog, null);

    Button mTaskButton = (Button)view.findViewById(R.id.taskButton);
    Button mExamButton = (Button)view.findViewById(R.id.examButton);
    ImageView mUserPicture = (ImageView) view.findViewById(R.id.userPicture);

    if (resultCode == RESULT_OK){
        if (requestCode == adapter.REQUEST_TAKE_PHOTO){
            //write code for what happens after picture taken here
            Picasso.with(this).load(adapter.getImageUri()).into(mUserPicture);
            AlertDialog.Builder builder = new AlertDialog.Builder(this)
                    .setTitle("Add Picture")
                    .setView(view)
                    .setPositiveButton("Add", null)
                    .setNegativeButton("Cancel", null);

            AlertDialog dialog = builder.create();
            dialog.show();
        }
    }

}

TaskAdapter

mTakePicButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mMediaUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
                if (mMediaUri == null){
                    Toast.makeText(mContext, "There was a problem accessing your device's external storage", Toast.LENGTH_LONG).show();
                } else{
                    Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    ((Activity) mContext).startActivityForResult(takePhotoIntent, REQUEST_TAKE_PHOTO);
                    takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
                }
            }

            private Uri getOutputMediaFileUri(int mediaType) {
                //check for external storage
                if  (isExternalStorageAvailable()){
                    //get the URI

                    //1.Get the external storage directory
                    File mediaStorageDir = mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
                    //2.Create a unique file name
                    String fileName = "";
                    String fileType = "";
                    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

                    if (mediaType == MEDIA_TYPE_IMAGE) {
                        fileName = "IMG_"+ timeStamp;
                        fileType = ".jpg";
                    }  else {
                        return null;
                    }
                    //3.Create the file
                    File mediaFile;
                    try {
                        mediaFile = File.createTempFile(fileName, fileType, mediaStorageDir);
                        //4.Return the file's Uri
                        mImageUri = Uri.fromFile(mediaFile);
                        return mImageUri;
                    }
                    catch (IOException e){
                        Toast.makeText(mContext, "Error creating file", Toast.LENGTH_LONG).show();
                    }
                }
                //something went wrong
                return null;
            }

            private boolean isExternalStorageAvailable(){
                String state = Environment.getExternalStorageState();
                if (Environment.MEDIA_MOUNTED.equals(state))
                    return true;
                else
                    return false;
            }
        });

public Uri getImageUri(){ return mImageUri; }

Peng Ouyang
Peng Ouyang
5,805 Points

Me neither!! I use my phone to test it, it doesn't have an external SD card. I wonder if the problem happens with this situation.