can't see the file in parse data browser - android

I'm trying to implement a simple file upload and associate the file with the current user.
I followed the guide on parse.com and checked with several questions on here with no luck.
The saveinBackground operation is successful, no exceptions are being thrown but I can't see the file in Parse.com data browser.
Here's my code.
final long time1 = Time;
//Image part
//upload the .jpg file for user's history
//Parse
byte[] data = filePath.getBytes();
final ParseFile file = new ParseFile("asdasd.jpg", data);
file.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
// Handle success or failure here ...
if ( e == null){
ParseObject rentedTime = new ParseObject("Time");
rentedTime.put("duration", renttime1);
rentedTime.put("owner", ParseUser.getCurrentUser());
rentedTime.put("id", id);
rentedTime.put("image", file);
rentedTime.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null)
Log.d("upload successful" + file.getName(), null);
}
});
} else {
Log.d("terrible", e.getMessage());
}
}
}, new ProgressCallback() {
public void done(Integer percentDone) {
// Update your progress spinner here. percentDone will be between 0 and 100.
}
});
}
What am I missing?

That's because you are NOT uploading the file..see
byte[] data = filePath.getBytes();
just getting the bytes from the file path wont work reasonably.
First get the bitmap and then convert it to byte[] see how..
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
byte[] data = getBytes(bitmap);
private byte[] getBytes(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
byte[] data = outputStream.toByteArray();
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
And you can now use the data..
final ParseFile file = new ParseFile("asdasd.png", data);
I've changed the file name to .png format if you wish ti save in jpg format then compress in .JPG format as well.
Hope you find this helpful! :)

Related

How to convert binary string to bitmap?

I am new to android. I am getting one issue as passing my image as binary in api using retrofit but while getting same binary string of image in response not able to convert binary string to Bitmap again. Below i am passing the binary string getting in response. Its a great help if anyone can help me.
"????\u0000\u0010JFIF\u0000\u0001\u0001\u0000\u0000H\u0000H\u0000\u0000??\u0000\u0011\b\u0002X\u0002?\u0003\u0001\"\u0000\u0002\u0011\u0001\u0003\u0011\u0001??\u0000\u001f\u0000\u0000\u0001\u0005\u0001\u0001\u0001\u0001\u0001\u0001\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b??\u0000?\u0010\u0000\u0002\u0001\u0003\u0003\u0002\u0004\u0003\u0005\u0005\u0004\u0004\u0000\u0000\u0001}\u0001\u0002\u0003\u0000\u0004\u0011\u0005\u0012!1A\u0006\u0013Qa\u0007\"q\u00142???\b#B??\u0015R??$3br?\t\n\u0016\u0017\u0018\u0019\u001a%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz???????????????????????????????????????????????????????????????????????????\u0000\u001f\u0001\u0000\u0003\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b??\u0000?\u0011\u0000\u0002\u0001\u0002\u0004\u0004\u0003\u0004\u0007\u0005\u0004\u0004\u0000\u0001\u0002w\u0000\u0001\u0002\u0003\u0011\u0004\u0005!1\u0006\u0012AQ\u0007aq\u0013\"2?\b\u0014B????\t#3R?\u0015br?\n\u0016$4?%?\u0017\u0018\u0019\u001a&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz
use this code,
String dataValue="";
byte[] bytes = dataValue.getBytes();
Bitmap bmp= BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Try with this.
You can write the byte array directly to file and do whatever you want with the file:
public void writeToFile(byte[] data, String fileName) throws IOException{
FileOutputStream out = new FileOutputStream(fileName);
out.write(data);
out.close();
}
Also if you have binary stream instance, you can create a bitmap instance directly from the stream, you can use BitmapFactory and convert to bitmap:
Bitmap image = BitmapFactory.decodeStream(stream);
You can download the image file using the following function, where body is retrofit2.0 responsebody instance:
private void DownloadImage(ResponseBody body) {
try {
InputStream in = null;
FileOutputStream out = null;
try {
in = body.byteStream();
out = new FileOutputStream("/sdcardpath" + "imagefilename.jpg");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

How to store the image in Parse local data store in offline mode

I have been searching for storing the image offline using:
Bitmap image = ...
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] data = stream.toByteArray();
ParseFile file = new ParseFile("image.png", data);
file.saveInBackground();
photo = new Photo();
photo.setPhotoFile(file);
photo.pinInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Log.d("SAVED", "SAVED SUCCESSFULLY");
} else {
Log.d("ERROR msg is :", e.getMessage());
}
});
This gives error:
java.lang.IllegalStateException: Unable to encode an unsaved ParseFile.
But when I use "photo.saveInBackground" it works. I have searched on the Google but can't find the appropriate solution.
The method file.saveInBackground(); is asynchronous. You should either call file.save(); or implement callback to pin the photo once the file is saved.

Android : bitmap is null when getting data from caching using Voley

I am using volley library and displaying image successfully. I want to get same downloaded image from cache . This is my code :
private Bitmap getBitmapFromUrl(String url){
String str = "" ;
Bitmap bitmap = null ;
byte[] bytes = null ;
Cache cache = AppController.getInstance().getRequestQueue().getCache();
Entry entry = cache.get(url);
try {
str = new String(entry.data, "UTF-8");
bytes = str.getBytes("UTF_8") ;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeByteArray( bytes, 0,bytes.length,options);
Log.i(PostItemAdapter.class.getSimpleName(), bitmap+"");
return bitmap ;
}
My issue is that the bitmap object is null .
Can any one tell me why ? thanks in advance .
My problem solved using the aid of this great link where I copied the three files in their images package :
DiskLruImageCache
ImageCacheManager
BitmapLruImageCache
Also I copied RequestManager file. Also there was an issue faced me in the DiskLruImageCache when creating the key from the URL, and I solved this by replacing few lines in this file.
The solution
-old code:
First
#Override
public Bitmap getBitmap( String key ) {
Bitmap bitmap = null;
DiskLruCache.Snapshot snapshot = null;
try {
snapshot = mDiskCache.get( key );
....... etc
Second
#Override
public void putBitmap( String key, Bitmap data ) {
DiskLruCache.Editor editor = null;
try {
editor = mDiskCache.edit( key );
......etc
-new code:
First
#Override
public Bitmap getBitmap( String key ) {
Bitmap bitmap = null;
DiskLruCache.Snapshot snapshot = null;
try {
snapshot = mDiskCache.get( ImageCacheManager.getInstance().createKey(key) );
....etc
Second
#Override
public void putBitmap( String key, Bitmap data ) {
DiskLruCache.Editor editor = null;
try {
editor = mDiskCache.edit( ImageCacheManager.getInstance().createKey(key) );
.....etc
Note : All this modification in DiskLruImageCache , Beside the caching you can save images to SD Card by this function :
private void saveImageToSD(Bitmap bmp) {
bytes = new ByteArrayOutputStream();
bmp.compress(mCompressFormat, 90, bytes);
file = new File(Environment.getExternalStorageDirectory()+File.separator+"myImage"+(++i)+".jpg");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
/*--- create a new FileOutputStream and write bytes to file ---*/
try {
fos = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fos.write(bytes.toByteArray());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
And calling to this function be inside the getBitmap(String URL) function witch inside the DiskLruImageCache file .

How to update image using parse Android?

I want to fetch image from table 1 to update image in table 2 on parse here is my code
ParseFile image= ParseUser.getCurrentUser().getParseFile("image");
if(image==null)
{
}
else
{
try {
byte[] data=image.getData();
Bitmap bmp = BitmapFactory
.decodeByteArray(
data, 0,
data.length);
// Set the Bitmap into the
// ImageView
image1.setImageBitmap(bmp);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This code is working perfect it retrieves and set image properly now I want this "image" to upload in my Second table I am doing this
ParseQuery<ParseObject> query = ParseQuery.getQuery("XYZ");
String user_id=ParseUser.getCurrentUser().getObjectId();
query.getInBackground(user_id, new GetCallback<ParseObject>() {
#Override
public void done(ParseObject pdata, ParseException e) {
// TODO Auto-generated method stub
pdata.put("image",image); // this line throw NullPointerException
pdata.saveInBackground();
}
});
What I am doing wrong anyone please help?
You need ParseFile object. Try the following code. (It works fine, I just checked it)
byte[] pfArray = getBytesFromBitmap(bmp);
ParseFile file = new ParseFile("abc.png", pfArray);
// Upload the image into Parse Cloud
file.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
System.out.println("saved");
}
});
public byte[] getBytesFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 70, stream);
return stream.toByteArray();
}
hope it helps.

Converting correctly bitmap from gallery to base64

I develop an Android App and I develop a feature to take a picture from the gallery and to send it to my server and assign it to user as his profil picture.
I select an image (important : taken by ma camera) and I convert it to base64 before sending it to my server. When I get the image and I try to display it I have only part of the picture but not for image in png... I tried to change compressformat in JPEG and it's worst... I didn't understand the problem since 2 days I get crazy... Help Please :)
private Bitmap bitmap;
public void chooseProfilePicture(View view) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, REQUEST_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
InputStream stream = null;
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
try {
if (bitmap != null)
bitmap.recycle(); // recyle unused bitmaps
stream = getContentResolver().openInputStream(data.getData());
bitmap = BitmapFactory.decodeStream(stream);
imageView.setImageBitmap(bitmap);
// HERE THE IMAGE IS DISPLAYED 100% WELL
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (stream != null)
try {
stream.close();
} catch (Exception ex) {
Log.e("EditProfilActivity", ex.getMessage());
}
}
}
public void save(View view) {
if (bitmap != null) {
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
bitmap.recycle();
byte[] imageBytes = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
picture = encodedImage;
// HERE WHEN I TRY TO DISPLAY THE IMAGE I HAVE ONLY A PART OF THE IMAGE
} catch (Exception e) {
Log.e("", e.getMessage());
}
}
You are trying to show image form string(from encoding image file)?
byte[] imagebyteArry = byteArrayOutputStream.toByteArray();
String imageString = encodeImage(imagebyteArry);
sendImageToserver(imageString);
............
String imageString = getImageFromserver();
byte[] imagebyteArry = decodeImage(imageString);
Search how to show image from byte[] in android?
public static String encodeImage(byte[] imageByteArray) {
return Base64.encodeBase64URLSafeString(imageByteArray);
}
public static byte[] decodeImage(String imageDataString) {
return Base64.decodeBase64(imageDataString);
}

Categories

Resources