捕获的图片不会出现在Dialog中的imageView中 - Android Studio

时间:2016-07-15 21:21:50

标签: android android-studio imageview android-camera

我试图在对话框片段中访问从相机拍摄的照片。这是我的代码。没有错误,但图像未加载到对话框中的ImageView

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

0 个答案:

没有答案