title: KiiObject Upload Picture to use for profile
body: <p>I am a web dev and a couple of weeks ago I started trying to learn android studio and app dev. Im currently using <a href="http://documentation.kii.com/en/guides/android/" rel="nofollow">kii cloud</a> for my android app and I am currently trying to figure out how implement the following scenario --</p>
  
  <p>a. I already have user registration, so I am currently working on a "edit profile" page.</p>
  
  <p>b. On the edit profile page Im trying to create a button so that the user clicks on it and it will then open a fragment where the user can upload the image. <a href="http://documentation.kii.com/en/guides/android/managing-users/user-attributes/" rel="nofollow">link</a></p>
  
  <p>c. Then attach that image to the url, and use the URL where needed such as a view profile page. link<a href="http://documentation.kii.com/en/guides/android/managing-data/object-storages/" rel="nofollow">enter link description here</a></p>
  
  <p><strong>EditUserProfile.java</strong></p>
  
  <pre><code>public class EditUserProfile extends Activity {
      Button picbutton;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main3);
          picbutton = (Button) findViewById(R.id.MypicButton);
  
          // Capture button clicks
          picbutton.setOnClickListener(new View.OnClickListener() {
              public void onClick(View arg0) {
  
          FragmentManager fm = getFragmentManager();
          Fragment fragment = fm.findFragmentById(R.id.fragcon);
          if(fragment == null)
              fragment = new ProfilePic();
          FragmentTransaction ft = getFragmentManager().beginTransaction();
          ft.replace(R.id.fragcon, fragment);
          ft.commit();
              }
          });
      }
      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
      }
  }
  </code></pre>
  
  <p><strong>ProfilePic.java (Fragment)</strong></p>
  
  <pre><code>public class ProfilePic extends Fragment {
      private static final String TAG = "ProfilePic";
      String objectUri = null;
      private static final int PICK_IMAGE = 1;
      private Activity activity;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_attach_file, container,
                  false);
          Bundle args = getArguments();
          objectUri = args.getString("object_uri");
          Button attachButton = (Button) view
                  .findViewById(R.id.attach_file_button);
          attachButton.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                  onAttachFileButtonClicked(v);
              }
          });
          setPageImage(3);
          ImageView imageView = (ImageView) view.findViewById(R.id.details);
          imageView.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
  
              }
          });
          return view;
      }
  
      @Override
      public void onAttach(Activity activity) {
          super.onAttach(activity);
          this.activity = activity;
      }
  
      @Override
      public void onDetach() {
          super.onDetach();
          this.activity = null;
      }
  
      public void onAttachFileButtonClicked(View v) {
          Intent intent = new Intent();
          intent.setType("image/*");
          intent.setAction(Intent.ACTION_GET_CONTENT);
          startActivityForResult(
                  Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
      }
  
      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          if (requestCode == PICK_IMAGE &amp;&amp; resultCode == Activity.RESULT_OK) {
              Uri selectedFileUri = data.getData();
              String filePath = getFilePathByUri(selectedFileUri);
              Log.v(TAG, "Picture Path : " + filePath);
              if (filePath == null) {
                  showAlert("File not exists, Please select an image that exists locally.");
                  return;
              }
              uploadFile(filePath);
          } else {
              showToast("picking file failed!");
          }
      }
  
      private void showToast(String message) {
          Toast.makeText(this.activity, message, Toast.LENGTH_SHORT).show();
      }
  
      private String getFilePathByUri(Uri selectedFileUri) {
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.JELLY_BEAN_MR1) {
              // Workaround of retrieving file image through ContentResolver
              // for Android4.2 or later
              String filePath = null;
              FileOutputStream fos = null;
              try {
                  Bitmap bmp = MediaStore.Images.Media.getBitmap(
                          this.activity.getContentResolver(), selectedFileUri);
  
                  String cacheDir = Environment.getExternalStorageDirectory()
                          .getAbsolutePath() + File.separator + "app";
                  File createDir = new File(cacheDir);
                  if (!createDir.exists()) {
                      createDir.mkdir();
                  }
                  filePath = cacheDir + File.separator + "upload.jpg";
                  File file = new File(filePath);
  
                  fos = new FileOutputStream(file);
                  bmp.compress(CompressFormat.JPEG, 95, fos);
                  fos.flush();
                  fos.getFD().sync();
              } catch (Exception e) {
                  filePath = null;
              } finally {
                  if (fos != null) {
                      try {
                          fos.close();
                      } catch (Exception e) {
                          // Nothing to do
                      }
                  }
              }
              return filePath;
          } else {
              String[] filePathColumn = { MediaStore.MediaColumns.DATA };
              Cursor cursor = this.activity.getContentResolver().query(
                      selectedFileUri, filePathColumn, null, null, null);
  
              if (cursor == null)
                  return null;
              try {
                  if (!cursor.moveToFirst())
                      return null;
                  int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                  if (columnIndex &lt; 0) {
                      return null;
                  }
                  String picturePath = cursor.getString(columnIndex);
                  return picturePath;
              } finally {
                  cursor.close();
              }
          }
      }
  
      private void uploadFile(String path) {
          KiiObject object = KiiObject.createByUri(Uri.parse(objectUri));
          File f = new File(path);
          Log.v(TAG, "file can read : " + f.canRead());
          KiiUploader uploader = object.uploader(this.activity, f);
          uploader.transferAsync(new KiiRTransferCallback() {
  
              @Override
              public void onStart(KiiRTransfer operator) {
                  setFragmentProgress(View.VISIBLE);
              }
  
              @Override
              public void onTransferCompleted(KiiRTransfer operator, Exception e) {
                  setFragmentProgress(View.INVISIBLE);
                  if (e == null) {
  
                  } else {
  
                  }
              }
          });
      }
  
      public void moveFromDialogFragment(Class&lt;?&gt; clazz) {
          if (clazz != null) {
              Intent i = new Intent(this.activity, clazz);
              startActivity(i);
          }
      }
  
      void showAlert(String message) {
          DialogFragment newFragment = AlertDialogFragment.newInstance(
                  R.string.operation_failed, message, null);
          newFragment.show(getFragmentManager(), "dialog");
      }
  
      void setFragmentProgress(int v) {
          ProgressFragment fragment = (ProgressFragment) getFragmentManager()
                  .findFragmentById(R.id.progressFragment);
          if (fragment != null &amp;&amp; fragment.isInLayout()) {
              fragment.setProgressBarVisiblity(v);
          }
      }
  
      void setPageImage(int page) {
          ProgressFragment fragment = (ProgressFragment) getFragmentManager()
                  .findFragmentById(R.id.progressFragment);
          if (fragment != null &amp;&amp; fragment.isInLayout()) {
              fragment.setPageImage(page);
          }
      }
  }
  </code></pre>
  
  <p>Then for things like this, Im trying to find a way to dynamically show the profile pictures associated with the users, and if not use the icon as default. </p>
  
  <pre><code>holder.icon.setImageResource(R.drawable.list_account);
  </code></pre>
  
  <p><strong>UserListLoader.java</strong></p>
  
  <pre><code>public class UserListAdapter extends AbstractArrayAdapter&lt;IUser&gt; {
  
      private final LayoutInflater inflater;
  
      public UserListAdapter(Context context) {
          super(context, R.layout.image_list_item);
          this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      }
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          ViewHolder holder;
          if (convertView == null) {
              convertView = this.inflater.inflate(R.layout.image_list_item, parent, false);
              holder = new ViewHolder();
              holder.text = (TextView)convertView.findViewById(R.id.row_text);
              holder.icon = (ImageView)convertView.findViewById(R.id.row_icon);
              convertView.setTag(holder);
          } else {
              holder = (ViewHolder)convertView.getTag();
          }
  
          IUser user = this.getItem(position);
          holder.position = position;
          holder.text.setText(user.getUsername());
          holder.icon.setImageResource(R.drawable.list_account);
          return convertView;
      }
  
  }
  </code></pre>
  
  <p>Also when opening in app, the EditUserProfile crashes at 
     <strong>picbutton.setOnClickListener(new View.OnClickListener()</strong> </p>
  
  <p>Any guidance or help on the matter is appreciated. Thanks. </p>