i am beginner in android programing, and i created a Full Screen Image Slider app using this tutorial:
Link:
http://www.androidhive.info/2013/09/android-fullscreen-image-slider-with-swipe-and-pinch-zoom-gestures/
in this tutorial it reads images from Sd Card but i want to read images from drawable folder, i don't want to access images from SD Card instead access images from drawable.
This is the way I've seen, I hope it is correct:
private Bitmap bitmap = null;
private void createBitmap(Context context)
{
Resources resources = context.getResources();
Drawable d = resources.getDrawable(R.drawable.imagename);
Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
this.bitmap = bitmap;
}
It can be done like this:
Drawable drawable = this.getResources().getDrawable(R.drawable.image);
where this refers to the context of the actual activity.
OR
you can just give the name of the image and then you can get the image using getIdentifier,
getResources().getIdentifier(name,"drawable", getPackageName());
Where name will be the name of your image i.e - "image1"
Related
I have a linear layout that has multiple fields that are to be filled by the user. The fields are to be filled with text or decimal numbers and they are all TextInputLayouts with one date picker. After the user has filled those fields I have a review button which once clicked shows a preview of all the values entered in an image view. I am showing the image by using bitmap and canvas and sending the bitmap as an argument to my material bottom sheet with the following code.
private Bitmap getDailySheetBitmap() {
Bitmap bitmap = Bitmap.createBitmap(completeSheetLinearLayout.getWidth(), completeSheetLinearLayout.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Log.d("Bitmap", "bitmap size: "+bitmap.getByteCount());
completeSheetLinearLayout.draw(canvas);
return bitmap;
}
And the code for sending arg is
private void buildBottomSheet(Bitmap bitmap) {
Bundle args = new Bundle();
args.putParcelable("Bitmap", bitmap);
ViewDailySheet viewDailySheet = new ViewDailySheet();
viewDailySheet.setArguments(args);
viewDailySheet.show(getChildFragmentManager(), "DailySheet");
}
The problem is that after the user reviews the fields and changes are made to those fields and when the user again clicks on the review button, the bitmap with the same old values is shown. I have tried recycling bitmap, invalidating the view, requestLayout() and forceLayout() method. But none of it is helping.
This is the code that puts bitmap into image view.This is in bottom sheet dialog fragment.
if (getArguments() != null) {
bitmap = getArguments().getParcelable("Bitmap");
binding.dailySheetImage.invalidate();
binding.dailySheetImage.setImageBitmap(bitmap);
getArguments().clear();
}
What am I doing wrong or missing?
The bitmap only captures what it thinks has changed. If the text field(not the data, just UI) is not changed it will show the same text field value as before. So I refreshed the whole layout as it would re-draw the entire layout by the following code.
private void refreshView() {
completeSheetLinearLayout.setVisibility(View.GONE);
completeSheetLinearLayout.setVisibility(View.VISIBLE);
}
I put this code just before creating the bitmap.
I've a screen in android, which has multiple controls like a Listview and TextView and some buttons to save the record.
If there are more question (say 15) the Listview is displayed in scroll bar as the Listview has fixed length. We have used the below code to take screenshot of the screen on save but when we have more questions in Listview the screen shot is taken only for the record available in the screen. Please suggest how I can take screenshot of all the questions in the Listview when we have more.
Code Used to take screenshot where View is the LinerLayout defined in .xml file
Sample :1
public Bitmap screenShot(View v) {
v.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false);
return bitmap;
}
Sample :2
Bitmap bitmap = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Drawable bgDrawable = v.getBackground();
if (bgDrawable != null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
v.draw(canvas);
I am following the tutorial in this link to learn about RecyclerView. The tutorial loads images from drawable folder. When I do the same the app just runs out of memory. So I made a little change to resize images.
#Override
public void onBindViewHolder(MyAdapter.ViewHolder viewHolder, int i) {
viewHolder.title.setText(galleryList.get(i).getImage_title());
viewHolder.img.setScaleType(ImageView.ScaleType.CENTER_CROP);
viewHolder.img.setImageResource((galleryList.get(i).getImage_ID()));
}
Changed the 3rd line to this
viewHolder.img.setImageDrawable(scaleImage(galleryList.get(i).getImage_ID(), ScaleFactor));
And defined scaleImage as
private Drawable scaleImage(int imageId, float scaleFactor)
{
Drawable image = context.getResources().getDrawable(imageId);
if ((image == null) || !(image instanceof BitmapDrawable)) {
return image;
}
Bitmap b = ((BitmapDrawable)image).getBitmap();
int sizeX = Math.round(image.getIntrinsicWidth() * scaleFactor);
int sizeY = Math.round(image.getIntrinsicHeight() * scaleFactor);
Bitmap bitmapResized = Bitmap.createScaledBitmap(b, sizeX, sizeY, false);
image = new BitmapDrawable(context.getResources(), bitmapResized);
return image;
}
But as I scroll RecyclerView lags until It loads the coming rows which I think is because I load each image individually and then resize it. Is there a way to do this asychronously?
It's become cause you are resizing the image, it's a heavy process.
try to resize images async or use image loader libs.
There are some useful libraries you can use for loading images like Glide
Also, you can see this Picasso v/s Imageloader v/s Fresco vs Glide
I need set image resource by Send parameter (String src)
I have this code
public void getSource(View view , String src){
ImageView image = (ImageView)findViewById(R.id.img);
image.setImageURI(src);
}
How do I solve this problem ؟
// method to call to set image
private void setImage(int src) {
ImageView iv = findViewById(R.id.your_image_view);
iv.setImageResource(src);
}
//Pass your resource and use it like this :-
setImage(R.drawable.plus_active);
If you want to set image from local storage, then you have to get the uri of the image and use setImageUri like this:
public void getSource(View view , String srcImageUri){
ImageView image = (ImageView)findViewById(R.id.img);
image.setImageURI(Uri.parse(src));
}
If you have image in drawable folder , then you have to set image like this:
image.setImageResource(R.drawable.imageName);
And if you want to set image from web url, then you have to use libraries like picaso or fresco.
use getResources().getIdentifier(); (there you can pass the image name String as a parameter to getIdentifier() function) to get the resourceID of the named image and then use setImageResource(resourceID); to set the image to ImageView.
public void getSource(View view , String src){
int resID = getResources().getIdentifier( src, "drawable", Context.getPackageName());
ImageView image = (ImageView)findViewById(R.id.img);
image.setImageResource(resID);
}
you may need to pass the Context as a parameter to your getSource() function.
I'm trying to display a captured image from a camera intent, but when I call a showPhoto method the image isn't shown in the ImageView. There is no error output to the stack trace so I'm not sure how the issue can be debugged.
Does anyone know what is wrong with the method that its not showing the captured image?
The showPhoto method is as follows:
private void showPhoto(Uri photoUri) {
String filePath = photoUri.getEncodedPath();
File imageFile = new File(filePath);
//File imageFile = new File(photoUri.getPath());
if (imageFile.exists()){
Drawable oldDrawable = photoImage.getDrawable();
if (oldDrawable != null) {
((BitmapDrawable)oldDrawable).getBitmap().recycle();
}
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
BitmapDrawable drawable = new BitmapDrawable(this.getResources(), bitmap);
photoImage.setScaleType(ImageView.ScaleType.FIT_CENTER);
photoImage.setImageDrawable(drawable);
}
}
This is the tutorial that I've followed: http://www.linux.com/learn/tutorials/722038-android-calling-the-camera
The device I'm testing on is JellyBean 4.1 and this is the link to the complete class:
http://hastebin.com/oqokupulol.java
Use BitmapOptions.inJustDecodeBounds to find out the dimension of the image first
Typically, Android won't load images larger than 4000px wide/tall
If you find out the image is too large, you can use BitmapOptions.inSampleSize to downscale the image so that Android would load it
And in fact, you can directly use ImageView.setImageBitmap(Bitmap) instead of setImageDrawable