Java 通过用户选择在imageview中添加图片

Java 通过用户选择在imageview中添加图片,java,android,Java,Android,我想将图片添加到一个图像视图中,并在一行中添加文本纹理。此代码正在打开库并让我选择一张图片。但是,当选择图片时,图片将不会显示在图像纹理中。我从这个链接获取了代码 [ 这是我的密码 image.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i

我想将图片添加到一个图像视图中,并在一行中添加文本纹理。此代码正在打开库并让我选择一张图片。但是,当选择图片时,图片将不会显示在图像纹理中。我从这个链接获取了代码 [

这是我的密码

     image.setOnClickListener(new View.OnClickListener() {
                 @Override
               public void onClick(View v) {
                   Intent i = new Intent(
                        Intent.ACTION_PICK,

     android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });

这是xml

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <ImageView
        android:padding="1dp"
        android:background="@color/Black"
        android:layout_marginTop="10dp"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:id="@+id/image"

        android:layout_weight="0.5"
       />
    <TextView
        android:layout_marginTop="10dp"
        android:text="TextView"
        android:layout_width="match_parent"


        android:singleLine="true"
        android:layout_gravity="right"
        android:background="@color/White"
        android:layout_height="wrap_content"
        android:id="@+id/child"
        android:textSize="24sp"/>
    </LinearLayout>

我想您忘记了读取外部存储的权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

您需要请求手动许可:

if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
    != PackageManager.PERMISSION_GRANTED) {

  ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                1);
} 在它覆盖之后:

@Override
public void onRequestPermissionsResult(int requestCode,
                                   String permissions[], int[] grantResults) {
switch (requestCode) {
    case 1: {

      // If request is cancelled, the result arrays are empty.
      if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            // permission was granted, yay! Do the
            // contacts-related task you need to do.          
        } else {

            // permission denied, boo! Disable the
            // functionality that depends on this permission.
            Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
        }
        return;
    }

    // other 'case' lines to check for other
    // permissions this app might request
}
}

API级别21之后,需要手动请求权限。

您可以使用Log.d(“标记”、“图片路径:”+picturePath);如果日志未显示,您可以检查生成版本代码、Android版本M或更高要求的权限读取外部存储。如果日志显示,您可以检查绝对路径图像并搜索“从外部存储加载位图”.我希望我的回答能对你有所帮助!
@Override
public void onRequestPermissionsResult(int requestCode,
                                   String permissions[], int[] grantResults) {
switch (requestCode) {
    case 1: {

      // If request is cancelled, the result arrays are empty.
      if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            // permission was granted, yay! Do the
            // contacts-related task you need to do.          
        } else {

            // permission denied, boo! Disable the
            // functionality that depends on this permission.
            Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
        }
        return;
    }

    // other 'case' lines to check for other
    // permissions this app might request
}