2017-03-25 52 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級後,有必要問手動權限。

相關問題