ImageView不显示从手机摄像头或照片库中拍摄的图像

时间:2017-06-07 22:50:14

标签: android

我目前正在设计一款Android应用程序,该应用程序在某个时刻需要用户从相机或手机的图库中选择图片。之后,我需要在活动的图像视图中显示所述图片。

我认为获取图片的代码部分没问题,因为我可以从两个选项中有效地选择照片。但在此之后,除了没有显示图片外,图像视图的src徽标消失了。我不明白我做错了什么或者这里有什么东西丢失了,因为这是一个非致命的错误,应用程序继续执行。

所以这是我的活动的XML布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="@style/Widget.Design.CoordinatorLayout"
android:orientation="vertical"

tools:context="pt.unl.fct.di.www.myapplication.ReportActivity">

<ScrollView
    android:id="@+id/report_form"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/submit_report_form"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:weightSum="1">

        <TextView
            android:id="@+id/type_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="30dp"
            android:text="@string/select_report_type"
            android:visibility="visible" />
        <Spinner
            android:id="@+id/report_type_spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:layout_marginBottom="30dp"/>


        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <EditText
                android:id="@+id/description"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:hint="Description"
                android:inputType="textMultiLine"
                android:selectAllOnFocus="false"
                android:layout_marginBottom="30dp"/>

        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="3">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Location"
                android:layout_marginBottom="30dp"/>

            <Button
                android:id="@+id/button2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Map"
                android:layout_marginBottom="30dp"/>
        </android.support.design.widget.TextInputLayout>

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:src="@android:drawable/ic_menu_camera"
            android:adjustViewBounds="true"
             />

        <Button
            android:id="@+id/camera_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Add Picture" />

        <Button
            android:id="@+id/submit_report_button"
            style="?android:textAppearanceSmall"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="@string/action_submit_report"
            android:textStyle="bold"
            android:layout_weight="0.68" />


    </LinearLayout>
</ScrollView>

活动的java(重要的东西):

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_report);
    Spinner spinner = (Spinner) findViewById(R.id.report_type_spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.report_types, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
    spinner.setAdapter(adapter);
    mReportType = spinner.getItemAtPosition(0).toString();
    spinner.setOnItemSelectedListener(this);
    mDescription = (EditText)findViewById(R.id.description);
    mImageLocationView =(ImageView) findViewById(R.id.imageView);
    Button cameraButton = (Button)findViewById(R.id.camera_button);
    cameraButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle(R.string.choose_photo_from)
                    .setItems(R.array.picture_types, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // The 'which' argument contains the index position
                            // of the selected item
                            switch(which){
                                case 0: //fotografia
                                    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                    startActivityForResult(takePicture, 0);
                                    onPause();
                                    //zero can be replaced with any action code
                                    break;
                                case 1:
                                    //galeria
                                    Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                                    startActivityForResult(pickPhoto , 1);
                                    //one can be replaced with any action code
                                    break;
                            }


                        }
                    });
            builder.create().show();
        }
    });


}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    BitmapFactory.Options o = new BitmapFactory.Options();
    mImageLocationView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    switch(requestCode) {
        case 0:
            if(resultCode == RESULT_OK){
                Bitmap photo = (Bitmap) imageReturnedIntent.getExtras().get("data");
                mImageLocationView.setImageBitmap(photo);

            }

            break;
        case 1:
            if(resultCode == RESULT_OK){
                Bitmap photo = (Bitmap) imageReturnedIntent.getExtras().get("data");
                mImageLocationView.setImageBitmap(photo);
            }
            break;
    }

}

我必须说,这是我的第一个Android项目,所以我对这么多的信息和APIS感到不知所措,并且做了大量的实验。

1 个答案:

答案 0 :(得分:0)

只需在`

中添加以下代码即可
if(resultCode == RESULT_OK){   
    // the new code
     Uri imageUri = data.getData();
            String[] filePath = { MediaStore.Images.Media.DATA };
            Cursor cursor = getActivity().getContentResolver().query(imageUri, filePath, null, null, null);
            cursor.moveToFirst();
            String path = cursor.getString(cursor.getColumnIndex(filePath[0]));
            cursor.close();

            //refresh Image view
            updateImageView(path);
        }
// the update image method
    public void updateImageView(String completePath) {
         Picasso picasoo = Picasso.with(getActivity());
          String path = "file://" + completePath;
          Long tsLong = System.currentTimeMillis();
         String ts = tsLong.toString();
          picasoo.load(path+"?time="+ts).fit().centerCrop().networkPolicy(OFFLINE).into(imgUserProfile);

}` Picasso是一个图像管理库,可以通过延迟加载图像来简化您的生活。