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
Related
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'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()
);
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");
}
After searching an trying how to do it, I put this question due to my failures.
The point is I am trying to show in a Gallery several pictures that I get from a http server. The thing is when I open the activity for first time, if the picture is not in a specific folder, local cache, I display a default picture and them I launch a download of the picture by a AsyncTask. What I try is as soon as the picture has been downloaded and save in the folder, to update/refresh the gallery in the activity to display the picture. If I slip left/right the gallery the picture appear when the gallery is refresh, but I want this happen avoiding to slip the gallery.
Also comment that the base adapter is all the time with the right reference to the picture, the default picture is selected if the file does not exist in the folder.
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageBitmap(getPicturePath(position + 1));
i.setLayoutParams(new Gallery.LayoutParams(wHeight / 3, wHeight / 4));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
private Bitmap getPicturePath(int picture) {
String picturePath = Environment.getExternalStorageDirectory()
.getAbsolutePath()
+ "/pictures/" + picture
+ ".jpg";
File f = new File(picturePath);
if (f.exists())
return BitmapFactory.decodeFile(picturePath);
else
return BitmapFactory.decodeResource(mContext.getResources(),
R.drawable.no_image);
}
I try to invalidate() in the gallery and also notifyDataSetChanged() in the adapter, baseadapter, but it does not work, the pictures on the screen do not change.
Any suggestion how to solve this?
Many thanks.
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.