我试图让用户将他们的图片(个人资料图片)上传到他们的个人资料创建页面的解析中,他们必须填写各种信息。用户可以在图像视图中预览他们的图片。
我试图通过以下步骤来实现这一目标。
1)创建一个按钮,允许用户从他们的图库中检索图片,并将其上传到创建的ImageView中。
Button buttonLoadImage = (Button) findViewById(R.id.btnPictureSelect);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
此部分通常已成功执行,用户可以从图库中上传图像并在图像视图中以可视方式显示图像。
2)在解析时存储用户上传的图像 这是我正在努力的领域。在代码之间添加了评论以进一步澄清,并强调我的问题是。
ParseUser currentUser = ParseUser.getCurrentUser();
/* This is the section where the images is converted, saved, and uploaded. I have not been able Locate the image from the ImageView, where the user uploads the picture to imageview from either their gallery and later on from facebook */
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
/*to be retrieved from image view */);
// 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();
// Create the ParseFile
ParseFile file = new ParseFile("profilePicture.png", image);
// Upload the image into Parse Cloud
// Create a column named "Profile Picture" and set the string
currentUser.put("ImageName", "Profile Picture");
// Create a column named "ImageFile" and insert the image
currentUser.put("ProfilePicture", file);
// Create the class and the columns
currentUser.put("name", name);
currentUser.put("age", age);
currentUser.put("headline", headline);
currentUser.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
setProgressBarIndeterminateVisibility(false);
if (e == null) {
// Success!
Intent intent = new Intent(ProfileCreation.this, MoodActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(ProfileCreation.this);
builder.setMessage(e.getMessage())
.setTitle(R.string.signup_error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
}
});
如果你能帮助我,那将是非常有帮助的。 如果您需要进一步说明,请与我们联系。 提前谢谢。
答案 0 :(得分:1)
您忘记先保存文件
ParseFile file = new ParseFile("profilePicture.png", image);
file.saveinBackground()
答案 1 :(得分:0)
您可以定义一个名为util.java的新类,在该类中您可以编写一个方法
public static byte[] getbytearray(Bitmap bm){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
现在你不需要一次又一次地使用这些基本行。 更重要的是你应该使用asynctask 避免内存不足错误喜欢 -
public class storeimage extends AsyncTask<Bitmap,Void,Void>{
@Override
protected Void doInBackground(Bitmap... params) {
byte[] byteArray = util.getbytearray(params[0]);
ParseUser user = ParseUser.getCurrentUser();
ParseFile parseFile = new ParseFile(user.getUsername()+"dp.png",byteArray);
parseFile.saveInBackground();
user.put("dp", parseFile);
user.saveInBackground();
Log.d("mayank","asynctask successful");
// Bitmap dpbitmap2 = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
//ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
// dpbitmap.compress(Bitmap.CompressFormat.PNG, 0, stream2);
// byte[] byteArray2 = stream2.toByteArray();
// ParseFile parseFile2 = new ParseFile(user.getUsername()+"dp_circle.png",byteArray2);
// ParseObject dp = new ParseObject("dp");
// dp.put("pic",byteArray2);
// dp.pinInBackground();
return null;
}
}
之后这个 你应该写
new storeimage().execute(BitmapFactory.decodeFile(picturePath));