我是一名网络开发者,几周前我开始尝试学习android studio和app dev。我目前正在使用kii cloud为我的Android应用程序,我目前正在试图弄清楚如何实现以下场景 -
一个。我已经有用户注册,所以我目前正在编写一个"编辑个人资料"页。
湾在编辑个人资料页面上,我尝试创建一个按钮,以便用户点击它,然后它将打开一个用户可以上传图像的片段。 link
℃。然后将该图像附加到网址,并在需要的位置使用URL,例如视图配置文件页面。链接enter link description here
EditUserProfile.java
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);
}
}
ProfilePic.java(片段)
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 && 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 >= 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 < 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<?> 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 && fragment.isInLayout()) {
fragment.setProgressBarVisiblity(v);
}
}
void setPageImage(int page) {
ProgressFragment fragment = (ProgressFragment) getFragmentManager()
.findFragmentById(R.id.progressFragment);
if (fragment != null && fragment.isInLayout()) {
fragment.setPageImage(page);
}
}
}
然后对于这样的事情,我试图找到一种方法来动态显示与用户关联的个人资料图片,如果不是默认使用图标。
holder.icon.setImageResource(R.drawable.list_account);
UserListLoader.java
public class UserListAdapter extends AbstractArrayAdapter<IUser> {
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;
}
}
同样在app中打开时,EditUserProfile崩溃了 picbutton.setOnClickListener(new View.OnClickListener()
对此事项的任何指导或帮助表示赞赏。谢谢。
答案 0 :(得分:0)
我希望这有助于某人...
public void uploadImage(){
UserFields userFields = new UserFields();
userFields.putDisplayName("AllDeals");
KiiUser.registerAsPseudoUser(userFields,
new KiiUserRegisterCallback() {
@Override
public void onRegisterCompleted(KiiUser kiiUser,
Exception exception) {
// TODO Auto-generated method stub
if (exception != null) {
// Error handling
Log.e("Exception", exception.toString());
return;
} else {
String accessToken = kiiUser.getAccessToken();
final KiiObject object = Kii.bucket("AllDeals")
.object();
object.set("placeImage", imageURL);
File localFile = new File(picturePath);
KiiUploader uploader = object.uploader(
getApplicationContext(), localFile);
uploader.transferAsync(new KiiRTransferCallback() {
public void onTransferCompleted(
KiiRTransfer operator,
Exception exception) {
if (exception != null) {
return;
}
object.publishBody(new KiiObjectPublishCallback() {
@Override
public void onPublishCompleted(
String url,
KiiObject object,
Exception exception) {
if (exception != null) {
return;
}
object.set("url", url);
object.save(new KiiObjectCallBack() {
public void onSaveCompleted(
int token,
KiiObject object,
Exception exception) {
if (exception != null) {
return;
}
Toast toast = Toast
.makeText(
MainActivity.this,
"Your image is added successfully",
500);
};
});
}
});
};
});
}
});
}