2017-12-04 5 views
1
private static int RESULT_LOAD = 1; 
String img_Decodable_Str; 


ImageView imageView = (ImageView) findViewById(R.id.Gallery); 
    imageView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent galleryIntent = new Intent(Intent.ACTION_PICK, 
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
      // Start the Intent 
      startActivityForResult(galleryIntent, RESULT_LOAD); 

     } 
    });} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    try { 
     // When an Image is picked 
     if (requestCode == RESULT_LOAD && resultCode == RESULT_OK 
       && null != data) { 
      // Get the Image from data 

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

      // Get the cursor 
      Cursor cursor = getContentResolver().query(selectedImage, 
        filePathColumn, null, null, null); 
      // Move to first row 
      cursor.moveToFirst(); 

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      img_Decodable_Str = cursor.getString(columnIndex); 
      cursor.close(); 
      ImageView imageView = (ImageView) findViewById(R.id.Gallery); 
      // Set the Image in ImageView after decoding the String 
      imageView.setImageBitmap(BitmapFactory 
        .decodeFile(img_Decodable_Str)); 

     } else { 
      Toast.makeText(this, "Hey pick your image first", 
        Toast.LENGTH_LONG).show(); 
     } 
    } catch (Exception e) { 
     Toast.makeText(this, "Something went embrassing", Toast.LENGTH_LONG) 
       .show(); 
    } 

}} 

나는 ImageView입니다. 사용자가 ImageView을 클릭하면 선택한 이미지를 이미지에 추가 할 수 있습니다. 을 클릭하면 ImageView 갤러리로 리디렉션되지만 이미지를 선택하면 이미지가 ImageView에 표시되지 않습니다. 내가 어디로 잘못 갔니?갤러리에서 이미지를 선택하고 ImageView를 사용하여 이미지를 표시하십시오.

단기적으로

답변

0

대체 :

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

     // Get the cursor 
     Cursor cursor = getContentResolver().query(selectedImage, 
       filePathColumn, null, null, null); 
     // Move to first row 
     cursor.moveToFirst(); 

     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     img_Decodable_Str = cursor.getString(columnIndex); 
     cursor.close(); 
     ImageView imageView = (ImageView) findViewById(R.id.Gallery); 
     // Set the Image in ImageView after decoding the String 
     imageView.setImageBitmap(BitmapFactory 
       .decodeFile(img_Decodable_Str)); 

로 :

 Uri selectedImage = data.getData(); 
     imageView.setImageURI(selectedImage); 

나중에

가 메인에 setImageURI()로드 이미지로, 피카소 나 글라이드 같은 이미지 로딩 라이브러리를 사용 그 응용 프로그램 스레드는 해당 작업이 수행되는 동안 UI를 고정시킵니다.

관련 문제