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

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");
}

Related

Android BitmapDecode in listview

I use the folowing to get a bitmap from my device and display it in a listview
pic1="Harris1.jpg"
pic2="Harris2.jpg"
pic3="Harris3.jpg"
Bitmap bmp = BitmapFactory.decodeFile("/storage/emulated/0/Pictures/" + pic1);
Bitmap bmp1 = BitmapFactory.decodeFile("/storage/emulated/0/Pictures/" + pic2);
Bitmap bmp2 = BitmapFactory.decodeFile("/storage/emulated/0/Pictures/" + pic3);
Bitmap[] image={bmp,bmp1,bmp2};
for(int i=0;i<text1.length;i++)
{
item_details.setImage(image[i]);
}
this works fine but is there a way to buildup the Bitmap {} image without having to put a line for each = BitmapFactory.decodeFile?
I want to be able to read the filenames from a database which I can do but sometimes there are just 3 pictures but other times here may by 50 and I want to be able for the routine to do the {bmp,bmp1,bmp2 etc...} automatically
Any ideas?
Your help appreciated
Mark
Based on your code sample. Using the String.format method is the simplest way to achieve your goal.
String picFormat="Harris%d.jpg";
for(int i=0;i<text1.length;i++)
{
String pic = String.format(picFormat, i+1);
Bitmap bmp = BitmapFactory.decodeFile("/storage/emulated/0/Pictures/" + pic);
item_details.setImage(bmp);
}

Setting facebook's profile picture as a LinearLayout background

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

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;
}
}

How do I get the initial url of an ImageView formed from a Bitmap

Initially I created a bitmap from an external URL by using a bitmapFactory. I then put the bitmap in an Image view, like:
ImageView mim = (ImageView) findViewById(R.id.moviepix);
String mi = "http://xxxxxxx.com/"+ result.movie_pix;
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(mi).getContent());
mim.setImageBitmap(bitmap);
Now I need to reverse engineer the url from the ImageView.
All help is appreciated.
You can't, but you can however store the URL for later use with ImageView.setTag()

Set Imageview to show image in sdcard?

I have a image stored in SD card of my phone. I want to show it in a image view. I know the location of the file. On the oncreate of the activity is there a simple way to say something like
ImageView img = (ImageView) findViewById(R.id.imageView1);
String path = Environment.getExternalStorageDirectory().toString() "Images/image.jpg";
img.setsrc = path ;
Please let me know if there is any way to do this. Thank you.
Bitmap bmp = BitmapFactory.decodeFile(pathName);
ImageView img;
img.setImageBitmap(bmp);
Hope this helps.
I have this snippet that may help:
File imageFile = new File("/sdcard/example/image.jpg");
if(imageFile.exists()){
ImageView imageView= (ImageView) findViewById(R.id.imageviewTest);
imageView.setImageBitmap(BitmapFactory.decodeFile(imageFile.getAbsolutePath()));
}
:)

Categories

Resources