saveInBackground throws exception "unsaved ParseObject" - android

I'm trying to save an item which has a relation to my image class. I can save the item, and i can save the image seperately, but when I am saving them with the relation if throws this error:
unable to encode an association with an unsaved ParseObject
Here is my code:
myAdvert = new ParseObject("Items");
myAdvert.put("title", "some title");
if (pic1Set) {
Bitmap bitmap = ((BitmapDrawable) pic1.getDrawable())
.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
myPhoto1 = new ParseObject("Images");
filePhoto = new ParseFile("image.png", byteArray);
filePhoto.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
myPhoto1.put("image", filePhoto);
ParseRelation relation = myAdvert.getRelation("pictures");
relation.add(myPhoto1);
myAdvert.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
// TODO Auto-generated method stub
if (e != null) {
--> // THIS IS WHERE THE ERROR IS THROWN!!!!
Log.d("error", e.toString());
} else {
}
}
});
} else {
Log.d("wow, error", e.toString());
Toast.makeText(getApplicationContext(),
"Error saving: " + e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
});

lets consider this a comment and some extra info rather then an answer -
I'm running to the same exception when trying to login BUT it will happen only after I have some action done offline while the local data store is working
I also have ACL on.
I'm guessing it has something to do with an attempt to save stuff pinned from my offline work in a wrong order or something.
I'm working on debugging mine - good luck to all of us - let me know if you find something.

Related

Force ToggleButton to stop writing into my local memory file

I have a variable MyFinalPressure which is populated with sensor data from the sensors pressure. If MyFinalPressure is == to 4000 (4000 points or 40 sec) then stop writing to local storage.
But it looks like when I debug the code, it hits the boolean MaxPoints and is still writing. I wonder if my logic is wrong or not.
Could you please help me out.
public Boolean Store = false;
Boolean MaxPoints = false;
if (activity.Store) {
activity.writeToFile(MyFinalPressure);//MyFinalPressure is float of one dimension array or stream of array.
}
if (MyFinalPressure==4000){ //this conditon, am trying to stop wrting to local memory.
activity.MaxPoints = true;
}
FileOutputStream fileOutputStream;
//method to write into local memory.
public void writeToFile(final float MyFinalPressure) {
Log.d(TAG, "writeToFile.");
String finalData;
finalData = String.valueOf(MyFinalPressure);
try {
// true here for 'append'
fileOutputStream = new FileOutputStream(file, true);
String Space = " ";
byte[] convert = Space.getBytes();
fileOutputStream.write(finalData.getBytes());
fileOutputStream.write(convert);
fileOutputStream.flush();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//write to file.
StartWriting = (ToggleButton) findViewById(R.id.startWriting);
StartWriting.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (StartWriting.isChecked()) {
Store = true;
Toast.makeText(getApplicationContext(), "Data Starts writing into (Message.txt) file", Toast.LENGTH_LONG).show();
} else {
if (!StartWriting.isChecked()|| MaxPoints==true) { //here - this is wrong logic to stop writing to my file.
Toast.makeText(getApplicationContext(), "Data Stored at myAppFile", Toast.LENGTH_SHORT).show();
String finalData1;
finalData1 = String.valueOf(fileOutputStream);
Log.i(TAG, "of_writes: " + finalData1);
// Toast.makeText(getApplicationContext(), "Data_write_number: " + finalData1.length(), Toast.LENGTH_SHORT).show();
Store = false;
}
}
}
});
Just take out !StartWriting.isChecked()
Because of the above if statement StartWriting.isChecked() will always be false. Then because you are checking for "!StartWriting.isChecked()" it will always enter the statement.

Upload Image to Parse is not working

I have a "save" button that will upload an image to parse. It used to work really well. But for some reason, it stops working.
When I press the button, nothing happens. As if the code ignored the onClick method. I thought something is wrong with the button or the other objects being saved. So I disabled every single object and found out it works fine when I disabled the image uploading.
So if I disable the image uploading, I can still save the name. But when I enable the image uploading, nothing happens and it feels like the onClick method is being ignored.
This is very weird. I haven't changed anything in my code and now it's not working. It used to work.
Do you guys have any idea what's wrong?
This is the updated code with logs:
protected Button mSavePet;
#Override
protected void onCreate(Bundle savedInstanceState) {
mSave = (Button) findViewById(R.id.Save);
mSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Log", "save button clicked");
myUploading();
}
});
}
private void myUploading() {
Log.d("Log", "myUploadig Start");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Compress image to lower quality scale 1 - 100
mBitmap.compress(Bitmap.CompressFormat.PNG, 3, stream);
byte[] image = stream.toByteArray();
ParseUser currentUser = ParseUser.getCurrentUser();
String currentUserUsername = currentUser.getUsername();
//get the new pet info
String petname = mPetName.getText().toString();
Log.d("Log", "puting objects start");
//Save Pet to the cloud
final ParseObject petObject = new ParseObject("MyPets");
petObject.put("petName", petname);
petObject.put("user", currentUserUsername);
// Create the ParseFile for Image
final ParseFile file = new ParseFile("petImage.png", image);
// Upload the image into Parse Cloud
file.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
Log.d("Log", "SaveFileInBackground Start");
if(e == null){
Log.d("Log", "Putting ImageFile Start");
// Create a column named "ImageFile" and insert the image
petObject.put("petImage", file);
// Create the class and the columns
petObject.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Log.d("Log", "putting objects finished");
finish();
} else {
}
}
});
}
}
});
}
}
This is the code output:
08-06 20:04:56.117 29376-29376/com.example.stanleysantoso.wikipetia D/Log﹕ save button clicked
08-06 20:04:56.129 29376-29376/com.example.stanleysantoso.wikipetia D/Log﹕ myUploadig Start
08-06 20:05:08.008 29376-29376/com.example.stanleysantoso.wikipetia D/Log﹕ puting objects start
08-06 20:07:06.986 29376-29376/com.example.stanleysantoso.wikipetia D/Log﹕ save button clicked
08-06 20:07:06.986 29376-29376/com.example.stanleysantoso.wikipetia D/Log﹕ myUploadig Start
08-06 20:07:16.972 29376-29376/com.example.stanleysantoso.wikipetia D/Log﹕ puting objects start
so, it never went in file.saveInBackGround.
you're trying to call file.saveInBackground(); and in the next line Object.put("Image", file);. As saveInBackground() is asynchronous it is probably finishes after you're trying to put this file object to Object. Try to add callback and move this code to callback:
file.saveInBackground(new SaveCallback callback() {
#Override()
public void done(ParseException e) {
if (e == null) {
Object.put("Image", file);
// Create the class and the columns
petObject.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
finish();
} else {
}
}
});
} else {
}
}
});

Passing a bitmap/uri to Box Android API v2

EDIT:
I wrote a function passing the Authenticated Box Client and Bitmap Bytestream. Was also informated the root directory on Box has an ID of 0. But I'm still unable to upload.
What I need to do is create a directory, if present don't and upload the file in that directory.
private void doUpload(final BoxAndroidClient client, final ByteArrayInputStream bs) {
if(client.isAuthenticated())
{
System.out.println("Trying to upload: In upload function");
AsyncTask<Null, Integer, Null> task = new AsyncTask<Null, Integer, Null>() {
#Override
protected void onPostExecute(Null result) {
Toast.makeText(PagerActivity.this, "done uploading", Toast.LENGTH_LONG).show();
super.onPostExecute(result);
}
#Override
protected void onPreExecute() {
Toast.makeText(PagerActivity.this, "start uploading", Toast.LENGTH_LONG).show();
super.onPreExecute();
}
#Override
protected Null doInBackground(Null... params) {
try {
BoxDefaultRequestObject folreqObj = new BoxDefaultRequestObject();
BoxFolder newFol = new BoxFolder();
String parentId = newFol.getId();
BoxFileUploadRequestObject requestObj = BoxFileUploadRequestObject.uploadFileRequestObject("0", "bestphotonow.png", bs);
client.getFilesManager().uploadFile(requestObj);
/*
if(client.getFoldersManager().getFolder(parentId, folreqObj).getName()!=null)
{
BoxFileUploadRequestObject requestObj = BoxFileUploadRequestObject.uploadFileRequestObject(parentId, "bestphotonow.png", bs);
client.getFilesManager().uploadFile(requestObj);
}
else
{
BoxFolderRequestObject upreqObj = BoxFolderRequestObject.createFolderRequestObject("BestPhotoNow", parentId);
client.getFoldersManager().createFolder(upreqObj);
BoxFileUploadRequestObject requestObj = BoxFileUploadRequestObject.uploadFileRequestObject(parentId, "bestphotonow.png", bs);
client.getFilesManager().uploadFile(requestObj);
}*/
} catch (BoxRestException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BoxJSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BoxServerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AuthFatalFailureException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
};
task.execute();
}
}
So, I have a wallpaper app that fetches some images from a server. I'm trying to integrate box to be able to upload the picture in the current view (viewpager) to Box via Box Android API v2.
https://github.com/box/box-android-sdk-v2
I have been successful to authenticate the user via oauth. But, I'm really stuck at trying to upload the bitmap or uri direct to their upload call without having to launch a folder picker activity. This is what I have.
On click of a button for upload:
case R.id.boxcloudbutton:
imageLoader.loadImage(url, new SimpleImageLoadingListener(){
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
Intent intent = OAuthActivity.createOAuthActivityIntent(getApplicationContext(), "vfoj4o6nxqblahh454wem", "J9TQWyFSblahV2C", false, "http://localhost");
intent.putExtra("boximageUri", imageUri);
System.out.println("BoxUri: "+imageUri);
startActivityForResult(intent, BOX_RESULT);
super.onLoadingComplete(imageUri, view, loadedImage);
}
});
break;
I'm trying to pass either the bitmap or uri
and in the OnActivityResult
case BOX_RESULT:
if (resultCode == Activity.RESULT_CANCELED) {
// Get the error message for why authentication failed.
String failMessage = data.getStringExtra(OAuthActivity.ERROR_MESSAGE);
// Implement your own logic to handle the error.
// handleFail(failMessage);
} else {
// You will get an authenticated oath token object back upon success.
BoxAndroidOAuthData oauth = data.getParcelableExtra(OAuthActivity.BOX_CLIENT_OAUTH);
// If you don't want to supply a customized hub or parser, use null to fall back to defaults.
BoxAndroidClient client = new BoxAndroidClient( "vfoj4oxxxxxxxxxxtordlh454wem",
"J9TjxxxxxxxmRwux2V2C", null, null, null);
client.authenticate(oauth);
System.out.println("Data for Box: "+data.getExtras().getString("boximageUri"));
BoxFileUploadRequestObject requestObj = BoxFileUploadRequestObject.uploadFileRequestObject(0, "name", file);
BoxFile bFile = client.getFilesManager().uploadFile(requestObj);
}
break;
Here, I successfully get authenticated, but I do not recieve the uri or the bitmap in the Intent data that I passed.
More, if anyone has any idea of how I can pass bitmap to a file for a file upload for Box api v2, I would really appreciate some assistance!
Thanks,
Arnab
Try shifting around the responsibility....
When you go to the Oauth activity , do just the authentication and return the credentials or Token from that which grants permission for subsequent actions on Box api.
Choose correct api to POST a bitmap... Here you want an api call that allows the ByteArray corresponding to the bitMap to be loaded as a ByteArrayEntity in the body of a http POST action.
This answer sample of how to prepare a local bitmap instance for being wrapped in body of Post as byteArray.

how can I access Picasso' s cached image to make a share intent?

I' m using Picasso to help the cache of images.
The question is, how can I access the downloaded image to make a share intent?
any ideas? thanks!
I hope you can understand my question :-)
Sorry for my delay, I found a solution, but, not a good one...
First, I really searched for a while and looked at the code of Picasso. It seems like you should provide your own downloader and other stuff. But then, why should I use the lib...
And then, I suppose it's Picasso's design / architecture to just cache the file in the internal storage. Maybe because the external storage is not always available (like the user may plug in his SD card to his computer), or maybe because the external storage is not as fast as the internal... That's my guess. In a word, other apps cannot access the internal storage of the current app, so the share cannot be done.
Thus, I made a really ordinary solution. I just wait for Picasso to give the Bitmap, and compress it to a file in the external file, then do the share. It seems like a bad solution, but it really solves the problem, yes...
You should be aware of whether the external cache directory is available or not. If not, you cannot do the share. And you need to put the compress task in a background thread, so, waiting the external file cached... Does it seem like a bad solution? I think so...
Below is my project code, you can have a try...
private boolean mSaved; // a flag, whether the image is saved in external storage
private MenuItem mShare;
private Intent mIntent;
private ShareActionProvider mShareActionProvider;
private File mImage; // the external image file would be saved...
private Target target = new Target() {
#Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
new Thread(new Runnable() {
#Override
public void run() {
FileOutputStream os = null;
try {
String dir = CatnutUtils.mkdir(getActivity(), Constants.FANTASY_DIR); // check the exteral dir avaiable or not...
String[] paths = Uri.parse(mUrl).getPath().split("/");
mImage = new File(dir + File.separator + paths[2] + Constants.JPG); // resoleve the file name
} catch (Exception e) { // the external storage not available...
Log.e(TAG, "create dir error!", e);
return;
}
try {
if (mImage.length() > 10) { // > 0 means the file exists
// the file exists, done.
mIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mImage));
mSaved = true;
return;
}
os = new FileOutputStream(mImage);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
mIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mImage));
mSaved = true;
} catch (FileNotFoundException e) {
Log.e(TAG, "io error!", e);
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
Log.e(TAG, "io closing error!", e);
}
}
}
}
}).start();
mFantasy.setImageBitmap(bitmap);
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
mFantasy.setImageDrawable(errorDrawable);
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
if (placeHolderDrawable != null) {
mFantasy.setImageDrawable(placeHolderDrawable);
}
}
};
#Override
public void onPrepareOptionsMenu(Menu menu) {
mShare.setEnabled(mSaved);
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fantasy, menu);
mShare = menu.findItem(R.id.action_share);
mShareActionProvider = (ShareActionProvider) mShare.getActionProvider();
mShare.setActionProvider(mShareActionProvider);
mShareActionProvider.setShareIntent(mIntent);
}
Finally, call Picasso.with(getActivity()).load(mUrl).into(target);
When the file is saved, the user can click the share menu do the share.
public static File getImageFile(Context context, String url)
{
final String CACHE_PATH = context.getCacheDir().getAbsolutePath() + "/picasso-cache/";
File[] files=new File(CACHE_PATH).listFiles();
for (File file:files)
{
String fname= file.getName();
if (fname.contains(".") && fname.substring(fname.lastIndexOf(".")).equals(".0"))
{
try
{
BufferedReader br=new BufferedReader(new FileReader(file));
if (br.readLine().equals(url))
{
File imgfile= new File(CACHE_PATH + fname.replace(".0", ".1"));
if (imgfile.exists())
{
return imgfile;
}
}
}
catch (FileNotFoundException|IOException e)
{
}
}
}
return null;
}
I've just found out this guide with a very good solution.
https://guides.codepath.com/android/Sharing-Content-with-Intents
The code will be like this:
// Can be triggered by a view event such as a button press
public void onShareItem(View v) {
// Get access to bitmap image from view
ImageView ivImage = (ImageView) findViewById(R.id.ivResult);
// Get access to the URI for the bitmap
Uri bmpUri = getLocalBitmapUri(ivImage);
if (bmpUri != null) {
// Construct a ShareIntent with link to image
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/*");
// Launch sharing dialog for image
startActivity(Intent.createChooser(shareIntent, "Share Image"));
} else {
// ...sharing failed, handle error
}
}
// Returns the URI path to the Bitmap displayed in specified ImageView
public Uri getLocalBitmapUri(ImageView imageView) {
// Extract Bitmap from ImageView drawable
Drawable drawable = imageView.getDrawable();
Bitmap bmp = null;
if (drawable instanceof BitmapDrawable){
bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
} else {
return null;
}
// Store image to default external storage directory
Uri bmpUri = null;
try {
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
file.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}
It basically consists in retrieving the bitmap from the imageview and saving it to a local temp file and then using it for sharing. I've tested it and it seems to work fine.

Internal memory full of pictures, probably caused by Bitmap.compress(format, int, stream)

My app is a Wifi chat app with which you can communicate between two Android units with text messages and snap camera pictures and send them. The pictures are stored to the SD-card.
I used to have an OutOfMemoryError thrown after a couple of sent images, but I solved that problem by sending the
options.inPurgeable = true;
and
options.inInputShareable = true;
to the BitmapFactory.decodeByteArray method. This makes the pixels "deallocatable" so new images can use the memory. Thus, the error no longer remains.
But, the internal memory is still full of images and the "Low on space: Phone storage space is getting low" warning appears. The app no longer crashes but there's no more memory on the phone after the app finishes. I have to manually clear the app's data in Settings > Applications > Manage Applications.
I tried recycling the bitmaps and even tried to explicitly empty the app's cache, but it doesn't seem to do what i expect.
This function receives the picture via a TCP socket, writes it to the SD-card and starts my custom Activity PictureView:
public void receivePicture(String fileName) {
try {
int fileSize = inStream.readInt();
Log.d("","fileSize:"+fileSize);
byte[] tempArray = new byte[200];
byte[] pictureByteArray = new byte[fileSize];
path = Prefs.getPath(this) + "/" + fileName;
File pictureFile = new File(path);
try {
if( !pictureFile.exists() ) {
pictureFile.getParentFile().mkdirs();
pictureFile.createNewFile();
}
} catch (IOException e) { Log.d("", "Recievepic - Kunde inte skapa fil.", e); }
int lastRead = 0, totalRead = 0;
while(lastRead != -1) {
if(totalRead >= fileSize - 200) {
lastRead = inStream.read(tempArray, 0, fileSize - totalRead);
System.arraycopy(tempArray, 0, pictureByteArray, totalRead, lastRead);
totalRead += lastRead;
break;
}
lastRead = inStream.read(tempArray);
System.arraycopy(tempArray, 0, pictureByteArray, totalRead, lastRead);
totalRead += lastRead;
}
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(pictureFile));
bos.write(pictureByteArray, 0, totalRead);
bos.flush();
bos.close();
bos = null;
tempArray = null;
pictureByteArray = null;
setSentence("<"+fileName+">", READER);
Log.d("","path:"+path);
try {
startActivity(new Intent(this, PictureView.class).putExtra("path", path));
} catch(Exception e) { e.printStackTrace(); }
}
catch(IOException e) { Log.d("","IOException:"+e); }
catch(Exception e) { Log.d("","Exception:"+e); }
}
Here's PictureView. It creates a byte[ ] from the file on the SD-card, decodes the array to a Bitmap, compresses the Bitmap and writes it back to the SD-card. Lastly, in the Progress.onDismiss, the picture is set as the image of a full screen imageView:
public class PictureView extends Activity {
private String fileName;
private ProgressDialog progress;
public ImageView view;
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
Log.d("","onCreate() PictureView");
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
view = new ImageView(this);
setContentView(view);
progress = ProgressDialog.show(this, "", "Laddar bild...");
progress.setOnDismissListener(new OnDismissListener() {
public void onDismiss(DialogInterface dialog) {
File file_ = getFileStreamPath(fileName);
Log.d("","SETIMAGE");
Uri uri = Uri.parse(file_.toString());
view.setImageURI(uri);
}
});
new Thread() { public void run() {
String path = getIntent().getStringExtra("path");
Log.d("","path:"+path);
File pictureFile = new File(path);
if(!pictureFile.exists())
finish();
fileName = path.substring(path.lastIndexOf('/') + 1);
Log.d("","fileName:"+fileName);
byte[] pictureArray = new byte[(int)pictureFile.length()];
try {
DataInputStream dis = new DataInputStream( new BufferedInputStream(
new FileInputStream(pictureFile)) );
for(int i=0; i < pictureArray.length; i++)
pictureArray[i] = dis.readByte();
} catch(Exception e) { Log.d("",""+e); e.printStackTrace(); }
/**
* Passing these options to decodeByteArray makes the pixels deallocatable
* if the memory runs out.
*/
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inInputShareable = true;
Bitmap pictureBM =
BitmapFactory.decodeByteArray(pictureArray, 0, pictureArray.length, options);
OutputStream out = null;
try {
out = openFileOutput(fileName, MODE_PRIVATE);
/**
* COMPRESS !!!!!
**/
pictureBM.compress(CompressFormat.PNG, 100, out);
pictureBM = null;
progress.dismiss(); }
catch (IOException e) { Log.e("test", "Failed to write bitmap", e); }
finally {
if (out != null)
try { out.close(); out = null; }
catch (IOException e) { }
} }
}.start();
}
#Override
protected void onStop() {
super.onStop();
Log.d("","ONSTOP()");
Drawable oldDrawable = view.getDrawable();
if( oldDrawable != null) {
((BitmapDrawable)oldDrawable).getBitmap().recycle();
oldDrawable = null;
Log.d("","recycle");
}
Editor editor =
this.getSharedPreferences("clear_cache", Context.MODE_PRIVATE).edit();
editor.clear();
editor.commit();
}
}
When the user presses the back key, the picture isn't supposed to be available anymore from within the app. Just stored on the SD-card.
In onStop() I recycle the old Bitmap and even try to empty the app's data. Still the "Low on space" warning appears. How can I be sure the images won't allocate the memory anymore when they're not needed?
EDIT: It appears the problem is the compress method. If everything after compress is commented, the problem remains. If I delete compress, the problem disappears. Compress seems to allocate memory that's never released, and it's 2-3 MB per image.
Ok, I solved it. The problem was, I was passing an OutputStream to compress, which is a stream to a private file in the app's internal memory. That's what I set as the image later. This file is never allocated.
I didn't get that I had two files: one on the SD-card and one in the internal memory, both with the same name.
Now, I'm just setting the SD-card file as the ImageView's image. I never read the file into the internal memory as a byte[], thus never decoding the array to a bitmap, thus never compressing the bitmap into the internal memory.
This is the new PictureView:
public class PictureView extends Activity {
public ImageView view;
private String path;
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
Log.d("","onCreate() PictureView");
path = getIntent().getStringExtra("path");
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
view = new ImageView(this);
setContentView(view);
Uri uri = Uri.parse( new File(path).toString() );
view.setImageURI(uri);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Log.d("","Back key pressed");
Drawable oldDrawable = view.getDrawable();
if( oldDrawable != null) {
((BitmapDrawable)oldDrawable).getBitmap().recycle();
oldDrawable = null;
Log.d("","recycle");
}
view = null;
}
return super.onKeyDown(keyCode, event);
}
}
Is it bad practice to put an external file as the image of an ImageView? Should I load it into internal memory first?
If you specifically want the image to be nullified from memory for sure when a user presses back you could override the back button and make your image clean up calls there. I do that in some of my apps and it seems to work. maybe something like this:
#Override
protected void onBackPressed() {
super.onBackPressed();
view.drawable = null;
jumpBackToPreviousActivity();
}
Im pretty sure there are some view methods that clear other caches and things like that. You can recycle the bitmap but that doesnt guarantee that it will be dumped right then but only at some point when the gc gets to it.....but Im sure you probably know that already :)
EDIT: You could also do the same thing in the onPause method. That one is guaranteed to get called. The other two may never get called according to the android docs.
http://developer.android.com/reference/android/app/Activity.html

Categories

Resources