2017-03-25 11 views
0

私はテキストビューveiwを1行に入れて画像ビューに画像を追加したいと思います。このコードはギャラリーを開き、画像を選択させます。画像が選択されると画像は画像内に表示されません。私は、このリンクからコードを取っ [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

です外部ストレージを読み込みます。

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

    ActivityCompat.requestPermissions(MainActivity.this, 
       new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 
       1); 

} はそれの後に上書き:

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

私はそれを追加することを忘れなかった。 – khanzada

+0

Log.d( "TAG"、 "picturePath:" + picturePath); ログが表示されない場合は、ビルドバージョンコード、AndroidバージョンM以上の要件のアクセス許可を外部記憶装置から読み取ることができます。 ログが表示されている場合は、絶対パスイメージを確認し、「外部ストレージからビットマップをロード」を検索できます。 私の答えがあなたを助けてくれることを願っています! – Dungnbhut

0

あなたが手動許可を求める必要がある

@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の後には、を求めることが必要です手動で許可してください。

関連する問題