当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Saving Image on Parse.com

问题描述:

I want to upload an Image to parse.com. I choose the Image from gallery and place it in an ImageView in Activity A as follows:

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);

}

};

Then, In 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();

}

Then I send the byte array through Intent:

intent.putExtra("Image",byteArray);

Now in Activity B:

byte[] data = getIntent().getByteArrayExtra("Image");

final ParseFile file = new ParseFile("propic.png", data);

file.saveInBackground();

Finally I send it to parse like:

listedProperty.put("PropPic", file);

Since then, data is not getting into my parse class and I am getting an unsuccessful callback from parse. What could be wrong?

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

网友答案:

Not sure if this helps but I save my bitmapped images to parse like this, this is after taking the photo with the camera.

@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();
    }
}
网友答案:

i have a similar code in my proyect, and i save the image of an user on Parse with this and show in a ParseImageView, and it work:

 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();
    }

}
分享给朋友:
您可能感兴趣的文章:
随机阅读: