I've really been stumped by this problem.
My app uses Facebook SDK and has a fragment (PersonalFragment) attached to the Main Activity, part of which displays the current user's name in a Text View (R.id.textView), and the current user's image in an ImageView (R.id.imageView)
My problem is I use the following logic to get the profile picture URI, and then use verified code to get a Bitmap from a URI. The following code results in a simple: "e\FNF Exception: Profile Picture" being written into the Log.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v=inflater.inflate(R.layout.fragment_personals, container, false);
ImageView image =(ImageView) (v.findViewById(R.id.imageView));
if(Profile.getCurrentProfile()!=null)
{
try {
Uri uri = (Profile.getCurrentProfile().getProfilePictureUri(100, 150));
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);
image.setImageBitmap(bitmap);
image.setScaleType(ImageView.ScaleType.FIT_XY);
}
catch(FileNotFoundException f)
{
Log.e("FNF Exception","Profile Picture");
}
catch(IOException i)
{
Log.e("IO Exception", "Profile Picture");
}
}
((TextView)v.findViewById(R.id.textView)).setText(Profile.getCurrentProfile().getName());
As one can see, the try-catch is within the if statement, so Profile.getCurrentProfile() is certainly not null. Furthermore, the code correctly inputs the user's name into the textview. Only the profile picture code throws a FileNotFoundException.
Suggestions?
The uri parameter in the line
MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);
is meant to reference images that are on the device or are part of your app's package using a file:// or content:// prefix. It can't be used to load image from the internet.
You could instead use something like this:
URL url = new URL(Profile.getCurrentProfile().getProfilePictureUri(100, 150).toString());
Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
An alternative, easier way of displaying a profile picture would be to use the ProfilePictureView included in the Facebook SDK:
<com.facebook.login.widget.ProfilePictureView
android:id="#+id/profilePicture"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
and
((ProfilePictureView)findViewById(R.id.profilePicture)).setProfileId(
Profile.getCurrentProfile().getId()
);
Related
I have a custom camera app with two activities. The first activity (MainActivity) allows the user to take a photo with a custom camera. I would like to open this photo in the second activity (DrawActivity) so that the user can eventually draw on it. My MainActivity works great, the camera opens, snaps and saves the image to the phones external storage. I am having trouble with the DrawActivity opening the photo. I am passing what I believe to be the Uri of the image from my MainActivity to my DrawActivivty with the following code:
Intent myIntent = new Intent(MainActivity.this, DrawActivity.class);
myIntent.putExtra("mybitmap",values.toString());
startActivity(myIntent);
Where values are defined (before I create the above intent):
FileOutputStream fout = new FileOutputStream(imageFile);
fout.write(ostream.toByteArray());
fout.close();
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATE_TAKEN,
System.currentTimeMillis());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.MediaColumns.DATA, imageFile.getAbsolutePath());
MainActivity.this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
I can not get the DrawActivity to display the photo. I set up a Toast message just to see what my DrawActivity was receiving, and I got this message. I set up the toast code with this:
Bundle extras = getIntent().getExtras();
String imageuri = extras.getString("mybitmap");
Toast.makeText(this, imageuri, Toast.LENGTH_LONG).show();
and try to pass it to my image view with:
ImageView iv = (ImageView) findViewById(R.id.imageDisplay);
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(imageuri));
iv.setImageBitmap(bitmap);
iv.setVisibility(View.VISIBLE);
When I pull up the photo properties in the stock photo app on my phone the image path is this. Am I passing the wrong information? Too much information? Am I parsing the passed information incorrectly?
You are passing the values which is not the imageUri, and you can get the bitmap from the image path. So, pass the file path only from your MainActivity to DrawActivity. After passing the path, you can get the Bitmap by the following code.
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
mImageView.setImageBitmap(bitmap);
Rob i suggest you this kind of User friendly architecture. Because going to another activity is ok but if you can do everything in the same activity like this is good approach as i know.
I have relativelayout (A template) where it contain textboxes whose content is populated at runtime.On clicking save button,I need to save the template as an image in SD card.Is it possible.
I referred the below link:
How do I convert a RelativeLayout with an Imageview and TextView to a PNG image?
But the image saved cannot be opened.
Is my requiremnet possible.Or else please advice how can I achieve it.
I am behind this for several days.I am new to ANdroid.Please help.
Thanks in Advance.
You can pass any view or layout to devBitmapFrmViewFnc function and get the bitmap. You can save the bitmap in jpeg using devImjFylFnc.
|==| Dev Bitmap Image from View :
Bitmap devBitmapFrmViewFnc(View viewVar)
{
viewVar.setDrawingCacheEnabled(true);
viewVar.buildDrawingCache();
return viewVar.getDrawingCache();
}
|==| Create a JPG File from Bitmap :
static void devImjFylFnc(String MobUrlVar, Bitmap BitmapVar)
{
try
{
FileOutputStream FylVar = new FileOutputStream(MobUrlVar);
BitmapVar.compress(Bitmap.CompressFormat.JPEG, 100, FylVar);
FylVar.close();
}
catch (Exception ErrVar) { ErrVar.printStackTrace(); }
}
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
I'm developing an app for Android, I'm using the Gallery widget, and I've resized it to fullscreen mode, so it displays one image at a time.
<com.example.librosapp.MyGallery
android:id="#+id/examplegallery" android:layout_width="1920px"
android:layout_height="1020px"
android:padding="0px"
android:layout_marginTop="-20px"
/>
And here is a part of my Activity's code:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imgView = new ImageView(cont);
//Here are my changes:
File imgFile = new File("sdcard/Libreria/0/0/0.JPG");
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
//The app runs OK til here:
imgView.setImageBitmap(myBitmap);
//BOOM! Exception
}
imgView.setLayoutParams(new MyGallery.LayoutParams(1950, 1000));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
return imgView;
}
I don't know which exception am I getting, because I can't debug here, I'm using the .APK in my device. (The only way that I have to debug this, is with the virtual device, and I donnow why it runs really slow.
Am I doing something wrong?, that code works perfect if I use the same image, but as a project Resource (using setImageDrawable)
This is happening because myBitMap is null. myBitMap is null because the file path is invalid. My guess would be the file path should be /sdcard/Libreria/0/0/0.JPG
I have some images that are saved to a directory on the Android device when the application starts--I would like to be able to display these images in a Gallery, but so far I haven't been able to do it.
I was following the sample Gallery code here, but it uses drawable resource IDs instead of file paths. I found this solution that is similar to what I'm looking for, except it uses ImageView instead of Gallery.
So the code using ImageView would look something like this:
File imgFile = new File(“/data/data/com.myproject.example/files/someImage.png”);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
}
The above code works, but I'm not sure how to do it with a Gallery. I have been searching for answers and trying different things, but I'm v new to Android development and I feel like I'm in a bit over my head.
Any help is appreciated. Thank you in advance.
Basically I think you just need to put your two examples together.
Use the HelloGallery example as a start, however you want to change the code inside the getView() method of the ImageAdapter to call setImageBitmap() on the ImageView instead of setImageResource().
You will need an array/collection of file paths of images you want to load.
What you need to do is something like this:
public ImageAdapter(Context c, int itemId) {
context = c;
imgArr = GlobalStore.getItem(itemId).getPhotos();
TypedArray attr = context.obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = attr.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);
attr.recycle();
}
as you can see this is basically a copy from Gallery tutorial. In the constructor imgArr variable is loaded with an array of JPG file names. These were for example read from a database.
Then in the getView function you have something like this...
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
String tmpStr = appContext.getFilesDir() + File.separator + "photos" + File.separator + imgArr.get(position);
Bitmap bitmap = BitmapFactory.decodeFile(tmpStr);
imageView.setImageBitmap(bitmap);
imageView.setLayoutParams(new Gallery.LayoutParams(350, 300));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
As you can see getFilesDir() gets your applications data location where it stores files, then let's imagine all photos are in "photos" directory, you build a path and attach a file name from the imgArr array. Since this is called for every photo you just use the passed position variable.
If you don't have an array of photos then maybe the way is to build it by reading the directory where you store photos, load all of the filenames in an array and then do this.
Then you do the rest on the gallery side as in the gallery tutorial.