我想上传一张图片在解析云服务器中的android。但我不能这样做。
我已经尝试了以下代码:
Drawable drawable = getResources().getDrawable(R.drawable.profilepic) ;
Bitmap bitmap = (Bitmap)(Bitmap)drawable()
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] data = stream.toByteArray();
ParseFile imageFile = new ParseFile("image.png", data);
imageFile.saveInBackground();
请让我知道我该怎么做。
我添加了一个赏金,为这个常见问题的找到最佳的最终代码
发布于 2014-05-30 01:25:44
在苦苦挣扎了几个小时之后,下面的代码段对我来说是有效的。
1.活动类的数据成员
Bitmap bmp;
Intent i;
Uri BmpFileName = null;
2.启动摄像头。目标是启动摄像机活动,并启动BmpFileName以存储对文件的引用
String storageState = Environment.getExternalStorageState();
if (storageState.equals(Environment.MEDIA_MOUNTED)) {
String path = Environment.getExternalStorageDirectory().getName() + File.separatorChar + "Android/data/" + this.getPackageName() + "/files/" + "Doc1" + ".jpg";
File photoFile = new File(path);
try {
if (photoFile.exists() == false) {
photoFile.getParentFile().mkdirs();
photoFile.createNewFile();
}
}
catch (IOException e)
{
Log.e("DocumentActivity", "Could not create file.", e);
}
Log.i("DocumentActivity", path);
BmpFileName = Uri.fromFile(photoFile);
i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, BmpFileName);
startActivityForResult(i, 0);
3.通过重写onActivityResult从摄像机输出读取内容。目标是让bmp变量赋值。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
try {
bmp = MediaStore.Images.Media.getBitmap( this.getContentResolver(), BmpFileName);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Myocode to display image on UI - You can ignore
if (bmp != null)
IV.setImageBitmap(bmp);
}
}
4.保存事件时
// MUST ENSURE THAT YOU INITIALIZE PARSE
Parse.initialize(mContext, "Key1", "Key2");
ParseObject pObj = null;
ParseFile pFile = null ;
pObj = new ParseObject ("Document");
pObj.put("Notes", "Some Value");
// Ensure bmp has value
if (bmp == null || BmpFileName == null) {
Log.d ("Error" , "Problem with image"
return;
}
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(CompressFormat.PNG, 100, stream);
pFile = new ParseFile("DocImage.jpg", stream.toByteArray());
try
{
pFile.save();
pObj.put("FileName", pFile);
pObj.save();
_mParse.DisplayMessage("Image Saved");
}
catch (ParseException e)
{
// TODO Auto-generated catch block
_mParse.DisplayMessage("Error in saving image");
e.printStackTrace();
}
//在我的案例中完成活动。你可以选择其他的finish();
所以这里是与其他的主要区别
如果它不起作用,请告诉我
发布于 2013-04-30 14:08:56
Parse.initialize(this, "applicationId", "clientKey");
byte[] data = "Sample".getBytes(); //data of your image file comes here
final ParseFile file = new ParseFile(data);
try {
file.save();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (file.isDirty()){
//exception or error message etc
}
else{
try {
ParseUser.logIn("username", "password"); //skip this if already logged in
} catch (ParseException e2) {
e2.printStackTrace();
}
ParseObject userDisplayImage = new ParseObject("UserDisplayImage");
user = ParseUser.getCurrentUser();
userDisplayImage.put("user", user); //The logged in User
userDisplayImage.put("displayImage", file); //The image saved previously
try {
userDisplayImage.save(); //image and user object saved in a new table. Check data browser
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//See how to retrieve
ParseQuery query = new ParseQuery("UserDisplayImage");
query.whereEqualTo("user", user);
try {
parseObject = query.getFirst();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ParseFile imageFile = null;
imageFile = parseObject.getParseFile("displayImage");
try {
byte[] imgData = imageFile.getData(); //your image data!!
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
发布于 2013-09-25 16:56:35
像这样使用它
//Convert Bitmap to Byte array --For Saving Image to Parse Db. */
Bitmap profileImage= "your bitmap";
ByteArrayOutputStream blob = new ByteArrayOutputStream();
profileImage.compress(CompressFormat.PNG, 0 /* ignored for PNG */,blob);
imgArray = blob.toByteArray();
//Assign Byte array to ParseFile
parseImagefile = new ParseFile("profile_pic.png", imgArray);
parseUser.getCurrentUser().put("columname in parse db", parseImagefile);
parseUser.getCurrentUser().saveInBackground();
我希望这能帮助你..
https://stackoverflow.com/questions/16292853
复制相似问题