Sunday, 23 October 2016

how to use global variable in android



                                        
Last week I was required to save one of the values for all the activities in my android app. Since I am a beginner so what I was actually looking for was : 
How to save value across several Activities and all parts of your application context ?
 We can certainly achieve this by creating static variable but it is not the good way as it influences towards memory leaks. The way in Android is to associate your variable with the Application context. Every Application has a context, and Android guarantees that Application context will exist as an instance across your application.
 
Then  finally I got the solution which I wanna share here...

The way to do this is ......
create a class and extends with android.app.Application, and specify your class in the application tag in your AndroidManifest.xml file. Android will create an instance of that class and make it available for your entire application context. You can get object of your class on any activity / broadcast receiver / service in application context(environment) by Context.getApplicationContext() method.


Tuesday, 11 October 2016

Image handling in android



I was working on image handling techniques last week. All I wanted to upload and download the images for my app. There were situations where I can easily handle the uploading and downloading of the images. But then there were also some situations when this technique wasn’t working at all. Well, to be specific, when I was taking images from gallery then it was working very fine which was not the case with images captured by Camera.

In some cases, such as selecting images from Gallery, the below code works fine.


Uri uri_gallery = data.getData();

StorageReference filepath = “your firebase location path”
filepath.putFile(uri_gallery).addOnSuccessListener
(new OnSuccessListener<UploadTask.TaskSnapshot>()
{
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot)
{
String downloadUriGallery = taskSnapshot.getDownloadUrl().toString();
Uri downloadUri = taskSnapshot.getDownloadUrl();
Picasso.with(ChatRoomActivity.this).load(downloadUriGallery).into(imageView);
}
}


But for capturing image from camera needed a different approach. Then I learned about use of bitmap.


if(data.getData()==null)dd
{
bitmap = (Bitmap)data.getExtras().get("data");
}
else
{
try
{
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());
}
catch (IOException e)
{
e.printStackTrace();
}
}



Uri uri_camera = getImageUri(getApplicationContext(),bitmap);
StorageReference filepath =”your Firebase storage path” ;
filepath.putFile(uri_camera).addOnSuccessListener
(new OnSuccessListener<UploadTask.TaskSnapshot>()
{
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot)
{
String downloadCamera = taskSnapshot.getDownloadUrl().toString();
}

}


public Uri getImageUri(Context inContext, Bitmap inImage)
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}


So Finally, I was able to handle images taken from Camera as well for my app. You can always leave a comment below if you too have issues related to handling the images for your project. I will try to fix that issues with best of my knowledge.