2016-05-26 7 views
0

私はここに似たような質問がたくさんあることを知っていますが、私のケースはかなりばかです。それとも私です。ギャラリーの画像がフラグメント内のImageViewに貼り付けられていません

だから、私はシンプルなレイアウト以下の持っている:

Layout mockup

XML構造:ボタンをクリックギャラリーで

<RelativeLayout> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="100dp" 
     android:id="@+id/image1" 
     android:contentDescription="Description" /> 

    <TextView/> 

    <android.support.design.widget.TabLayout /> 

    <android.support.v4.view.ViewPager /> 

    <Button /> 

</RelativeLayout> 

、開かれたユーザーは、いくつかの画像を選び、それがImageViewの中に置かれています

私は以下のコードでテストアクティビティを作成しました。完全に動作します:

public class TestActivity extends AppCompatActivity { 
    ImageView imageView; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_test); 

     imageView = (ImageView) findViewById(R.id.image1); 
     Button button = (Button) findViewById(R.id.button1); 

     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(Intent.ACTION_PICK); 
       intent.setType("image/*"); 

       startActivityForResult(intent, 10); 
      } 
     }); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (requestCode == 10 && resultCode == RESULT_OK) { 
      Uri uri = data.getData(); 
      imageView.setImageURI(uri); 
     } 

    } 
} 

マークアップ:このコードは動作しません、元のプロジェクトで

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    > 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="30dp" 
     android:id="@+id/image1" 
     android:layout_above="@+id/button1" 
     android:contentDescription="image" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Pick Image" 
     android:id="@+id/button1" 
     android:layout_gravity="center" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" /> 
</RelativeLayout> 

。ギャラリーでは、すべてのメソッドも同様に呼ばれて、速やかに呼び出されますが、イメージは単にImageViewのに貼り付けされていません。

:私はこれまでREAD_EXTERNAL_STORAGE(念のため)とスニペットを次のように使用しようとした

public class MainFragment extends Fragment { 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

... 

button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent chooserIntent = new Intent(Intent.ACTION_PICK); 
       chooserIntent.setType("image/*"); 

       startActivityForResult(chooserIntent, REQUEST_CODE_PICK_IMAGE); 
      } 
     }); 

} 

@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK) { 
      Uri uri = data.getData(); 
      imageView.setImageURI(uri); 

      Toast.makeText(getContext(), "Image is picked successfully", Toast.LENGTH_LONG).show(); 
     } 
} 

スニペット1

@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK) { 
      Uri uri = data.getData(); 

      try { 
       InputStream inputStream = getContext().getContentResolver().openInputStream(uri); 
       Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 
       imageView.setImageBitmap(bitmap); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     }  
} 

スニペット2

@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK) { 
      Uri uri = data.getData(); 

      try { 
       Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), uri); 
       imageView.setImageBitmap(bitmap); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     }  
} 

スニペット3

@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK) { 
      Uri uri = data.getData(); 

      try { 
       InputStream inputStream = getActivity().getContentResolver().openInputStream(uri); 
       Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 
       Drawable drawable = new BitmapDrawable(getResources(), bitmap); 
       imageView.setImageDrawable(drawable); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     }  
} 

スニペット4

@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK) { 
      Uri uri = data.getData(); 

      String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

      Cursor cursor = getContext().getContentResolver().query(uri, filePathColumn, null, null, null); 
      cursor.moveToFirst(); 

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      String picturePath = cursor.getString(columnIndex); 
      cursor.close(); 

      imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 
     }  
} 

まだありません結果。何が問題なのか分かりません。私はどんな助けにも非常に感謝しています。

はあなたに

答えて

0

に感謝し、突然、私は私のImageViewのがフラグメントであるとしてギャラリーは右の画像は、ピッキング後に立ち上げ、再作成されたときに、それが破壊されている

を投稿質問の30分後に、答えを見つけることができました。レクリエーション中にImageViewが初期値で描画され、選択された画像のUriが失われます。

解決策は、Uriをどこかに保存し、条件文でフラグメントを作成することです。何らかの形でフラグメントレクリエーションを防止する

私はそれが誰かにとって参考になることを願っています

関連する問題