2014-11-06 173 views
0

編輯:解決。無法找到上傳圖像解析

我試圖上傳一個圖像來解析,從手機的畫廊中選擇。但是,我似乎無法找到imageview,因爲它返回null。我認爲這個頁面:Saving images to Parse與我所問的問題有類似的問題,但沒有答案。

片段類:

public void onClick(View view) { 
      img = (ImageView) rootView.findViewById(R.id.imageView1); 
      // Locate the image (error is here) 
      Bitmap bitmap = BitmapFactory.decodeResource(getActivity().getResources(), img); 
      // Convert it to byte 
      ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
      // Compress image to lower quality scale 1 - 100 
      bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 
      byte[] image = stream.toByteArray(); 


      //byte[] image = img.getContext().toString().getBytes(); 

      //Bitmap bitmap = BitmapFactory.decodeFile(img); 

      ParseFile file = new ParseFile("image.jpg", image); 
      file.saveInBackground(); 
      ParseObject familyTree = new ParseObject("FamilyTree"); 
      familyTree.put("image", file); 
      familyTree.put("name", etName.getText().toString()); 
      familyTree.put("relation", etRelation.getText().toString()); 
      familyTree.saveInBackground(); 
      Toast.makeText(FamilyTreeFragment2.this.getActivity(), "Saved.", Toast.LENGTH_SHORT).show(); 
      // getFragmentManager().beginTransaction().replace(R.id.container, new FamilyTreeFragment2()).addToBackStack(null).commit(); 
     } 

任何幫助表示讚賞。謝謝。

回答

0

我已經設法找到解決方案。

替換

Bitmap bitmap = BitmapFactory.decodeResource(getActivity().getResources(), img); 

Bitmap bitmap = ((BitmapDrawable) img.getDrawable()).getBitmap();