javaandroidparse-platformparse-android-sdk

Save drawable into parse


I saved into my project a drawable R.drawable.descarga what I need to do I that when there is no file to upload to parse it uploads this one.

This is my code.

    if (requestCode == REQUEST_GALLERY_PHOTO7 && resultCode == RESULT_OK) {
            Uri imageUri = data.getData();

            InputStream inputStream;
            try {
                inputStream = getActivity().getApplicationContext().getContentResolver().openInputStream(imageUri);
                Bitmap image = BitmapFactory.decodeStream(inputStream);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                image.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] byteArray = stream.toByteArray();
                ParseFile fileInmoFotoPrinci = new ParseFile("image.jpg", byteArray);
                inmoFoto8.setImageBitmap(image);
                if(fileInmoFotoPrinci!=null) {
                    grabarImagenPrinc.put("imagen7", fileInmoFotoPrinci);
                }else{
                    Drawable myDrawable = getResources().getDrawable(R.drawable.descarga);
                    Bitmap myLogo = ((BitmapDrawable) myDrawable).getBitmap();
//                    FileOutputStream fos = new FileOutputStream(myDrawable);
//
//                    File file=myLogo.compress(Bitmap.CompressFormat.PNG, 100, fos);

                    grabarImagenPrinc.put("imagen7", myDrawable);


                }
                grabarImagenPrinc.saveInBackground();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Toast.makeText(getActivity(), "No fue posible abrir la imagen", Toast.LENGTH_LONG).show();
            }

        }

i would be having the trouble in this part ...

Drawable myDrawable = getResources().getDrawable(R.drawable.descarga);
                    Bitmap myLogo = ((BitmapDrawable) myDrawable).getBitmap();
//                    FileOutputStream fos = new FileOutputStream(myDrawable);
//
//                    File file=myLogo.compress(Bitmap.CompressFormat.PNG, 100, fos);

                    grabarImagenPrinc.put("imagen7", myDrawable);

i have tried to apply many StackOverflow post in this part but nothing seems to work.

The Documentation says this but I don't really understand it...

    byte[] data = "Working at Parse is great!".getBytes();
ParseFile file = new ParseFile("resume.txt", data);

Also tried this code in the else statement.

 Drawable d = null; // the drawable (Captain Obvious, to the rescue!!!)
                Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
                byte[] bitmapdata = stream.toByteArray();
                ParseFile file = new ParseFile("image.jpeg", bitmapdata);
                grabarImagenPrinc.put("imagen7",file);

My problem is that the drawable isnt saving to parse how it should do.

This is the code update!.

private void queryEmpresa() {
    /**Ojo no es que no este sirviendo el metodo sino que el tipo de empresa asignado al usuario
     * no concuerda para que llene el recycler*/
    ParseQuery<ParseUser> query = ParseUser.getQuery();
    query.whereEqualTo("objectId", ParseUser.getCurrentUser().getObjectId());
    query.include("Empresa");
    query.getInBackground(ParseUser.getCurrentUser().getObjectId(), new GetCallback<ParseUser>() {
        public void done(ParseUser object, ParseException e) {
            if (e == null) {
                // object will be your user and you should be able to retrieve Empresa like this
                empresa = object.getParseObject("Empresa");
                if (empresa != null) {
                    ParseObject grabarImagenPrinc = new ParseObject("PropiedadesInmobiliarias");

                    stringEmpresa = empresa.getObjectId();
                    ParseObject entity = new ParseObject("PropiedadesInmobiliarias");
                    Bitmap bm = ((BitmapDrawable) inmoFotoPrincipal.getDrawable()).getBitmap();

                    if(bm.equals("")) {
                        ByteArrayOutputStream myLogoStream = new ByteArrayOutputStream();
                        bm.compress(Bitmap.CompressFormat.PNG, 100, myLogoStream);
                        byte[] myLogoByteArray = myLogoStream.toByteArray();
                        bm.recycle();
                        ParseFile myLogoFile = new ParseFile("mylogo.png", myLogoByteArray);
                        grabarImagenPrinc.put("imagen7", myLogoFile);
                        grabarImagenPrinc.saveInBackground(new SaveCallback() {
                            @Override
                            public void done(ParseException e) {

                            }
                        });
                    }
                entity.put("numeroBanos", edittextNumeroDeBanos.getText().toString().trim());
                entity.put("descripcionAdicionalPropiedad", edittextDescripcion.getText().toString().trim());
                entity.put("Empresa", ParseObject.createWithoutData("Empresa", stringEmpresa));
                entity.put("NombrePropiedad", edittextNombrePropiedad.getText().toString().trim());
                entity.put("Precio", edittextPrecio.getText().toString().trim());
                String xx=edittextNumeroHabitaciones.getText().toString().trim();
                entity.put("numeroDeHabitaciones", edittextNumeroHabitaciones.getText().toString().trim());
                String xy=edittextMetrosCuadrados.getText().toString().trim();
                entity.put("metrosCuadrados", edittextMetrosCuadrados.getText().toString().trim());
                entity.put("valorAdministracion", edittextValorAdmin.getText().toString().trim());
                entity.put("Parqueaderos", edittextParqueaderos.getText().toString().trim());
                entity.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(ParseException e) {

                        Log.i("XX","este es el error de porque no salva",e);
                        Intent intent = new Intent(getActivity(), MainActivity.class);
                        getActivity().startActivity(intent);
                    }
                });

            } else {
                // something went wrong. It would be good to log.
            }
        }
    }


});
}

This is used in a button to save with an on click listener...

look at this i made a different approach taking your suggestions davi.

Look that the value for bm is "" but i dont know how to do the if it is always skipping it


Solution

  • try:

    ...
    Drawable myDrawable = getResources().getDrawable(R.drawable.descarga);
    Bitmap myLogo = ((BitmapDrawable) myDrawable).getBitmap();
    ByteArrayOutputStream myLogoStream = new ByteArrayOutputStream();
    myLogo.compress(Bitmap.CompressFormat.PNG, 100, myLogoStream);
    byte[] myLogoByteArray = myLogoStream.toByteArray();
    myLogo.recycle();
    ParseFile myLogoFile = new ParseFile("mylogo.png", myLogoByteArray);
    grabarImagenPrinc.put("imagen7", myLogoFile);
    ...