2016-05-11 41 views
0

画像を画像ビューで設定する必要があるため、画像を選択しましたが、画像ビューでは設定しません。選択したギャラリー画像が画像表示に表示されません

関連コードを掲載しました。

EditViewProfileActivity.java私はimageview.Anyoneに表示されない選択した画像はthis.Thankあなたと私を助けることができる理由を知らない

public class EditViewProfileActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener { 

    private static final int CAMERA_PICTURE = 1; 
    private static final int GALLERY_PICTURE = 2; 
    File destination = null; 
    String filePath = null; 
    Bitmap chosenImage; 
    String imgSelected = null; 

ImageView dateBirthImg; 


@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.edit_view_profile_activity); 

    galImageView = (ImageView) findViewById(R.id.gallery_image_edit_view); 

galImageView.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       choosePictureAction(); 
      } 
     }); 

} 

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



      if (requestCode == GALLERY_PICTURE 
       && resultCode == RESULT_OK) { 
      Uri selectedImageUri = data.getData(); 
      String[] projection = {MediaStore.MediaColumns.DATA}; 
      Cursor cursor = getContentResolver().query(selectedImageUri, projection, null, null, null); 
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); 
      cursor.moveToFirst(); 
      String selectedImagePath = cursor.getString(column_index); 
      destination = new File(selectedImagePath); 
      filePath = selectedImagePath; 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inJustDecodeBounds = true; 
      BitmapFactory.decodeFile(selectedImagePath, options); 
      final int REQUIRED_SIZE = 200; 
      int scale = 1; 
      while (options.outWidth/scale/2 >= REQUIRED_SIZE 
        && options.outHeight/scale/2 >= REQUIRED_SIZE) 
       scale *= 2; 
      options.inSampleSize = scale; 
      options.inJustDecodeBounds = false; 
      chosenImage = BitmapFactory.decodeFile(selectedImagePath, options); 


      if (chosenImage != null) { 
       imgSelected = "fulFilled"; 
       galImageView.setImageBitmap(chosenImage); 

       Log.e("ChosenImage", "" + chosenImage); 

      } 


     } else if (requestCode == GALLERY_PICTURE 
       && resultCode == RESULT_CANCELED) { 

     } 
    } 


    private void choosePictureAction() { 

     final CharSequence[] items = {"Camera", "Gallery", "Cancel"}; 
     android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(EditViewProfileActivity.this); 
     builder.setTitle("Add Photo!"); 
     builder.setItems(items, new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 

       if (items[which].equals("Camera")) { 
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
        startActivityForResult(intent, CAMERA_PICTURE); 
       } else if (items[which].equals("Gallery")) { 
        Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
        startActivityForResult(intent, GALLERY_PICTURE); 
       } else if (items[which].equals("Cancel")) { 
        dialog.dismiss(); 
       } 

      } 
     }); 

     builder.show(); 

    } 


} 

+0

をあなたを助けるために喜んhttp://stackoverflow.com/a/37130472/6097062 –

答えて

1

と同じように、私はこのコードを使用している:

private void openGallery() { 
    Intent gallery = 
      new Intent(Intent.ACTION_PICK, 
        android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI); 
    startActivityForResult(gallery, PICK_IMAGE); 
} 



@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) { 
      Uri imageUri = data.getData(); 

      try { 
       bitmaps = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri); 

       final ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
       bitmaps.compress(Bitmap.CompressFormat.PNG, 90, stream); 
       byte[] byteArray = stream.toByteArray(); 

       encodeded = Base64.encodeToString(byteArray, Base64.DEFAULT); 

       byte[] decodedString = Base64.decode(encodeded, Base64.DEFAULT); 
       Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
       userinfoimage.setImageBitmap(bitmaps); 

       new UploadImage().execute(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 


     } 
    } 
+0

@user 11を参照してください。 –

1

イメージビューでのみイメージを設定する場合。あなたは直接uriを使用することができます。この

String stringUri = selectedImageUri.getPath(); 
galImageView.setImageURI(null); 
galImageView.setImageURI(Uri.parse(stringUri)); 
関連する問題