2017-12-04 18 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をフリーズすることになります。

関連する問題