android - display image from gallery - android

I have several images stored in content://media/external/images/media. I would like to display by using ImageView and add the image view to the table. here is my code.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 24;
final Bitmap b = BitmapFactory.decodeFile("content://media"+getFileLocation(), options);
ImageView milestoneImageView = new ImageView(this);
milestoneImageView.setImageBitmap(b);
recordRow.addView(milestoneImageView);
When I print out the getFileLocation(), it says "/external/images/media/62". So I added "content://media" to make a full path, but it is not working...
Can someone point out why the code above is not working?

I think "/external/images/media/62" its a Uri not a real file path..
So instead of
final Bitmap b = BitmapFactory.decodeFile("content://media"+getFileLocation(), options);
Just try
final Bitmap b = BitmapFactory.decodeStream(getContentResolver().openInputStream("content://media"+getFileLocation()), null,options);
And let me know what happen..

Related

How to get the drawable from relative layout in android

What i have done:
I have set the bitmap for a relative layout as below
RelativeLayout root;
root= (RelativeLayout) itemView.findViewById(R.id.root);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap preview_bitmap = BitmapFactory.decodeResource(context.getResources(),godImages[position], options);
Drawable drawable = new BitmapDrawable(context.getResources(),preview_bitmap);
root.setBackgroundDrawable(drawable);
What i am trying to do:
I am trying to get the bitmap from the relativelayout back again that was set
so that i can pass to another activity.
Note: i am aware of using the intents to padd data between activities only i want to know how to get the bitmap
Any ideas on How to achieve this ?
There are a couple of ways you can do this:
1. Try:
RelativeLayout layout = (RelativeLayout)findViewById(R.id.rel);
layout.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(layout.getDrawingCache());
layout.setDrawingCacheEnabled(false);
2. Or try:
RelativeLayout layout = (RelativeLayout)findViewById(R.id.rel);
Bitmap b = Bitmap.createBitmap( layout.getLayoutParams().width, layout.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
layout.layout(layout.getLeft(), layout.getTop(), layout.getRight(), layout.getBottom());
layout.draw(c);
To get the current ImageView
1. To get current position, use
ViewPager viewPager = getView().findViewById(r.id.pager);
int position = viewPager.getCurrentItem();
2. To get the current View, use
View currentView = viewPager.getChildAt(int position);
This currentView is the View created by the onCreateView() method of the current Fragment.
3. To get the ImageView, use
ImageView currentImage = (ImageView)currentView.findViewById(R.id.image);
where R.id.image is the ImageView object you want from the current Fragment.

android displaying image after capture

I have requirement like I have a custom Horizontal ScrollView with text and image(image which is invisible) and button to start camera intent.
When I select any item I am getting Id of that scroll item. When I click on button to capture photo my code is taking photo and getting the path from sdcard.but image view is not getting visibility even when I use imageView.setVisibility(View.VISIBLE); this line code.
ImageView I declared globally in main class.
Please give me an idea in this issue.
=============================================================================
This is the code I done after capturing photo inside on
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 5;
bb = BitmapFactory.decodeFile(imageFile.getAbsolutePath(),
options);
MediaStore.Images.Media.insertImage(getContentResolver(),
bb, null, null);
System.out.println("Bitmap is : " + bb);
System.out.println("Tab Id to keep photo is : " + tabId);
imageView.setImageBitmap(bb);
imageView.setVisibility(View.VISIBLE);
filePath = imageFile.getAbsolutePath();
Thanks,
UdaySekhar

Gallery is loading all the images but gallery should be load one image at a time

i have created a gallery for display images haveing pinchzoom with goto next and previous by touch . i were use to visit https://github.com/kilaka/ImageViewZoom library . i saw Gallery first upload all the images and then it is displaying images which is good activity. but gallery can upload max 4MB images , if images size go to greater than 4MB then it gives us Bitmap outofmemory error. i have used to bitmap.recycle() but it is recycle all the images.
do here any way, so my gallery upload images one by one from sdcard and when i will go to next or previous images by touch , my currently images memory should be delete . and next images should be uploaded in gallery memory . so my gallery can upload a number of images .
this is my code , this code is link from visit https://github.com/kilaka/ImageViewZoom :-
gallery = new GalleryTouch(this);
gallery = (GalleryTouch) findViewById(R.id.image);
gallery.setSpacing(20);
arrayAdapter = new ArrayAdapter<ImageView>(this, -1) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
bookM= position; return getItem(position);
}
};
if(!(it.isEmpty() )) {
Collections.sort(it);
for(int i=0; i<it.size();i++) {
ImageViewTouch imageView = new ImageViewTouch(imageTouch.this);
imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
final Options options = new Options();
options.outHeight = (int) scaleHeight;
options.outWidth = (int) scaleWidth;
options.inScaled = true;
options.inPurgeable = true;
options.inSampleSize = 2;
String photoURL = it.get(i);
bitmap = BitmapFactory.decodeFile(photoURL,options);
imageView.setImageBitmap(bitmap);
arrayAdapter.add(imageView);
}
gallery.setAdapter(arrayAdapter);
}
Note:- in this code, "it" is a variable of List it; and list having a list of path of image from sd card like as sdcard/images/one.jpg , sdcard/images/two.jpg ........ etc.
please expert help me ,
thank you for your important time.

android:enlarging an image on click

I am making an android app in which i need to enlarge the image size on click.
I am getting these images downloaded from server and then adding these images programmatically in the activity and hence setting id of images programmatically. Now my problem is that when i send the imageid o another activity to enlarge it it is taking only the id of last image.
and hence gives the following exception.
Unable to start activity ComponentInfo{com.emx.OnDaMove/com.emx.OnDaMove.FullImageActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x5
i am using the following code for making images and adding them to table rows.
imageUrl = "http://ondamove.it/English/images/users/";
imageUrl = imageUrl + listObject.get(j).getImage();
image = new ImageView(this);
image.setPadding(20, 10, 0, 0);
image.setId(j+1);
imageId=image.getId();
tr.addView(image);
and the following method for clickliastener:
private OnClickListener clickImageListener = new OnClickListener() {
public void onClick(View v) {
Intent fullScreenIntent = new Intent(v.getContext(),FullImageActivity.class);
fullScreenIntent.putExtra(ProfilePageNormalUser.class.getName(),imageId) ;
ProfilePageNormalUser.this.startActivity(fullScreenIntent);
}
};
and the activity in which images are to be enlarged is:
ImageView myimage = (ImageView) findViewById(R.id.imageviewEnlarged);
Intent intent = getIntent();
int imageId = (Integer) intent.getExtras().get(ProfilePageNormalUser.class.getName());
System.out.println("********"+imageId +"**********");
InputStream is = this.getResources().openRawResource(imageId);
Bitmap originalBitmap = BitmapFactory.decodeStream(is);
Matrix imageMatrix = new Matrix();
imageMatrix.postRotate(90);
Bitmap scaledBitmap = Bitmap.createBitmap(originalBitmap, myimage.getWidth(), myimage.getHeight(), originalBitmap.getWidth(), originalBitmap.getHeight(), imageMatrix, false);
myimage.setImageBitmap(scaledBitmap);
myimage.setScaleType(ScaleType.FIT_XY);
Ya surely it will give the last Id since the value of imageId will be update on every loop. This will store the last known value during looping.
You should use array instead of simple variable.
or you can use v.getId in onClick method. (simpler one)

Android - how to compress or downsize an image?

ImageButton avatarButton = (ImageButton) findViewById(R.id.ImageButton_Avatar);
avatarButton.setImageResource(R.drawable.avatar);
strAvatarFilename = "Image.jpg";
final Uri imageUriToSaveCameraImageTo = Uri.fromFile(new File(Environment.getExternalStorageDirectory()+"/Folder/"+strAvatarFilename));
avatarButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent pictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
pictureIntent.putExtra( MediaStore.EXTRA_OUTPUT, imageUriToSaveCameraImageTo );
startActivityForResult(Intent.createChooser(pictureIntent, strAvatarPrompt), TAKE_AVATAR_CAMERA_REQUEST);
Editor editor = Preferences.edit();
editor.putString(PREFERENCES_AVATAR, imageUriToSaveCameraImageTo.getPath());
editor.commit();
}
});
I have this code. It takes a photo and then stores it in two locations, the default location on the SDcard and the /folder/ file i want the photo to be stored in also. Once taken i refresh the screen using this...
ImageButton avatarButton = (ImageButton) findViewById(R.id.ImageButton_Avatar);
String strAvatarUri = Preferences.getString(PREFERENCES_AVATAR, "");
Uri imageUri = Uri.parse(strAvatarUri);
avatarButton.setImageURI(null);
avatarButton.setImageURI(imageUri);
This refreshes the image button so the user can see the image they have taken. However, this is appears very big and fills the screen. Is there any way i can compress or downsize the image so the user can see it at a reasonable size? thanks
Bitmap thumb = Bitmap.createScaledBitmap (BitmapFactory.decodeFile(photoPath), 96, 96, false);
Where the second and third parameters to createScaledBitmap are the width and height respectively.
http://developer.android.com/reference/android/graphics/Bitmap.html#createScaledBitmap%28android.graphics.Bitmap,%20int,%20int,%20boolean%29
EDIT: It now occurred to me that you can do this in a faster, more efficient way, using BitmapFactory.Options and the inSampleSize option:
BitmapFactory.Options opts = new BitmapFactory.Options ();
opts.inSampleSize = 2; // for 1/2 the image to be loaded
Bitmap thumb = Bitmap.createScaledBitmap (BitmapFactory.decodeFile(photoPath, opts), 96, 96, false);
This way you will save even more memory and the downsizing should take less time.
You can mess with the ImageButton's scaleType properties (to fitXY for example, or cropCenter), and by setting a fixed size to LayoutPreferences (height and width) you'll be all set.
The Android Library SiliCompressor provide this functionality for you to compress your images while keeping the image quality.

Categories

Resources