2017-03-25 53 views
0

我想在图像视图中添加图片,并在一行中显示文本。此代码正在打开画廊并让我选择一张照片。但是当图片被选中时,图片并未在图像中显示。我把代码从这个链接 [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> 

回答

0

我想你忘记权限读取外部存储。

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

我没有忘记添加它.. – khanzada

+0

您可以使用Log.d(“TAG”,“picturePath:”+ picturePath); 如果日志不显示,您可以检查构建版本代码,Android版本M或更高要求的权限读取外部存储。 如果日志显示,您可以检查绝对路径图像并搜索“从外部存储装载位图”。 我希望我的回答能帮助你! – Dungnbhut

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级后,有必要问手动权限。

相关问题