I am working in an android application and I want to Clear my Bitmap data. The scenario is that I am taking a screen capture of an Widget(Imageview) and I am storing it in a Bitmap. This action comes in a Button click. SO after some time I get a Memory error. So I want to clear the values in the bitmap. So to do that I have done the following code :
The BitMap variable is mCaptureImageBitmap
public void ButtonClick(View v)
{
mCaptureImageBitmap.recycle();
mCaptureImageBitmap=null;
View ve = findViewById(R.id.mainscreenGlViewRelativeLayout);
ve.setDrawingCacheEnabled(true);
mCaptureImageBitmap = ve.getDrawingCache();
}
But I get an error of NullPoint exception. Please help me
You have most of the right code but in the wrong order. Try doing something like this
public void ButtonClick(View v)
{
Bitmap mCaptureImageBitmap;
final View ve = findViewById(R.id.mainscreenGlViewRelativeLayout);
ve.setDrawingCacheEnabled(true);
mCaptureImageBitmap = ve.getDrawingCache();
// Do something useful with your image here
mCaptureImageBitmap.recycle();
mCaptureImageBitmap = null;
}
Try this code ...
ve.setDrawingCacheEnabled(true);
// Add these lines
ve.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
ve.layout(0, 0, ve.getMeasuredWidth(), ve.getMeasuredHeight());
ve.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(ve.getDrawingCache());
mCaptureImageBitmap = b;
ve.setDrawingCacheEnabled(false); // clear drawing cache
Related
If I want to change a image to another by the click of a button and then back to the previous image again by the click of the same button on imageview in android studio how to do that in short and easiest way? As I am new to it I am not familiar with all the functions of imageview.
For example:-
I wrote this code to do what I needed after a lot of failure in finding a easier way.
int i=0;
public void change(View v){
int img[] = {R.drawable.cat1,R.drawable.cat2};
ImageView cat = findViewById(R.id.imageView2);
if(i==0)
{cat.setImageResource(img[1]);
i=1;}
else {cat.setImageResource(img[0]);
i=0;}
}
Before I was trying to do something like this:-
public void change(View v){
ImageView cat = findViewById(R.id.imageView2);
if(cat.getDrawable()==R.drawable.cat2;)
{cat.setImageResource(R.drawable.cat1);}
else
{cat.setImageResource(R.drawable.cat1};
}
But it kept giving error that they have different type and I also tried some other functions named getId() but it didnt work either...
So my main objective is, is there a function through which I can campare the resource of image view directly with the image in drawable folder and how to implement it in if else or some other conditional statement?
The first approach should work, but the i value seems not tightly coupled to the ImageView. So, instead you can set a tag to the ImageView that equals to the current drawable id:
Initial tag:
ImageView cat = findViewById(R.id.imageView2);
cat.setImageResource(R.drawable.cat1);
cat.setTag(R.drawable.cat1);
And click listener callback:
public void change(View v){
ImageView cat = findViewById(R.id.imageView2);
int tag = (int) cat.getTag();
if(tag == R.drawable.cat2){
cat.setImageResource(R.drawable.cat1);
cat.setTag(R.drawable.cat1);
} else {
cat.setImageResource(R.drawable.cat2);
cat.setTag(R.drawable.cat2);
}
}
You could try StateListDrawable, LevelListDrawable, with each state/level, it will change image depend on your state/level
I am doing a notepad application There are also pictures that I saved as SQLite database ine byte in the note list. I have shared the picture . As you can see from the picture, I want users to be able to view the picture when they want to view the note. For this I created an imageView in the NoteDetail class and I want to put the selected image there. When the users click the note which one is selected, I want that users can see their image in NoteDeteail.class. how can I do that. The solutions I searched from the internet did not offer me a solution.
I have tried this but not worked;
Here is part of my adapter. I can get id, title, and description. All is ok except for image.
final byte[] outImage = note.getImages();
ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
final Bitmap theImage = BitmapFactory.decodeStream(imageStream);
holder.takenPhoto.setImageBitmap(theImage);
holder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
Toast.makeText(context, "" + notes.get(position).getTitle(), Toast.LENGTH_SHORT).show();
Intent noteDetail = new Intent(context, NoteDetail.class);
noteDetail.putExtra(Common.NOTE_DETAIL_ID, notes.get(position).getNoteID());
noteDetail.putExtra(Common.NOTE_DETAIL_TITLE, notes.get(position).getTitle());
noteDetail.putExtra(Common.NOTE_DETAIL_DESCRIPTION, notes.get(position).getDescription());
noteDetail.putExtra(Common.NOTE_DETAIL_IMAGE,notes.get(position).getImages());
context.startActivity(noteDetail);
}
});
Here is my part of NoteDetail.class. Intents are working well except for image of course.
if (getIntent() != null) {
ID = Integer.parseInt(getIntent().getExtras().get(Common.NOTE_DETAIL_ID).toString());
}
setDefaultBackroundColor();
setDefaultTextType();
setDefaultTextColor();
taken_title.setText(getIntent().getExtras().get(Common.NOTE_DETAIL_TITLE).toString());
taken_description.setText(getIntent().getExtras().get(Common.NOTE_DETAIL_DESCRIPTION).toString());
taken_photo.setImageBitmap((Bitmap) getIntent().getParcelableExtra(Common.NOTE_DETAIL_IMAGE));
I'm trying to send an image to zoom activity from my main_activity.
I have a onclick function:
case R.id.imageViewHero:
String image = ViewHolder.this.post.getImageUrl();
Intent intentv = new Intent(context, Zoom.class);
Bundle extras = new Bundle();
extras.putParcelable("imagebitmap", image);
intentv.putExtras(extras);
context.startActivity(intentv);
break;
the problem is the string image, I don't know what to do next to send it to zoom. Any ideas?
my zoom.class if needed:
public class Zoom extends Activity {
#SuppressLint("NewApi")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zoom);
Bundle extras = getIntent().getExtras();
Bitmap bmp = (Bitmap) extras.getParcelable("imagebitmap");
ImageView imgDisplay;
Button btnClose;
imgDisplay = (ImageView) findViewById(R.id.imgDisplay);
btnClose = (Button) findViewById(R.id.btnClose);
btnClose.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Zoom.this.finish();
}
});
imgDisplay.setImageBitmap(bmp );
}
}
The code is correct for most part except one mistake in making the Bitmap from the image URL.
You cannot make a Bitmap image from an image URL like you have done.
Its already been answered on StackOverflow.
Check this link on how to convert an Image from an Image URL to a Bitmap - How to get bitmap from a url in android?
Basically, you are getting an Image URL from Intent which you are passing from the previous Activity, in the Zoom Activity, save it to a String variable, make Bitmap from it, set the Bitmap to the ImageView.
In onClick method, you seem to send imageUrl. However in Zoom.java you're treating it as Bitmap.
You can use a library like Picasso to populate ImageView using Image URL
I'm new to Android and I'm trying to change the content of an ImageView with a button and if I press the button again the image changes back. I thought it would be easy with an if-else statement but I have been looking around in the ImageView API and I don't see the method that allows me to get the image that is being displayed in that moment... Any ideas?
Here is my code so far...
boton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
imagen.setImageResource(R.mipmap.imag1);
}
});
I didn't copy the rest of the code because I dont think it's necessary
You can get the image of the button with using this method:
Bitmap bitmap = ((BitmapDrawable)imagen.getDrawable()).getBitmap();
To set image of a button with another bitmap you can use:
imagen.setImageBitmap(bitmap);
We have foreground image where some area of that image are transparent & we are showing another image underlying in transparent region. So when we are going to save it, it will be saving full screen. so we want particular area of screen to be saved in the gallery.
Here is the code which we used to save..
save.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mDecorView = getWindow().getDecorView();
runOnUiThread(new Runnable() {
public void run() {
mDecorView.invalidate();
mDecorView.post(this);
}
});
View v1 = mDecorView.getRootView();
System.out.println("Root View : "+v1);
v1.setDrawingCacheEnabled(true);
try {
BitmapSave(v1.getDrawingCache());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Please help us friends.
Thanks
You want to modify the bitmap you get from the screenshot before saving it.
Use this :
Bitmap bmp=v1.getDrawingCache());
int startX=0,startY=0,endX=30,endY=230;
Bitmap bitmap=Bitmap.createBitmap(bm, startX,startY, endX, endY);
BitmapSave(bitmap);
Modify the start and end x,y positions to get the screen co-ordinates you want to save.