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

Android ImageView不显示从手机摄像头或照片库拍摄的图像,android,Android,我目前正在设计一款android应用程序,它要求用户在某一点上从相机或手机的多媒体资料中选择图片。之后,我需要在活动的图像视图中显示所述图片 我认为获取图片的代码部分是可以的,因为我可以有效地从两个选项中选择图片。但是在那之后,除了没有显示图片之外,图像视图的src徽标也消失了。我不明白我是否做错了什么,或者这里是否缺少了什么,因为这是一个非致命错误,应用程序仍在继续执行 下面是我的活动XML布局: <?xml version="1.0" encoding="utf-8"?> <

我目前正在设计一款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(重要内容):

创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_报告);
微调器微调器=(微调器)findViewById(R.id.report\u type\u微调器);
//使用字符串数组和默认微调器布局创建ArrayAdapter
ArrayAdapter=ArrayAdapter.createFromResource(此,
R.array.report_types、android.R.layout.simple_微调器_项);
//指定显示选项列表时要使用的布局
setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
//将适配器应用于微调器
旋转器。设置适配器(适配器);
mReportType=spinner.getItemAtPosition(0.toString();
spinner.setOnItemSelectedListener(此);
mddescription=(EditText)findViewById(R.id.description);
mImageLocationView=(ImageView)findViewById(R.id.ImageView);
按钮cameraButton=(按钮)findViewById(R.id.camera_按钮);
cameraButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
AlertDialog.Builder=新建AlertDialog.Builder(mContext);
builder.setTitle(R.string.choose\u photo\u from)
.setItems(R.array.picture_类型,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
//“which”参数包含索引位置
//所选项目的名称
开关(哪个){
案例0://fotografia
意向拍摄=新意向(MediaStore.ACTION\u IMAGE\u CAPTURE);
startActivityForResult(拍摄照片,0);
onPause();
//零可以替换为任何操作代码
打破
案例1:
//盔甲
Intent pickPhoto=新Intent(Intent.ACTION\U PICK,
android.provider.MediaStore.Images.Media.EXTERNAL\u CONTENT\u URI);
startActivityForResult(pickPhoto,1);
//一个可以替换为任何操作代码
打破
}
}
});
builder.create().show();
}
});
}
@凌驾
ActivityResult上受保护的void(int-requestCode、int-resultCode、Intent-ImageReturnedContent){
super.onActivityResult(请求代码、结果代码、图像返回内容);
BitmapFactory.Options o=新的BitmapFactory.Options();
mImageLocationView.setScaleType(ImageView.ScaleType.CENTER_-INSIDE);
开关(请求代码){
案例0:
if(resultCode==RESULT\u OK){
位图照片=(位图)imageReturnedIntent.getExtras().get(“数据”);
mImageLocationView.setImageBitmap(照片);
}
打破
案例1:
if(resultCode==RESULT\u OK){
位图照片=(位图)imageReturnedIntent.getExtras().get(“数据”);
mImageLocationView.setImageBitmap(照片);
}
打破
}
}

我必须说,这是我的第一个android项目,所以我被如此多的信息和API淹没了,并且做了大量的实验。

只需添加以下代码即可

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);
}`
毕加索是一个图像管理库,通过惰性加载图像,使您的生活变得轻松。

ACTION\u PICK
不使用额外的
“数据”
。返回的
Uri
imageReturnedIntent.getData()
)指向图像。使用图像加载库(Glide、毕加索等)将其填充到您的
ImageView
。它成功了!现在我知道怎么做了,我将进一步探索毕加索的API。非常感谢你!
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);