Imageview消失了?

问题描述 投票:0回答:2

我刚刚做了一个新的新项目,但是当我在布局中添加图片视图并启动模拟器时,我找不到它。我的目标是,使用户单击imageview,然后将其定向到图库,然后选择图片,然后将其保存在数据库中,就这么简单。但是,当我启动我的应用程序时,我只是找不到imageview。关于发生了什么的任何想法?

这是我的布局:

<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageVieww"
        android:layout_width="148dp"
        android:layout_height="160dp"
        app:layout_constraintBottom_toTopOf="@+id/Arabic"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@drawable/galboy" />

    <EditText
        android:id="@+id/Arabic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Arabic"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.268" />

    <EditText
        android:id="@+id/Long"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Long"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/English"
        app:layout_constraintVertical_bias="0.071" />

    <EditText
        android:id="@+id/Mob"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Mob"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/Lat"
        app:layout_constraintVertical_bias="0.072" />

    <EditText
        android:id="@+id/user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Username"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/Mob"
        app:layout_constraintVertical_bias="0.103" />

    <EditText
        android:id="@+id/pass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Pass"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/user"
        app:layout_constraintVertical_bias="0.144" />

    <EditText
        android:id="@+id/Lat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Lat"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/Long"
        app:layout_constraintVertical_bias="0.075" />

    <EditText
        android:id="@+id/English"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="English"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/Arabic"
        app:layout_constraintVertical_bias="0.058" />

</androidx.constraintlayout.widget.ConstraintLayout>

这里是在代码中调用我的imageview的地方:

imageoffarm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            askFileReadPermission();
        }
    });
}

private void askFileReadPermission() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, READ_PERM_CODE);
    } else {
        Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(gallery, GALLERY_REQUEST_CODE);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == CAMERA_PERM_CODE || requestCode == READ_PERM_CODE || requestCode == WRITE_PERM_CODE) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(gallery, GALLERY_REQUEST_CODE);
        }
    } else {
        Toast.makeText(this, "الرجاء قبول الكميرة لاجل التصوير", Toast.LENGTH_SHORT).show();
    }
}

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



    if (requestCode == GALLERY_REQUEST_CODE) {
        if (resultCode == Activity.RESULT_OK) {
            Uri contentUri = data.getData();
            File imageFile = new File(getRealPathFromURI(MainActivity.this, contentUri));
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            String imageFileName = "JPEG_" + timeStamp + "." + getFileExt(contentUri);
            DatabaseHelper databaseHelper = new DatabaseHelper(MainActivity.this);
            databaseHelper.SavingimagepathAR(Uri.fromFile(imageFile).getPath());
            Log.d("tag", "onActivityResult: Gallery Image Uri:  " + Uri.fromFile(imageFile));
            imageoffarm.setImageURI(contentUri);


        }

    }
}


private String getRealPathFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {
        String[] proj = {MediaStore.Images.Media.DATA};
        cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } catch (Exception e) {
        Log.e(TAG, "getRealPathFromURI Exception : " + e.toString());
        return "";
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

private String getFileExt(Uri contentUri) {
    ContentResolver c = getContentResolver();
    MimeTypeMap mime = MimeTypeMap.getSingleton();
    return mime.getExtensionFromMimeType(c.getType(contentUri));
}

任何帮助!?

java android imageview android-imageview
2个回答
1
投票

您使用了tools修饰符,它仅在Editor mode中显示图像,并且它将在屏幕上显示一个空的ImageView,但是您仍然可以在其上单击

更改后:

<ImageView
    ..
    app:srcCompat="@drawable/galboy" />

0
投票

正在使用错误的名称空间。

ImageView内部,使用app:srcCompat代替tools:srcCompat

© www.soinside.com 2019 - 2024. All rights reserved.