我想将图片上传到parse.com。我从图库中选择图像并将其放在活动A中的ImageView中,如下所示:
addImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
};
然后,在OnActivityforResult:
@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) {
mMediaUri = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(mMediaUri,
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.imgView);
propertyImage.setImageBitmap(BitmapFactory.decodeFile(picturePath));
Bitmap bmp = BitmapFactory.decodeFile(picturePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byteArray = stream.toByteArray();
}
然后我通过Intent发送字节数组:
intent.putExtra("Image",byteArray);
现在在活动B中:
byte[] data = getIntent().getByteArrayExtra("Image");
final ParseFile file = new ParseFile("propic.png", data);
file.saveInBackground();
最后我发送它来解析如:
listedProperty.put("PropPic", file);
从那以后,数据没有进入我的parse类,我从parse得到一个不成功的回调。可能有什么不对?
logcat的:
03-14 04:14:23.987 1688-1688/com.iwillcode.realestate I/Choreographer﹕ Skipped 33 frames! The application may be doing too much work on its main thread.
03-14 04:14:25.808 1688-1717/com.iwillcode.realestate W/EGL_emulation﹕ eglSurfaceAttrib not implemented
03-14 04:14:25.808 1688-1717/com.iwillcode.realestate W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa24751e0, error=EGL_SUCCESS
03-14 04:14:34.204 1688-1717/com.iwillcode.realestate W/EGL_emulation﹕ eglSurfaceAttrib not implemented
03-14 04:14:34.204 1688-1717/com.iwillcode.realestate W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa2426fc0, error=EGL_SUCCESS
03-14 04:14:35.624 1688-1717/com.iwillcode.realestate W/EGL_emulation﹕ eglSurfaceAttrib not implemented
03-14 04:14:35.624 1688-1717/com.iwillcode.realestate W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa2475a20, error=EGL_SUCCESS
03-14 04:14:37.224 1688-1717/com.iwillcode.realestate W/EGL_emulation﹕ eglSurfaceAttrib not implemented
03-14 04:14:37.224 1688-1717/com.iwillcode.realestate W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa2b0b140, error=EGL_SUCCESS
03-14 04:14:39.432 1688-1717/com.iwillcode.realestate W/EGL_emulation﹕ eglSurfaceAttrib not implemented
03-14 04:14:39.432 1688-1717/com.iwillcode.realestate W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa2426060, error=EGL_SUCCESS
03-14 04:14:46.229 1688-1717/com.iwillcode.realestate W/EGL_emulation﹕ eglSurfaceAttrib not implemented
03-14 04:14:46.229 1688-1717/com.iwillcode.realestate W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa2475e20, error=EGL_SUCCESS
03-14 04:14:51.681 1688-1717/com.iwillcode.realestate W/EGL_emulation﹕ eglSurfaceAttrib not implemented
03-14 04:14:51.681 1688-1717/com.iwillcode.realestate W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa2b18f20, error=EGL_SUCCESS
03-14 04:14:53.801 1688-1717/com.iwillcode.realestate W/EGL_emulation﹕ eglSurfaceAttrib not implemented
03-14 04:14:53.801 1688-1717/com.iwillcode.realestate W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xacf8fde0, error=EGL_SUCCESS
03-14 04:14:55.425 1688-1717/com.iwillcode.realestate W/EGL_emulation﹕ eglSurfaceAttrib not implemented
03-14 04:14:55.425 1688-1717/com.iwillcode.realestate W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa0f727e0, error=EGL_SUCCESS
03-14 04:15:03.226 1688-1688/com.iwillcode.realestate E/No﹕ Unseccessfull
答案 0 :(得分:1)
不确定这是否有帮助,但我将我的位图图像保存为这样解析,这是在用相机拍照后。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
byte[] image_byte_array;
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
post_object = new Post();
Bundle extras = data.getExtras();
image = (Bitmap) extras.get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, stream);
image_byte_array = stream.toByteArray();
picture_file = new ParseFile("Picture", image_byte_array);
picture_file.saveInBackground();
post_object.put("Image", picture_file);
post_object.saveInBackground();
}
}
答案 1 :(得分:0)
我的项目中有类似的代码,我用Parse保存用户的图像并在Parse ImageView中显示,它可以工作:
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 filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
// put on my imageview
// ImagenParseUserNavigation.setImageBitmap(yourSelectedImage);
// ImagenParseUser.setImageBitmap(yourSelectedImage);
byte[] image = stream.toByteArray();
ParseUser currentUser = ParseUser.getCurrentUser();
ParseFile photoFile = new ParseFile("fotoperfil"+currentUser.getUsername()+".png",image);
currentUser.put("Imagen", photoFile);
currentUser.saveInBackground();
}
}