Setting facebook's profile picture as a LinearLayout background - android

I have been trying with no avail to set my facebooks profile picture as a Linearlayout background in my application.
here are the two ways I tried to do this:
first way: got the uri from the profile picture and converted it to drawable
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
LinearLayout banner = (LinearLayout)findViewById(R.id.banner);
Profile profile = Profile.getCurrentProfile();
if(profile != null){
// profile_pic_id.setProfileId((profile.getId()));
Drawable d;
Uri uri = profile.getLinkUri();
//also tried profile.getProfilePictureUri(random_num,random_num)
try {
InputStream inputStream = getContentResolver().openInputStream(uri);
d = Drawable.createFromStream(inputStream, uri.toString());
} catch (FileNotFoundException e) {
d = getResources().getDrawable(R.drawable.blue_material);
}
banner.setBackgroundDrawable(d);
}
}
now this always threw an exception so the Drawable i get is the blue_material one (the Drawable set in the catch block), so if anyone knows why its throwing an exception all the time I'd be grateful.
second way: converted the ProfilePictureView to ImageView and then used getDrawable() on the ImageView.
ProfilePictureView profilePictureFB=(ProfilePictureView)findViewById(R.id.pic);
profilePictureFB.setProfileId((profile.getId()));
ImageView fbImage = ( ( ImageView)profilePictureFB.getChildAt( 0));
banner.setBackgroundDrawable(fbImage.getDrawable());
now this resulted in having a background with the default picture facebook puts when someone has no profile pic.
AND whenever I use decodeStream an exception gets thrown.(example below)
URL img_url =new URL("https://graph.facebook.com/"+profile.getId()+"/picture?type=normal");
Bitmap bmp =BitmapFactory.decodeStream(img_url.openConnection().getInputStream());//bmp a bitmap
I didnt want to resort to asking this question on stackoverflow because I wanted to solve it myself, but I tried for so very long, so I had to get some help.
Thanks for anyone who answers :)

I solved it!!
i used imageLoader, and for some reason the url works with it, here is the code:
imageView = (ImageView) findViewById(R.id.pic);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageLoader = ImageLoader.getInstance();
Profile profile = Profile.getCurrentProfile();
if(profile != null){
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true).build();
imageLoader.displayImage("http://graph.facebook.com/" + profile.getId() + "/picture?width=400&height=400", imageView, options);
// banner.setBackgroundDrawable(imageView.getDrawable());
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
the method setScaleType stretches my imageview across all myLinearLayout.
for more information about the universal image loader visit this link:
https://github.com/nostra13/Android-Universal-Image-Loader

I m not sure but this what i used to do
img_url =new URL("https://graph.facebook.com/"+uname+"/picture?type=normal");
bmp =BitmapFactory.decodeStream(img_url.openConnection().getInputStream());//bmp a bitmap
linearlayout.setImageBitmap(bmp);//your linear layout

Related

new to android - not able to display picture in imageview twice

after clicking on a button and taking a picture, I want to display it in an imageview as in the following:
Bitmap bMap = BitmapFactory.decodeFile(path);
ImageView myImage1 = (ImageView) findViewById(R.id.ivReturnedPic);
myImage1.setImageBitmap(bMap);
This works great the first time you take a picture, the picture displays fine on the screen. But if I click on the button again to take a second picture, it just errors out on the phone. Emulator seems to work fine, so I have no error message to share with you. Do you think ADB bridge might be helpful in this case ? Now, if I comment out the following piece of code, no error:
myImage1.setImageBitmap(bMap);
May be because bMap is null ? Can someone help me in this issue ?
Check if bMap is null or not before assigning to ImageView
so try this
Bitmap bMap = BitmapFactory.decodeFile(path);
ImageView myImage1 = (ImageView) findViewById(R.id.ivReturnedPic);
if(bMap!=null)
{
myImage1.setImageBitmap(bMap);
}
else
{
Log.d("Checking Bitmap","bMap is null");
}

Android ImageButton setImageBitmap goes on the wrong element

I have a situation with three image buttons:
Take a photo by the camera
Delete the photo
Show a cropped2fit version of the image taken
I'm using the camera of the phone.
My problem is that SOMETIMES, apparently without no explanation, when I set the photo with setImageBitmap, it goes on the wrong imageButton (seems to be always the delete button, but I'm not really sure). Rebooting the device seems to solve the problem.
The code is as simple as it should be: I use findViewById casting the object into an ImageButton and setting the image with setImageBitmap.
ImageView iv = ((ImageView) findViewById(R.id.imgFotoPrima));
iv.setImageBitmap(fn.setupImage(data, PrimaOutputFileUri));
iv.setScaleType(ScaleType.CENTER_CROP);
public static Bitmap setupImage(Intent data, Uri outputFileUri) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2; // SAMPLE_SIZE = 2
Bitmap tempBitmap = null;
Bitmap bm = null;
try {
tempBitmap = (Bitmap) data.getExtras().get("data");
bm = tempBitmap;
Log.v("ManageImage-hero", "the data.getData seems to be valid");
FileOutputStream out = new FileOutputStream(outputFileUri.getPath());
tempBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
}
catch (NullPointerException ex) {
Log.v("ManageImage-other", "another phone type");
bm = otherImageProcessing(options, outputFileUri);
}
catch (Exception e) {
Log.e("ManageImage-setupImage", "problem setting up the image", e);
}
return bm;
}
The elements in the xml have different id.
I've tried the application on a Lenovo (7') and a Galaxy Tab 7 II.
It seems to happen only on the Galaxy Tab.
Could it be a problem on the tablet?? Anyone in my situation?

BitmapFactory causing app to crash on orientation change

I'm writing an app that stores a String in an SQLite database, which represents the filepath of an image on the /sdcard/. I have this code in the onCreate() one of my activities:
final Intent receivedIntent = getIntent();
String imageStr = receivedIntent.getExtras().getString("picture");
ImageView imageView = (ImageView) findViewById(R.id.pPicture);
File file = new File (imageStr);
if (file.exists()) {
Bitmap bitmap = BitmapFactory.decodeFile(imageStr);
imageView.setImageBitmap(bitmap);
}
The code works when I'm first loading the activity, but when I switch between screen orientations, it crashes my application. Any ideas on what I can do to fix this? I'd like to bee able to continue switching between orientations, but I don't need to refresh each time.
Also, I'm somewhat new, so please try to keep your answers not too complicated if possible.
Note that the file does exist in all cases.
I think your app crashes because of OutOfMemoryException. Try recylcing the bitmap in onDestroy():
#Override
public void onDestroy() {
super.onDestroy();
ImageView imageView = (ImageView) findViewById(R.id.pPicture);
Drawable drawable = imageView.getDrawable();
imageView.setImageDrawable(null);
if (drawable instanceof BitmapDrawable) {
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
bitmap.recycle();
bitmap = null;
}
}

Extra space above and below Android Bitmap

I am downloading a bitmap from a website and then displaying it in my application. Whenever I download this image and set it into an ImageView, there is always a lot of extra space above or below the actual image. This extra space is part of the ImageView and is only there after I set the ImageBitmap to the downloaded bitmap.
So, this is making me think that the extra space is somehow part of the bitmap.
However, when I download the same image in a Webview, there is no extra space.
If you have any ideas on why this could be happening, please let me know! Let me know if you need any more information, thanks.
Edit: Here's my code getting the bitmap:
InputStream in = null;
Message msg = Message.obtain();
msg.what = 1;
try{
in = openHttpConnection(_url);
if (in != null)
{
Bitmap bit = BitmapFactory.decodeStream(in);
Bundle b = new Bundle();
b.putParcelable("bitmap", bit);
msg.setData(b);
in.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
_handle.sendMessage(msg);
And this is what I use to then for the ImageView, I get the bitmap from the code above and:
imageV.setImageBitmap(comic);
Edit 2:
After trying this with some other images from different website, I've found that this white space is not always there. Given that, and there's probably not anything wrong with the code, are there any suggestions on removing this extra space since it doesn't show up in the actual image online nor in a webview?
imageSize = din.readInt();
imageName = din.readUTF();
byte b[] = new byte[imageSize];
din.readFully(b);
bmImg = BitmapFactory.decodeByteArray(b,0,b.length);
//This works for me....
//din is DataInputStream object.

creating a drawable from sd card to set as a background in android

I am trying to use an image from the sd card and set it as the background for a relativelayout. I have tried other solutions that i have found here and elsewhere but they havent seemed to work for me. here is my code. I have commented out other ways that i have tried and didnt work. the only thing that worked for me was using setBackgroudnResource and using a resource from the app, but this was just to test to make sure mRoot was set up correctly. when I have tried all the other ways, it just doesn't set anything. Anyone know what I am doing wrong, or if there is a better way to do this?
//one way i tired...
//String extDir = Environment.getExternalStorageDirectory().toString();
//Drawable d = Drawable.createFromPath(extDir + "/pic.png");
//mRoot.setBackgroundDrawable(d);
//another way tried..
//Drawable d = Drawable.createFromPath("/sdcard/pic.png");
//mRoot.setBackgroundDrawable(d);
//last way i tried...
mRoot.setBackgroundDrawable(Drawable.createFromPath(new File(Environment.getExternalStorageDirectory(), "pic.png").getAbsolutePath()));
//worked, only to verify mRoot was setup correctly and it could be changed
//mRoot.setBackgroundResource(R.drawable.bkg);
You do not load a drawable from SD card but a bitmap. Here is a method to load it with the reduced sampling (quality) so the program will not complain if the image is too large. Then I guess you need to process this bitmap i.e. crop it and resize for the background.
// Read bitmap from Uri
public Bitmap readBitmap(Uri selectedImage) {
Bitmap bm = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2; //reduce quality
AssetFileDescriptor fileDescriptor =null;
try {
fileDescriptor = this.getContentResolver().openAssetFileDescriptor(selectedImage,"r");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
finally{
try {
bm = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
fileDescriptor.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bm;
}
The Uri here can be supplied from a gallery picker activity.
The image then can be saved into application resources and loaded into an imageView
private void saveBackground(Bitmap Background) {
String strBackgroundFilename = "background_custom.jpg";
try {
Background.compress(CompressFormat.JPEG, 80, openFileOutput(strBackgroundFilename, MODE_PRIVATE));
} catch (Exception e) {
Log.e(DEBUG_TAG, "Background compression and save failed.", e);
}
Uri imageUriToSaveCameraImageTo = Uri.fromFile(new File(BackgroundSettings.this.getFilesDir(), strBackgroundFilename));
// Load this image
Bitmap bitmapImage = BitmapFactory.decodeFile(imageUriToSaveCameraImageTo.getPath());
Drawable bgrImage = new BitmapDrawable(bitmapImage);
//show it in a view
ImageView backgroundView = (ImageView) findViewById(R.id.BackgroundImageView);
backgroundView.setImageURI(null);
backgroundView.setImageDrawable(bgrImage);
}
File file = new File( url.getAbsolutePath(), imageUrl);
if (file.exists()) {
mDrawable = Drawable.createFromPath(file.getAbsolutePath());
}
I suggest checking that the drawable is being loaded correctly. Some things to try:
Try using a different image on the sd card
Put pic.png in R.drawable and make sure mRoot.setBackgroundResource() does what you expect
After loading the drawable, check d.getBounds() to make sure it is what you expect

Categories

Resources