2016-09-17 91 views
0

我想創建一個具有功能的片段,就好像我按下了一個按鈕,然後畫廊將被打開,然後我選擇一個圖像,並將該圖像放入我的圖像視圖中。如何從圖片庫中獲取圖片?

我寫的代碼,

public class GreenFragment extends Fragment { 
    ImageView mImageview; 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_green, container, false); 
     mImageview = (ImageView) view.findViewById(R.id.iv_beforesendpost); 

     view.findViewById(R.id.btn_getgallery).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       int permissionCheck = ContextCompat.checkSelfPermission(getActivity(), 
         Manifest.permission.READ_EXTERNAL_STORAGE); 

       if (permissionCheck == PackageManager.PERMISSION_GRANTED) { 
        startGallery(); 
       } else { 
        ActivityCompat.requestPermissions(getActivity(), 
          new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 
          2000); 
       } 
      } 
     }); 
     return view; 
    } 

    private void startGallery() { 
     Intent cameraIntent = new Intent(Intent.ACTION_GET_CONTENT); 
     cameraIntent.setType("image/*"); 
     if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) { 
      startActivityForResult(cameraIntent, 1000); 
     } 
    } 

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

     if (requestCode == 1000 && resultCode == Activity.RESULT_OK) { 

     } 
     Uri returnUri; 
     returnUri = data.getData(); 

     Glide.with(this) 
       .load(returnUri) 
       .override(1280, 1280) 
       .centerCrop() 
       .crossFade() 
       .into(mImageview); 

但是,當我點擊Button (btn_gatgallery),什麼都沒有發生。

我的錯在哪裏?請告訴我。

+1

你得到任何錯誤?如果是這樣發佈錯誤日誌。 – RamithDR

回答

3
view.findViewById(R.id.btn_getgallery).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       if(ActivityCompat.checkSelfPermission(getActivity(), 
         Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) 
       { 
        requestPermissions(
          new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 
          2000); 
       } 
       else { 
        startGallery(); 
       } 
      } 
     }); 
     return view; 
    } 

添加

Intent cameraIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 

手柄請求

@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    //super method removed 
    if(resultCode == RESULT_OK) { 
    if(requestCode == 1000){ 
     Uri returnUri = data.getData(); 
     Bitmap bitmapImage = MediaStore.Images.Media.getBitmap(getActivity.getContentResolver(), returnUri); 
     your_imageView.setImageBitmap(bitmapImage); 
    } 
    } 
    //Uri returnUri; 
    //returnUri = data.getData(); 
+0

謝謝。 W4r10ck –

+0

Hail Hydra。 welocme – W4R10CK