I am making an application in Android, which requires sending bitmap object from one activity and displaying that bitmap object sent on the second activity page. But , I am getting a blank screen
Here is a sample of my code for sending the Bitmap object :-
Intent intent = new Intent(Display2.this, Display3.class);
ImageView iv = (ImageView) findViewById(R.id.imageView1);
Bitmap bitmap = Bitmap.createBitmap(iv.getWidth(), iv.getHeight(), Bitmap.Config.RGB_565);
intent.putExtra("BitmapImage", bitmap);
startActivity(intent);
Now the part of code on second activity to retrieve the sent object and displaying it on screen :-
public class Display3 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Bitmap bitmap = (Bitmap)getIntent().getParcelableExtra("BitmapImage");
ImageView myIV = (ImageView) findViewById(R.id.imageView1);
bitmap = bitmap.createBitmap(myIV.getWidth(), myIV.getHeight(), Bitmap.Config.RGB_565);
myIV.setImageBitmap(bitmap);
setContentView(R.layout.display3);
}
}
can anyone suggest whats wrong in this part ?
Thanks !
Bitmap implements Parcelable object, so you could always pass it in the intent like below :
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);
and retrieve it on the other end:
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
this is not good for passing bitmap from one activity to another..
You can simply name you Bitmap as static first.
then create a method like
public static Bitmap getBitmap(){
return bitmap;
}
then you can simply call from other activities,
bitmapexistingclass.getBitmap();
if we use intents for passing bitmap we will get some errors check this question How to pass bitmap from one activity to another
from your code above you are creating an empty bitmap:
ImageView iv = (ImageView) findViewById(R.id.imageView1);
Bitmap bitmap = Bitmap.createBitmap(iv.getWidth(), iv.getHeight(), Bitmap.Config.RGB_565);
I do not see any reason why from this code you'll get anything else then blank black bitmap with hight and width equals to the imageView hight and width
what you are expecting from android to fill drawing in Bitmap itself , you are creating an Empty Bitmap Object with height and width equavalent to ImageView object;
as Bitmap implements Parcelable you can simply put it in bundle and retrive on other Activity
Use below two methods to convert a bitmap to string and vice versa. Then you can pass the string with intents from activity to another activity. I too do the same in my application.
Then we dont need to use ParcelableExtra and serialzable class.
public String convertBitmapToString(Bitmap src) {
if(src!= null){
ByteArrayOutputStream os=new ByteArrayOutputStream();
src.compress(android.graphics.Bitmap.CompressFormat.PNG, 100,(OutputStream) os);
byte[] byteArray = os.toByteArray();
return Base64.encodeToString(byteArray,Base64.DEFAULT);
}
return null;
}
public Bitmap getBitMapFromString(String src){
Bitmap bitmap = null;
if(src!= null){
byte[] decodedString = Base64.decode(src.getBytes(), Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(decodedString,0,decodedString.length);
return bitmap;
}
return null;
}
Related
I have a class where a photo is captured and Bitmap is stored in the ImageView. In another class I got brush tool (canvas). If I create a new Bitmap in a second class I can draw on canvas but I can't draw on the captured image from the first class. How could I move the bitmap from first class to another?
Method of the first class of captured image
protected void onActivityResult (int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
bitmap = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(bitmap);
}
Method of the second class with the brush tool and empty canvas
public void init(DisplayMetrics metrics) {
int height = metrics.heightPixels;
int width = metrics.widthPixels;
mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
currentColor = DEFAULT_COLOR;
strokeWidth = BRUSH_SIZE;
}
Save the bitmap and get its path. Now pass that path into your activity intent. Then retrieve it from other activity using intent.getExtras()
To save bitmap you can check this answer 'How to save a bitmap on internal storage'
If its not an activity then use set get methods
public void setBitmap( Bitmap b ) {
bitmap= b;
}
public Bitmap getBitmap() {
return bitmap;
}'
Check this tutorial for set get methods
'http://www.javawithus.com/tutorial/get-and-set-methods'
Just take a one static variable of bitmap
Set your received bitmap into that variable and access that variable by its class name directly into your second activity.
I want to click a button from Activity 1 with image to show an image for Activity 2, but it's not working.
Activity1:
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
// Bundle bundle = new Bundle();
Drawable drawable=img1.getDrawable();
Bitmap bitmap= ((BitmapDrawable)drawable).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
intent.putExtra("picture", b);
// String ten = edt.getText().toString();
// bundle.putString("tenkh", ten);
// intent.putExtras(bundle);
startActivity(intent);
Activity2:
ImageView image = (ImageView) findViewById(R.id.img2);
TextView txtTen= (TextView) findViewById(R.id.tv1);
Intent goiIntent=getIntent();
Bundle extras = goiIntent.getExtras();
byte[] b = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
image.setImageBitmap(bmp);
You should show us the logical to help us understand the problem.
However i think your problem is that the byte [] is to big to be pass in an intent.
One workaround could be to have an abstract class with a public static field of type byte[]. Before broadcasting your intent, set this field with your data and in the next activity read this data and don't forget to set the field to null when you don't need it to avoid memory leak.
Create a class ImageHelper as follow :
public abstract class ImageHelper {
public static byte [] image;
}
In your activity 1, before launching the intent, instead of intent.putExtra("picture", b); use ImageHelper.image = b;
Then in your activity 2, instead of byte[] b = extras.getByteArray("picture");, you use byte[] b = ImageHelper.image;.
Activity 1
intent.putExtra("BitmapImage", bitmap);
Activity 2
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
ImageView image = (ImageView) findViewById(R.id.img2);
image.setImageBitmap(bitmap);
I have a grid view when i click an item inside the grid view and its going to another activity and view it there my code something like this :
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), MainActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
Activity which is im going to view
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("s");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(imageAdapter.moodpic[position]);
You try to get Extra with wrong key here getInt("s").
Try this
int position = getIntent().getIntExtra("id",0);
you should change with
int position = i.getExtras().getInt("id" , 0);
instead this
int position = i.getExtras().getInt("s");
This is because you have put "id" as a key so whenever you are retrieving then that key must be same and by default the value of int is 0.
I would highly recommend a different approach.
It's possible if you REALLY want to do it, but it costs a lot of memory and is also slow. It might not work if you have an older phone and a big bitmap. You could just pass it as an extra, for example
intent.putExtra("data", bitmap)
A Bitmap implements Parcelable, so you can put it in an extra. Likewise, a bundle has putParcelable f you want to pass it inbetween activities, I would store it in a file. That's more efficient, and less work for you. You can create private files in your data folder using MODE_PRIVATE that are not accessible to any other app.
First Convert Image into Byte Array and then pass into Intent and in next activity get byte array from Bundle and Convert into Image(Bitmap) and set into ImageView.
Convert Bitmap to Byte Array:-
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
// Bitmap bmp = BitmapFactory.decodeFile(path); You can use this also.
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Pass byte array into intent:-
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);
Get Byte Array from Bundle and Convert into Bitmap Image:-
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
2) First Save image into SDCard and in next activity set this image into ImageView.
3) Pass Bitmap into Intent and get bitmap in next activity from bundle, but the problem is if your Bitmap/Image size is big at that time the image is not load in next activity.
I am trying to create a camera intent and a sub portion of the code is given below.
public void onPictureTaken(byte[] data, Camera camera) {
String dat = new String(data);
byte[] datas = dat.getBytes();
preview.setVisibility(View.GONE);
ImageView iv2 = (ImageView)findViewById(R.id.iv1);
Bitmap bMap = BitmapFactory.decodeByteArray(datas, 0, datas.length);
iv2.setImageBitmap(bMap);
}
This keeps the imageview blank, however when I give
Bitmap bMap = BitmapFactory.decodeByteArray(data, 0, data.length);
the imageview is properly loaded. Am I doing any mistake in byte array to string conversion??
why do you need these two lines?
String dat = new String(data);
byte[] datas = dat.getBytes();
use data directly in the decodeByteArray
You dont even need to convert the byte[] to String. Just use it as it is.
public void onPictureTaken(byte[] data, Camera camera) {
preview.setVisibility(View.GONE);
ImageView iv2 = (ImageView)findViewById(R.id.iv1);
// ensure ImageView is visible.
iv2.setVisibility( View.VISIBLE);
Bitmap bMap = BitmapFactory.decodeByteArray(data, 0, data.length);
iv2.setImageBitmap(bMap);
}
Just use the above modified code.
This seems simple, I am trying to set a bitmap image but from the resources, I have within the application in the drawable folder.
bm = BitmapFactory.decodeResource(null, R.id.image);
Is this correct?
Assuming you are calling this in an Activity class
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);
The first parameter, Resources, is required. It is normally obtainable in any Context (and subclasses like Activity).
Try this
This is from sdcard
ImageView image = (ImageView) findViewById(R.id.test_image);
Bitmap bMap = BitmapFactory.decodeFile("/sdcard/test2.png");
image.setImageBitmap(bMap);
This is from resources
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
If the resource is showing and is a view, you can also capture it. Like a screenshot:
View rootView = ((View) findViewById(R.id.yourView)).getRootView();
rootView.setDrawingCacheEnabled(true);
rootView.layout(0, 0, rootView.getWidth(), rootView.getHeight());
rootView.buildDrawingCache();
Bitmap bm = Bitmap.createBitmap(rootView.getDrawingCache());
rootView.setDrawingCacheEnabled(false);
This actually grabs the whole layout but you can alter as you wish.
If you have declare a bitmap object and you want to display it or store this bitmap object. but first you have to assign any image , and you may use the button click event, this code will only demonstrate that how to store the drawable image in bitmap Object.
Bitmap contact_pic = BitmapFactory.decodeResource(
v.getContext().getResources(),
R.drawable.android_logo
);
Now you can use this bitmap object, whether you want to store it, or to use it in google maps while drawing a pic on fixed latitude and longitude, or to use some where else
just replace this line
bm = BitmapFactory.decodeResource(null, R.id.image);
with
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.YourImageName);
I mean to say just change null value with getResources() If you use this code in any button or Image view click event just append getApplicationContext() before getResources()..
Using this function you can get Image Bitmap. Just pass image url
public Bitmap getBitmapFromURL(String strURL) {
try {
URL url = new URL(strURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}