2017-04-03 94 views
-4

我打開畫廊從我的片段之一,但後我從畫廊選擇圖像 圖像不顯示給我的視野。顯示圖像畫廊在片段不工作

onActivityResult我在這行的rootview中有錯誤: ImageView imgView =(ImageView)rootview.findViewById(R.id.imgView); 和我測試吐司給我看所選的圖像路徑,但它不顯示我的路徑 。

這裏是我的代碼片段:

public class Share_Page extends Fragment implements View.OnClickListener { 

    private static final int RESULT_OK = 1; 
    String path=""; 
    String imgPath, fileName; 
    private Button home_page,search_page; 
    private static int RESULT_LOAD_IMG = 1; 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     View rootview = inflater.inflate(R.layout.share, container, false); 
     home_page = (Button) rootview.findViewById(R.id.home_page); 
     search_page = (Button) rootview.findViewById(R.id.search_page); 


     rootview.findViewById(R.id.buttonLoadPicture).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       loadImagefromGallery(); 
      } 
     }); 




     btn_click(); 

     return rootview; 
    } 
    public void loadImagefromGallery() { 
     Intent galleryIntent = new Intent(Intent.ACTION_PICK, 
       android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     startActivityForResult(galleryIntent, RESULT_LOAD_IMG); 
    } 

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

      if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK 
        && null != data) { 
       Uri selectedImage = data.getData(); 
       String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

       Cursor cursor = getActivity().getContentResolver().query(selectedImage, 
         filePathColumn, null, null, null); 
       cursor.moveToFirst(); 

       int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
       imgPath = cursor.getString(columnIndex); 
       cursor.close(); 
       //ImageView imgView = (ImageView) findViewById(R.id.imgView); 
       ImageView imgView = (ImageView) rootview.findViewById(R.id.imgView); 
       imgView.setImageBitmap(BitmapFactory 
         .decodeFile(imgPath)); 
       String fileNameSegments[] = imgPath.split("/"); 
       fileName = fileNameSegments[fileNameSegments.length - 1]; 
       path=imgPath; 

      } else { 
       Toast.makeText(getActivity(), "image not select", 
         Toast.LENGTH_LONG).show(); 
       imgPath="2"; 
      } 
     } catch (Exception e) { 
      Toast.makeText(getActivity(), "error`enter code here`...!", Toast.LENGTH_LONG) 
        .show(); 
     } 
    } 
+0

你給我的是否定的投票?它不是重複的問題 – ali

+0

定義ImageView imgView =(ImageView)rootview.findViewById(R.id.imgView);在onCreateView方法和從onActivityResult刪除 – sunita

+0

我這樣做,但它未知的onActivityResult – ali

回答

0

此方法是故意開畫廊,並選擇圖像:

public void loadImagefromGallery(View view) { 
     Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     startActivityIfNeeded(galleryIntent, RESULT_LOAD_IMG); 
    } 

這裏是結果的被拍攝後。正如你看到我用形象改變的背景下,美國可以把它放到ImageView的或什麼都u想

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     try { 
      if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK 
        && null != data) { 
       Uri selectedImage = data.getData(); 
       String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
       Cursor cursor = getContentResolver().query(selectedImage, 
         filePathColumn, null, null, null); 
       assert cursor != null; 
       cursor.moveToFirst(); 
       int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
       String imgDecodableString = cursor.getString(columnIndex); 
       cursor.close(); 
       Drawable d = new BitmapDrawable(getResources(), imgDecodableString); 
       relativeLayout.setBackground(d); 
      } else { 
       Toast.makeText(this, "You haven't picked Image", 
         Toast.LENGTH_LONG).show(); 
      } 
     } catch (Exception e) { 
      Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG) 
        .show(); 
     } 
} 

也請記得在清單

實例添加權限如何使用權限運行,將其粘貼到活動

private static final int REQUEST_CODE_EXTERNAL_STORAGE = 1; 
    private static final int REQUEST_CODE_CAMERA = 2; 
private static int RESULT_LOAD_IMG = 1; 
@TargetApi(23) 
    public void checkCameraPermission(){ 
     if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M){ 
      return; 
     } 
     if (this.checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { 
      requestPermissions(new String[] {Manifest.permission.CAMERA}, REQUEST_CODE_CAMERA); 
     } 
    } 

    @TargetApi(23) 
    public void checkStoragePermission() { 
     if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { 
      return; 
     } 
     if (this.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager 
       .PERMISSION_GRANTED) { 
      requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 
        REQUEST_CODE_EXTERNAL_STORAGE); 
     } 
    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] 
      grantResults) { 
     switch (requestCode) { 
      case REQUEST_CODE_EXTERNAL_STORAGE: 
       if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        Toast.makeText(this, "Thanks for your permission", LENGTH_SHORT).show(); 
       } else { 
        Toast.makeText(this, "We need your permission to save image", 
          LENGTH_SHORT).show(); 
       } 
       break; 
      case REQUEST_CODE_CAMERA: 
       if(grantResults[0] == PackageManager.PERMISSION_GRANTED){ 
        Toast.makeText(this, "Thanks for your permission", LENGTH_SHORT).show(); 
       } else { 
        Toast.makeText(this, "We need your permission to start SOS", 
          LENGTH_SHORT).show(); 
       } 
      default: 
       super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
       break; 
     } 
} 

如果ü要檢查片段權限ü應該這樣做:

((Activity) getContext()).checkAnyPermission(); 

你應該在你打開一個圖庫後檢查這個

+0

<使用權限android:name =「android.permission.WRITE_EXTERNAL_STORAGE」/> <使用權限android:name =「android.permission.READ_EXTERNAL_STORAGE」/> – Rodriquez

+0

我使用你的代碼,但在這一行 relativeLayout.setBackground d); 顯示我爲relativeLayout的錯誤 – ali

+0

而不是relativeLayout我應該添加imgView? – ali