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

时间:2017-03-25 17:58:56

标签: java android

我想在图像视图中添加图片,文本视图在一行中。这段代码打开了画廊,让我选择一张图片。但是当选择图片时,图片就不会显示在图像视图中。我从这个链接中获取了代码 [Image browse button in android activity

这是我的代码

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

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

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null   

       != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        ImageView imageView = (ImageView) findViewById(R.id.image);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }


       }

这是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>

2 个答案:

答案 0 :(得分:0)

我认为您忘记了阅读外部存储空间。

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

答案 1 :(得分:0)

您需要申请手动许可:

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之后,需要手动请求权限。