Storing and retrieving app icons of all installed apps programmatically - android

I am building an app in which I programmatically collect details of all the apps installed on the device (appName, packageName, appIcon, and some other appDetails).
However, I observe that fetching and storing these app details in a data structure takes a perceivable amount of time. I have tried storing some of the app details using SharedPreferences so that I can retain data between different app executions and not initiate a fetch each and every time my app is launched.
Storing the app icons (drawables) however, takes up a lot of space and makes me wonder if there is a more efficient way of retaining the above mentioned app data between app executions, unless of course, new apps are installed after the last time the installed apps were programmatically fetched.
So, my question is, is there a efficient way of storing app icons (drawables) such that they don't take up too much space, and at the same time, the app set can be retained between different app executions?

I believe your best shot is saving the list of Apps installed in your App database. You can store the drawable icon of the app as encoded string.
String image = getImageFromDrawable(app.loadIcon(pm));
where app is ApplicationInfo and pm is PackageManager.
public String getImageFromDrawable(Drawable drawable){
String img = null;
if(drawable instanceof BitmapDrawable) {
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] arr = stream.toByteArray();
img = Base64.encodeToString(arr, Base64.URL_SAFE);
return img;
}
return null;
}
You can then store the details of the app in your application database.

This can be done by converting icons to String and storing them in a file using Parceler. To do so,
Create an App POJO class with icon as an attribute of Bitmap type
Make your class Parcelable
Initialize an ArrayList of this class type with all icons to be stored
Parcel and store these objects in a file
Now, attributes of type Bitmap are not Parcelable, so you will have to convert them to parcelable type. You may use this function for conversion:
static String bitMapToString(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
return Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
}
And the following function to convert the data back to Bitmap type.
static Bitmap stringToBitMap(String encodedString) {
try {
byte[] encodeByte = Base64.decode(encodedString, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
} catch (Exception e) {
e.getMessage();
return null;
}
}

Related

wrong Base64 string after bitmap photo encoding

So in my app I have possibility to take photo. What I want to do later is to sent this photo to server using API as Base64 String. Here's my code to encode bitmap (photo) to String :
public static String convertPhotoToBase(Bitmap bmp) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
Log.e(TAG, Base64.encodeToString(byteArray, Base64.DEFAULT));
return Base64.encodeToString(byteArray, Base64.DEFAULT);
}
Looks ok, right ? Well, it's not and I don't why String I'm receiving is not encoded correctly. I add, that bitmap is ok , cuz I can display it in ImageView without any problems.
String I'm getting looks like this :
iVBORw0KGgoAAAANSUhEUgAAA8AAAAZACAIAAAA5PLyzAAAAA3NCSVQICAjb4U/gAAAgAElEQVR4 nKS9WbbsOK4kagZ5DON9VM1/hncL9j7QEKTkHnFXKXOd8K2GBNETBEH+3//z/0mSBIgkAJpIfmhx kXQ6ABEkP/YPyPjT3SXe963b3f8AAEah28l2JYckwQUAEgDAyerDPtERZMpXZAJJGgyU7rjpDing dNoN6MOrHrm7S4o/CUGEkbzOUQgASAYAzOHIQAl0wam8KGab8bKMOTrPjurpFfdv0d3piUwDAZiZ iGpTd6Oa/PCSvJvqITgpCaSMJEhelIEY8CDw6C7pdpMUQzPBkHBe5gAsxytZ9nvTScoR4FGQKCmo TF6QJbi4AVwSjLyAK6h2Xbgk6f5z9/u+izRXQEVSuqmFIuGW5EqwA/MkA2Y3q+HfNUCRvK5/mnYu Rl8ApCs+9gSYxEXyn4vVOxurt4Q7OeRPdyMZgNnVbGBmwcIBHoA73lxUXhc8byb+5YFgkkB2DVjw SQwnW7DgwIATja6ApAFz9x1RnowqgfkIAHiFdMxBNfxBkUSFw7nIAUBySqSasS8kQDfkUgwo+CpY SNVzoB3AxezLEBLBACbgUWmD6zLSsl8sWVBxQcjmuNT8jNIZJHF7DqEaiAZbHGgQSS65lgSwhm2B HKNCRtyB2wFXgJ56oWldVMOnaFrASe7+t4SXJE0AvJuZ4zn+7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9dDDgZlJXClyxBszo4tmGKJp
It looks like correct Base64 string but if you check it - it's not. Do you have any idea why is that ? Is it bitmap issue ,or what ? Any help, please ?
use this method to encode the bitmap you want
public static String enBitmap64(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
bitmap.recycle();
byte[] array = stream.toByteArray();
return Base64.encodeToString(array, 0);
}
and this method to decode the string
public static Bitmap deBitmap64(String string) {
byte[] decodedString = Base64.decode(string, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
return decodedByte;
}
However, before using encode64 you need to know few things
Encode and decode bitmaps needs to be done in background (NOT IN THE MAIN THREAD) it hangs the UI specially if the bitmap was large, use Asynctask, Service or Runnable (Asynctask is best choice).
It's not recommended to encode large images and upload it as string to server, encode and decode only in two cases.
First, if all you images are small.
Second, if you have large images and somewhere you app you display it as thumbnail For exp; In friends search of Facebook you see small images(thumbnail) you click it becomes large(original image). so there is small image and large image, when you are in friends search page you only request(download) the small image (low resolution) from the server no need to request(download) such large image(high resolution), but the two images are stored in the server. the small one could be saved as encoded but NOT the large one, large images is to be upload to server though HttpClient, Volley or HttpURLConnection.. there are more
I have almost the same issue,
encoding and decoding were working perfectly on the java side.
But my Base64 string couldn't be decoded on the server side.
comparing different base64s helped me to see that my base64 break into lines so using following code helped to correct base64
base64string.replace("\n", ""));

Android compare/search multiple images

I got a list of images and an input image, I want to output true if the image exist in my list.
Images will consider to be equal if they got the same raw data.
Is there an easy way of doing this in Android?
I saw this method:
boolean Bitmap.sameAs(Bitmap)
But it require to test binary data against each of my images in the list and this is kind of expensive. Is there a better way doing this?
And what if I change the definition of equal images to "similar", in such way that if the images got over 90% visual similarity they will consider to be equal.
Here is the solution for the first part where equal images are those who got the same raw data.
You can hash each image and compare it by hash, this way you will test the image raw data only once.
String toSHA1(Bitmap bitmap){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
try {
String sha1 = toSHA1(byteArray);
stream.close();
return sha1;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
String toSHA1(byte[] data) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-1");
BigInteger bigInteger = new BigInteger(1, md.digest(data));
return bigInteger.toString(Character.MAX_RADIX);
}

Go from JPG to Bitmap then store in a Database

I am developing an android app where I have users upload profile pictures from their phones which are then saved into a database. What I have so far is I create a bitmap from the user's picture, then I compress it with the bitmap compress function and then my server receives the byte array and stores it into the database. I can then send the byte array back but I cannot figure out how to convert the byte array into a bitmap again. Here are some code snippets of what I am doing:
(Here I am just taking a dummy picture from in the app and sending it to the server)
Bitmap t = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.j0);
mProtocol.updateUser(t);
(The updateUser command)
public void updateUser(Bitmap b){
try {
mOutput.writeObject(20);
b.compress(Bitmap.CompressFormat.JPEG, 90, mSocket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
(Finally, I try to recreate the bitmap from the byte array)
Bitmap t = BitmapFactory.decodeByteArray(user.getProPic(),0, 20000);
But after I run the app, the bitmap still remains blank. Any thoughts of what is going on?

Encoding images for URI use

I am busy with a small android app. The appplication connects to a WCF using restful that in turn connects to a database. The application does work. Now I want to send a Image to the database.
[OperationContract]
[WebGet(UriTemplate = "sendImage/{seq}", ResponseFormat = WebMessageFormat.Json)]
string sendImage(string seq);
On the android application I encode a Image making use of BASE64.
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
TextView textView = (TextView) findViewById(R.id.defaults);
textView.setText(encodedImage);
This encryption does work. The problem is that I now what to send the string encodedImage to the WCF to write to the database. Thus the string is passed in the URI.
"htp://_____.__/serv/ManagementSystem.svc/sendImage/" + encodedImage
The problem is that the string that I get from encoded contains / characters.
/9J/DASDFkFASDF/.....
This causes a problem in the URI as it picks up the / as a change of parameter, thus picking up that it is not a valid request. Is there a way to remove the / without compromising the decoding or is there a better way of converting a image to a string and back to an image?
If you have control over the server side, I can at least think of two options:
1) After base64 encoding, replace all / with _, which is valid for url, has no particular pre-assigned meaning, and it is not used by base64. On the server side, you apply the opposite replace;
2) This is more straightforward that option 1, instead of get, you use put or post.

Saving and recalling an image with a placeholder image as default in android

I currently have a questionnaire, where each question is added dynamically in a db, which the app then receives for instructions. When it uses the camera, it sets the image just fine, using the code:
void setResponse(Bitmap image) {
captureButton.setImageBitmap(image);
}
Simple enough, right? When I click update, to save it to the sqlite db, I save it as a base64 string using this:
String getResponse() {
String response = "";
try
{
Bitmap bitmap = Bitmap.createBitmap(captureButton.getDrawingCache());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image = stream.toByteArray();
response = Base64.encodeToString(image, 0);
}
catch (Exception e)
{
Log.e("Capture image error", e.getMessage());
e.printStackTrace();
}
return response;
}
And then when I go back into the questionnaire, if it has data already, it just loads it up using this:
void setResponse(String response) {
byte[] decodedString = Base64.decode(response, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
captureButton.setImageBitmap(decodedByte);
}
I currently have pretty much the same system for signatures using the gestureOverlayView. But for this, I have a placeholder image defined in the CameraQuestionView.xml file:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:src="#drawable/placeholder"
android:id="#+id/cameraQuestionCaptureButton"/>
But for some reason, when loading back up, the placeholder image is displayed instead of the saved image in the db. I tried invalidating the view, but that didn't work either.
Thanks!
Forgot to do
captureButton.setDrawingCacheEnabled(true);
when saving my response, ergo, no bitmap was saved in the first place. Took me a while to track down, and I doubt this'll help anyone else since it was just a silly mistake of another problem.

Categories

Resources