Android: Saving a created bitmap to some equivalent of SharedPreferences? - android

So I'm having the user create a profile, which includes an avatar. I would like the program to, like the user's name and other info, remember the avatar so it appears as their avatar whenever they use the app. I'd prefer for this bitmap to be saved when created, so the app doesn't need to rebuild the avatar (which is scaled and whatnot) each time the app is started. Is this possible? From the looks of it this wouldn't be able to be saved in SharedPrefs... any idea what the best way to do this would be? Thanks.
Followup: Followed CommonWares's suggestion. Saved to SD... but having trouble calling it back when the app is reloaded.
Currently, in onCreate:
String path = "testapp/images/thumbs/"+m_username+".jpg";
File file = new File("testapp/images/thumbs/"+m_username+".jpg");
if(file.exists()) {
Log.e("TRY TO GET THUMB", "I EXIST");
m_thumb = BitmapFactory.decodeFile(path);
Drawable draw = new BitmapDrawable(getResources(), m_thumb);
m_photoButtonE.setBackgroundDrawable(draw);
}
Does not find the file, says it does not exist, though when I check my SD card the image is in that exact spot, with the proper file name and everything. Any ideas? Thanks all.

Is this possible?
Save it in a file.
From the looks of it this wouldn't be able to be saved in SharedPrefs... any idea what the best way to do this would be?
Save it in a file. If you intend on having multiple avatar files, put a path to the saved avatar file in the SharedPreferences.

Related

How to read an image into a Mat object using opencv c++ and android studio ndk?

I have searched for a few hours now and can't seem to find an answer to my question(s). I have written the following lines of code in the android ndk(c++) and I am using the needed opencv libraries to accomplish the task.
void opening_images(){
Mat image ;
sillyString = "I have changed";
String imagePath = "//drawable//ring.png";
image = imread(imagePath,IMREAD_COLOR);
if(image.empty()){
sillyString = "Image not loaded";
}
else {
sillyString = "Image loaded";
}
}
I have tested this code in Qt with opencv and it works fine.At the moment the program in android studio returns the "Image not loaded" string. I think the main problem which is present is, the fact that I don't completely understand how to work with the file paths? In android studio I have included a picture under res/drawable/ring.png. I am able to view this image using the java side of the app.
Question 1: Is the specified imagePath = "//drawable//ring.png" correct to access the ring.png file ?
Question 2: Is there any permissions needed allowing the ndk to access res folders ?
Question 3: Is there any similar methods to assign an image to a Mat object?
Any help will be appreciated.
Edit:
If you take a look at how BitmapFactory decode resource works - you will see that getting bitmap from drawable still requires unpacking of a compressed image.
So answer to your q1: No it is not correct way to access ring.png, you will either have to download resource to your device or unpack it to byteArray and use imdecode instead of imread
Since this was my first time using Android Studio there was much to learn. It took me a while but here is the answers to the questions that I posted.
Question 1: Is the specified imagePath = "//drawable//ring.png" correct to access the ring.png file ?
This is most definitely not the correct path to use when accessing images for the purpose of image processing etc.The drawable folder can still be used to update ex. an image view by setting the src of the image view to the image
imageView.setImageResource(R.drawable.ring);
When working with images and Mat objects, I found it best to use the Android debugging bridge to copy the files to the SD card of the device. This link will provided you with the necessary steps to install the adb https://www.howtogeek.com/125769/how-to-install-and-use-abd-the-android-debug-bridge-utility/
When the images are done copying to the SD card. The file path can be found by making use of the built in Java functionEnvironment.getExternalStorageDirectory(). Which looks something like /storage/emulated/0/YOUR_file it depends on the location which was used to copy the files to.
Useful tip: Download ES File Explorer for the device to help navigate through the external or internal storage.
Question 2: Is there any permissions needed allowing the ndk to access res folders ?
The method which I used didn't need any permissions. However at the moment the NDK side cannot directly read an image from the SD card, the image must be passed from the Java side by making use of assets or by passing the address of the image which was read into the Mat object(Java side).
Read and write permission is needed in order to access the SD card. This must be set in the manifest.xml and must be correctly implemented in the code. There are many great tutorials on YouTube.
Question 3: Is there any similar methods to assign an image to a Mat object?
This question seems redundant now, there are many ways to skin a cat.
In short, I think it is easier to stick to the Java side when using Opencv4Android and some form of image processing is needed.To get you started in java here is a small snippet from my code.
Mat image;
String imageInSD = Environment.getExternalStorageDirectory()+"/Pictures/Data/Birds/"(ImageFolders[i])+"/"+String.valueOf(id)+".png";
image = Imgcodecs.imread(imageInSD,Imgcodecs.IMREAD_COLOR);
Good luck!!
Another way to use saved image in NDK is follows,
Then drawable folder, you can save it in assets folder. This helps to access multiple images also easily.
Then BitmapFactory.decodeStream helps to take it as bitmap and Utils.bitmapToMat is used to convert bitmap image to Mat file.
Then this Mat file, you can pass to NDK and process it using OpenCV C++.
Thanks

Titanium 3.X getFile() from local storage

Using Titanium on Android 4+ I want to access a jpeg file which has been taken with the camera. I need to achieve 2 objectives, namely, return the EXIF data and transfer the bytes to an API endpoint. My problem is I'm unable to access the file...
I'm using a 3rd party module to handle the file selection (Multi Image Picker) which returns a list of file locations, using the File Manager app on the emulator (GenyMotion) I can confirm the location on disk is correct. However, the following always returns false...
var file = Ti.Filesystem.getFile('/mnt/sdcard/DCIM/Camera/IMG_20140901_083735.jpg');
Ti.API.info('Do we have a file? ' (file.exists()? 'YES' : 'NO'));
The output for the above would be... Do we have a file? NO
Further reading shows Titanium has 5 predefined folder locations which can be passed into the getFile() method and one possible reason for the above code not working would be it is defaulting to the 'Resouces' folder location? That said all but one folder location is app specific, the exception being externalStorageLocation. Now my understanding of an Android device is that any image taken with the camera will be stored on the internal storage system unless an SD card is present. This is true in my case as the following lists 0 files...
var extDir = Ti.Filesystem.getExternalStorageDirectory();
var dir = Ti.Filesystem.getFile(extDir);
var dir_files = dir.getDirectoryListing();
Ti.API.info('External files... ' + dir_files.length);
The output for the above would be... External files... 0
So am I right in thinking Appcelerator have simply not included the ability to access local storage (outside of any app specific folders) within their API? Or am I missing something and there is in fact another way?
Thanks to #Bharal I was able to find a solution...
By using the Ti.Media.openPhotoGallery() method I was able to identify the correct native path for the image by inspecting the event object returned from the success callback.
The path was missing 'file://' at the beginning, I couldn't be 100% sure but I suspect this forces the getFile() method to use an absolute path and not a relative path from within the Resources folder.
To confirm, the following will return a file object...
var file = Ti.Filesystem.getFile('file://[path]');
Where [path] is the folder location as reported within the File Manager app on the device, for example '/mnt/sdcard/DCIM/Camera/IMG_20140901_083735.jpg'
Yah mon, i dunno.
Here is wat i used when i was doin pictures on my Ti app, but then i got rid of that section because i realised i didn't need to be doin pictures. Pictures mon, dey ain' what you want sometimes, yo?
Ti.Media.openPhotoGallery({ //dissall jus' open up a piccha selectin' ting. ez.
success:function(event){
var image = event.media;
if (event.mediaType==Ti.Media.MEDIA_TYPE_PHOTO){
//so image.nativePath is the path to the image.
// profileImg be jus' some Ti.UI.createImageView ting yo be puttin in yo' page.
//meyybe yo be wantin' alert(image.nativePath); here too, dat be helpin?
profileImg.image = image.nativePath;
}
},
cancel:function(){
//we cancelled out, why we doin' that?
}
});
Now that isn't going to really be helpin' you, but yo can use that to see wat the native path yo piccha be usin' be, and then be seein' if maybe what yo be puttin' in yo code be sam ting.
Jus' wrap the above as an addEventListener("click", function(){ ... } ); on sam ting in yo page, and jus' add sam element to put th' piccha in if yo be wantin' to see the piccha but i be tellin' you picchas mon, sometimes dey ain' worth time.
But meyybe yo wantin' use not an emulator for dis ting, dey can be actin' weird yo should be usin some small phone maybe? Dat way you can be findin' if yo got dem memory leeks and meyybe some memory sprouts, an memory onions too.

Android-Where does image taken get saved?

Something weird is happening in my application im not sure if it is worth uploading all the code...
Intent pictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
pictureIntent.putExtra( MediaStore.EXTRA_OUTPUT, imageUriToSaveCameraImageTo );
startActivityForResult(Intent.createChooser(pictureIntent, strAvatarPrompt), TAKE_AVATAR_CAMERA_REQUEST);
I use this code to take an photo. The photo gets saved in the DCIM folder and also into imageUriSaveCameraImageto which points to sdcard/folder...The image is given the name image1.jpg..once run it works.
Then i delete the files from DCIM and sdcard/folder and run the application again and take a different photo...for some reason the old photo appears in the folder...it must be caching or storing a copy of it else where...does anyone know where and how i can delete it?thanks
Android indeed caches thumbnails of all the photos in another location.
See here:
http://www.droidforums.net/forum/droid-general-discussions/30998-thumbnail-cache-can-cleared.html
I can't give you an exact answer, but my feeling is that the MediaStore is a ContentProvider, so you may be able to call ContentResolver.delete(URI).

android: How can I check package to be assured that is a file exist?

In my project I have a Picture that through program, I extracted pixels, manipulate those pixels and then saved it in my package
FileOutputStream fos = new
FileOutputStream(context.getFilesDir().getAbsolutePath()
+ "/newPicture.bmp");
so, it saves my picture in
/data/data/my_package_name/files
Now, I want to replace my default picture with this new picture. Actually, since I don't have this new picture, default picture should be shown. when new picture prepared, default picture should be replace with new one.
my main problem is How can I check to be assured that is new picture exist? if not exist show default picture, if exist show new picture.
Thanks.
Write a service in android that will monitor your old file whether any update has happened.
If there is change in timestamp for the file, then new file has been updated.Get this status from your other Activity or View class before you are performing the file operations.
Hope this will help you.

Android MediaStore setting photo name to NULL

I have an app that allows the user to take and save a new picture using the Intent ACTION_IMAGE_CAPTURE. After the user accepts the newly taken picture, I create a ContentValue object to set the picture information, and then I insert the picture into the MediaStore and send a broadcast so that the user can see the photo when opening a picture viewer app such as gallery:
ContentValues newImage = new ContentValues(3);
newImage.put(Media.DISPLAY_NAME, mPicName);
newImage.put(Media.MIME_TYPE, "image/png");
newImage.put(MediaStore.Images.Media.DATA, path);
mPictureUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, newImage);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mPictureUri));
All of this code works fine. The photo is correctly saved, and I can view the photo using gallery. The problem is that when I view the photo in gallery and select "Details" it shows the photo name as "null." And yes, I know for a fact that the variable mPicName above is not null and always has the correct value.
The odd thing is that, when running my app on my Droid running Android 2.1, when I choose to copy files to/from the phone and my computer, I open up the Droid on my Windows computer, and when I go into my app's created folder for photos and view the new photo, the name is correct. Then, when I disable copying files from the phone/computer and go back into gallery to view the file on my Droid, the name is suddenly correct and is no longer null.
Can someone tell me why the name is null after my code runs, and why the name is correct after viewing the file on my computer rather than on the phone? Is there a way to get the name non-null right when the picture is saved? I'd bet that the name would be correct if I turned my phone off and turned it back on, but I'm not sure how exactly to get force things to work immediately.
Answer is by looking at the sources of MediaStore.
One places is here:
http://devdaily.com/java/jwarehouse/android/core/java/android/provider/MediaStore.java.shtml
Basically they also add a thumbnail representation of the image and instead of
Media.DISPLAY_NAME they use Media.TITLE.
I used that code adapted a little and worked fine.

Categories

Resources