2016-09-25 38 views
0

這部作品模擬器(我用genymotion),但由於某種原因,在真實設備上不會工作。 圖像視圖不會顯示圖像,我敢肯定,這不是空的,因爲它顯示路徑文件。這是代碼。當我啓動應用程序時,除了文件(圖像)沒有顯示在圖像視圖上以外,所有工作都很好。我試圖查看logcat,並且爲什麼圖像不會顯示沒有錯誤。謝謝回答圖片視圖不會顯示圖像setImageBitmap()

ImageView img_logo; 
    protected static final int CAMERA_REQUEST = 1; 
    protected static final int FILE_REQUEST = 2; 
    private Uri imageCaptureUri; 

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     view = inflater.inflate(R.layout.fragment_my_account, container, false); 
     try{ 
     firstname = getArguments().getString("firstname"); 
     lastname = getArguments().getString("lastname"); 
     username = getArguments().getString("username"); 
     cno = getArguments().getString("cno"); 
     email = getArguments().getString("email"); 
     address = getArguments().getString("address"); 
     userID = getArguments().getString("userID"); 

     fullname = (TextView) view.findViewById(R.id.name); 
     tv_uname = (TextView) view.findViewById(R.id.user_name); 
     tv_cno = (TextView) view.findViewById(R.id.user_cno); 
     tv_email = (TextView) view.findViewById(R.id.user_email); 
     tv_address = (TextView) view.findViewById(R.id.user_address); 

     //upload photo 
     startDialog(); 

     DisplayImage(); 
     fullname.setText(firstname + " " + lastname); 
     tv_uname.setText(username); 
     tv_cno.setText(cno); 
     tv_email.setText(email); 
     tv_address.setText(address); 

     }catch (Exception e){ 
      e.printStackTrace(); 
     } 

     return view; 
    } 

方法startDialog()

private void startDialog(){ 
     final String[] items = new String[] {"From Cam", "From SD Card"}; 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.select_dialog_item, items); 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     builder.setTitle("Choose Action: "); 
     builder.setAdapter(adapter, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       if (which == 0) { 
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
        File file = new File(Environment.getExternalStorageDirectory(), "tmp_avatar" + String.valueOf(System.currentTimeMillis()) 
          + ".jpg"); 
        imageCaptureUri = Uri.fromFile(file); 
        try { 
         intent.putExtra(MediaStore.EXTRA_OUTPUT, imageCaptureUri); 
         intent.putExtra("return data", true); 

         startActivityForResult(intent, CAMERA_REQUEST); 
        } catch (Exception ex) { 
         ex.printStackTrace(); 
        } 
        dialog.cancel(); 
       } else { 
        Intent intent = new Intent(); 
        intent.setType("image/*"); 
        intent.setAction(Intent.ACTION_GET_CONTENT); 
        startActivityForResult(Intent.createChooser(intent, "Complete Action using"), FILE_REQUEST); 
       } 
      } 
     }); 
     final AlertDialog dialog = builder.create(); 
     //image 
     txt_image_path = (TextView) view.findViewById(R.id.image_path); 
     img_logo = (ImageView) view.findViewById(R.id.display_image); 
     img_logo.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       dialog.show(); 
      } 
     }); 
    } 

上的活動結果()

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

     if(resultCode != Activity.RESULT_OK) 
      return; 
     Bitmap bitmap = null; 
     String path = ""; 
     if(requestCode == FILE_REQUEST){ 
      imageCaptureUri = data.getData(); 
      path = getRealPathFromURI(imageCaptureUri); 
      if(path != null){ 
       bitmap = BitmapFactory.decodeFile(path); 
      }else{ 
       path = imageCaptureUri.getPath(); 
      } 

     }else { 
      path = imageCaptureUri.getPath(); 
      bitmap = BitmapFactory.decodeFile(path); 

     } 

     img_logo.setImageBitmap(bitmap); 
     txt_image_path.setText(path); 

    } 
+0

可能的複製[ImageView不會顯示圖像whe N乘setImageBitmap設置()](http://stackoverflow.com/questions/6538843/imageview-wont-show-image-when-set-by-setimagebitmap) – Danieboy

+0

使用畢加索從URL加載烏爾圖像。從havig中拯救自己,寫下大量的樣板代碼。 http://square.github.io/picasso/ –

+0

我沒有加載從URL的圖像,該圖像是在手機外部存儲或相機。 –

回答

0

試試這個修改後的代碼在logcat中發現的錯誤

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

    if(resultCode != Activity.RESULT_OK) 
     return; 
    Bitmap bitmap = null; 
    String path = ""; 
    if(requestCode == FILE_REQUEST){ 
     imageCaptureUri = data.getData(); 
     path = getRealPathFromURI(imageCaptureUri); 
     if(path != null){ 
      bitmap = BitmapFactory.decodeFile(path); 
     }else{ 
      path = imageCaptureUri.getPath(); 
     } 

    }else if(requestCode == CAMERA_REQUEST && 
    resultCode == RESULT_OK) { 

    try 
    {path = imageCaptureUri.getPath(); 
     bitmap = BitmapFactory.decodeFile(path); 
     img_logo.setImageBitmap(bitmap); 
     txt_image_path.setText(path); 
     } 
     catch (NullPointerException e) { 
      e.printStackTrace(); 

    } 



    }