I got this image from the server. The image is not in the project resource folder. The text is working correctly, but the image is not showing in another activity.
MyAdapter objAdapter1;
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Item item = (Item) objAdapter1.getItem(position);
Intent intent = new Intent(ChooseDriver.this, DriverDetail.class);
intent.putExtra(ID, item.getId());
intent.putExtra(NAME, item.getName().toString());
intent.putExtra(IMAGE, item.getImage().toString());
image.buildDrawingCache();
Bitmap image= image.getDrawingCache();
Bundle extras = new Bundle();
extras.putParcelable("imagebitmap", image);
intent.putExtras(extras);
startActivity(intent);
}
});
try this:
Main Activity:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mItem.getIcon().compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
newIntent.putExtra("image", byteArray);
Second Activity:
byte[] byteArray = getIntent().getExtras().getByteArray("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
img.setImageBitmap(bmp);
In FirstActivity,
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("YourBitmap", bitmap);
and in SecondActivity,
Intent intent = getIntent();
Bitmap bitmap = (Bitmap)intent.getParcelableExtra("YourBitmap");
May it help you.
Related
i'm trying to send an image from an arraylist one activity to another through intent in recyclerView. But in putextra method it shows error like cannot resolve method 'putextra(java.lang.string,android.widget.imageview)'
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ImageView img = images.get(position);
Intent intent = new Intent(context,Result.class);
intent.putExtra("Image",img);
context.startActivity(intent);
}
});
You cannot pass an ImageViewas an extra, the object you are passing must be a Parcelable or a Serialiazable.
Your ImageView is also relevant for this activity only. If you are trying to send an Image to another activity, it is better to send the path to the image.
You cannot send image directly because intent does not support it
Try this Solution
You can send image using ByteArray though eg
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ImageView img = images.get(position);
img.buildDrawingCache();
Bitmap bmp = ((BitmapDrawable)img.getDrawable()).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent intent = new Intent(this, Result.class);
intent.putExtra("Image", byteArray);
context.startActivity(intent);
}
});
in onCreate() of Result Activity class
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("Image");
//convert it to bitmap again
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
//set image to bitmap
image.setImageBitmap(bmp);
SOURCE
Passing image from one activity another activity
At the end of the day finally i did it, you simply create an array of images(NOTE: don't create arrayList) in case u want to choose between multiple images and simply pass these images through an integer. below is the code...
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.image.setImageResource(images[position]);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int img = images[position];
Intent intent = new Intent(context,Result.class);
intent.putExtra("Images", img);
intent.putExtra("personName", Name);
intent.putExtra("Gender", Gender);
context.startActivity(intent);
}
});
}
//In targeted activity
public void getdata(){
if (getIntent().getExtras() !=null) {
gender = getIntent().getStringExtra("Gender");
Name = getIntent().getStringExtra("personName");
int image = getIntent().getIntExtra("Images",0);
mytext(gender,Name,image);
}
}
public void mytext(String gender,String Name,int img){
textView1 = findViewById(R.id.txt1);
textView2 =findViewById(R.id.txt2);
imageView = findViewById(R.id.Imagename);
textView1.setText(Name);
textView2.setText(gender);
imageView.setImageResource(img);
}
//then in onCreate method call "getdata();"
I uploaded an Image from my Internal Storage and want now to pass this
selected Image to a new Activity. I do not pass an Image from the
drawable. I pass an Image which I selected from my Internal Storage. I
tried to pass this Image via the R.id
ImageView imageView_selectedImage;
imageView_selectedImage =(ImageView)findViewById(R.id.imageView_selectedImage);
Button button_goToNextActivity = (Button) findViewById(R.id.button_goToNextActivity);
button_goToNextActivity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(this, nextActivity.class);
intent.putExtra("resId", R.id.selectedImage);
startActivity(intent);
}
}
imageView.invalidate();
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
Then pass the byte array to the next activity.
Intent intent = new Intent(this, nextActivity.class);
intent.putExtra("image", imageEncoded);
startActivity(intent);
Get the byte from the intent second activity.
Bitmap bmp;
byte[] byteArray = getIntent().getByteArrayExtra("image");
bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
Set the bitmap to the imageview of the second activty
imageview.setImageBitmap(bmp);
Don't pass imageview to next activity.
You can pass uri of Image which is selected from gallery.\
Get Uri for selected image in onActivityResult method like below code:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SELECT_GALLERY) {
if (data != null) {
imageSelectedFromGallery(data);
pictureUri=data.getData();
imageView_selectedImage.setImageURI(pictureUri);
}
}
}
Note**: make pictureUri instance variable instead of local variable.
Pass Image Uri to next Activity like below code:
Intent intent=new Intent(Main2Activity.this,NextActivity.class);
intent.putExtra("pictureUri",pictureUri);
startActivity(intent);
Get Image Uri in Next Activity and set into imageView :
ImageView imageview=findViewById(R.id.imageview);
Bundle bundle=getIntent().getExtras();
if(bundle!=null) {
String pictureUri = bundle.getString("pictureUri");
imageview.setImageURI( Uri.parse(pictureUri));
}
I hope its work for you.
I have developed an app that allows the following:
1. Load image from gallery and view using ImageView.
2. Save image from ImageView into gallery.
3. An alert dialog box pops out which opens into another layout when clicked 'yes'.
I would like to pass the image bitmap from the first activity to the second activity. I have followed few example but the image is not being passed. My coding are as follows.
LoadImage.java:
public class LoadImage extends Activity {
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
private ImageView img;
Button buttonSave;
final Context context = this;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load_image);
img = (ImageView)findViewById(R.id.ImageView01);
buttonSave = (Button) findViewById(R.id.button2);
((Button) findViewById(R.id.Button01))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
});
buttonSave.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
img.setDrawingCacheEnabled(true);
Bitmap bitmap = img.getDrawingCache();
String root = Environment.getExternalStorageDirectory().toString();
File newDir = new File(root + "/PARSE");
newDir.mkdirs();
Random gen = new Random();
int n = 10000;
n = gen.nextInt(n);
String fotoname = "Photo-"+ n +".jpg";
File file = new File (newDir, fotoname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
Toast.makeText(getApplicationContext(), "Saved to your folder", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
}
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set title
alertDialogBuilder.setTitle("Review the Answer?");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to submit answer!")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
Intent intent = new Intent(LoadImage.this, TestImage.class);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
intent.putExtra("byteArray", bs.toByteArray());
startActivity(intent);
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
TestImage.java:
public class TestImage extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_image);
Intent intent = getIntent();
ImageView img = (ImageView) findViewById(R.id.ImageView01);
if(getIntent().hasExtra("byteArray")) {
ImageView previewThumbnail = new ImageView(this);
Bitmap bitmap = BitmapFactory.decodeByteArray(
getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);
previewThumbnail.setImageBitmap(bitmap);
}
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("byteArray");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
img.setImageBitmap(bmp);
}
}
The 'intent' is the second activity is grey out stating variable 'intent' is never used.
Can someone help me to resolve this issue. Any help is much appreciated. Thank you.
UPDATED!!
LoadImage.java:
buttonSave.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
img.setDrawingCacheEnabled(true);
final Bitmap bitmap = img.getDrawingCache();
String root = Environment.getExternalStorageDirectory().toString();
File newDir = new File(root + "/PARSE");
newDir.mkdirs();
Random gen = new Random();
int n = 10000;
n = gen.nextInt(n);
String fotoname = "Photo-"+ n +".jpg";
File file = new File (newDir, fotoname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
Toast.makeText(getApplicationContext(), "Saved to your folder", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
}
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set title
alertDialogBuilder.setTitle("Review the Answer?");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to submit answer!")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
Intent intent = new Intent(LoadImage.this, TestImage.class);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bytes = stream.toByteArray();
intent.putExtra("bitmapbytes",bytes);
startActivity(intent);
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
TestImage.java:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_image);
byte[] bytes = getIntent().getData().getByteArrayExtra("bitmapbytes");
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
ImageView img = (ImageView) findViewById(R.id.ImageView01);
img.setImageBitmap(bmp);
}
}
Activity
To pass a bitmap between Activites
Intent intent = new Intent(this, Activity.class);
intent.putExtra("bitmap", bitmap);
And in the Activity class
Bitmap bitmap = getIntent().getParcelableExtra("bitmap");
Fragment
To pass a bitmap between Fragments
SecondFragment fragment = new SecondFragment();
Bundle bundle = new Bundle();
bundle.putParcelable("bitmap", bitmap);
fragment.setArguments(bundle);
To receive inside the SecondFragment
Bitmap bitmap = getArguments().getParcelable("bitmap");
Transferring large bitmap (Compress bitmap)
If you are getting failed binder transaction, this means you are exceeding the binder transaction buffer by transferring large element from one activity to another activity.
So in that case you have to compress the bitmap as an byte's array and then uncompress it in another activity, like this
In the FirstActivity
Intent intent = new Intent(this, SecondActivity.class);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPG, 100, stream);
byte[] bytes = stream.toByteArray();
intent.putExtra("bitmapbytes",bytes);
And in the SecondActivity
byte[] bytes = getIntent().getByteArrayExtra("bitmapbytes");
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Bitmap implements Parcelable, so you could always pass it in the intent:
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);
and retrieve it on the other end:
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
Yes You can send Bitmap through Intent
Intent intent =new Intent(Context ,YourActivity.class);
intent.putExtra("key",bitmap);
To receive the bitmap in YourActivity.class
Intent intent=getIntent();
Bitmap image=intent.getParcelableExtra("key");
This will work fine for Small Size bitmap
When the size of Bitmap is large it will Fail to send through Intent. Beacuse Intent are mean to send small set of data in key value pair.
For this You can send the Uri of the Bitmap through Intent
Intent intent =new Intent(Context ,YourActivity.class);
Uri uri;// Create the Uri From File Or From String.
intent.putExtra("key",uri);
In your YourActivity.class
Intent intent=getIntent();
Uri uri=intent.getParcelableExtra("key");
Create the Bitmap From the Uri.
Hope It will help you.
maybe your bitmap is too large.And you will find FAILED BINDER TRANSACTION in your logcat. So just take a look at FAILED BINDER TRANSACTION while passing Bitmap from one activity to another
I have try convering with bitmap but the problem is that the image look bed here is what i did :
submitPhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
customImage.setDrawingCacheEnabled(true);
customImage.buildDrawingCache();
//Bitmap bit =Bitmap.createBitmap(customImage.getWidth(), customImage.getHeight(),Bitmap.Config.ARGB_8888);
//Canvas canvas = new Canvas(bit);
//personPhoto.draw(canvas);
Bitmap bitmap=Bitmap.createBitmap( customImage.getDrawingCache());
customImage.setDrawingCacheEnabled(false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
Intent intent=new Intent(OnPhotoTakedActivity.this,OnPhotoSubmit.class);
intent.putExtra("bitmap",scaleDownBitmap(bitmap,120,getApplicationContext()));
startActivity(intent);
}
});
and in the next activity :
Intent intent = getIntent();
Bitmap bitmap=intent.getExtras().getParcelable("bitmap");
imageView.setImageBitmap(bitmap);
I tryed all the codes on this and other webs but still the image look bad
thanks any way.
I'm trying to draw a Bitmap on a ImageView, but it is not showing... I created the Bitmap and stored on a Intent Extra, but on the new activity I'm not able to draw it on the ImageView.
Here is my code, calling the new activity:
public void onClickShare(View v) {
Intent myIntent = new Intent(this, Share.class);
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
myIntent.putExtra("createdImg", b);
startActivity(myIntent);
}
And on the new activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.share);
backgroundImg = (ImageView)findViewById(R.id.imageView1);
Intent myIntent = getIntent();
Bitmap bitmap = (Bitmap) myIntent.getParcelableExtra("createdImg");
backgroundImg.setImageBitmap(bitmap);
}
What am I missing? Thanks!
Try to send it as ByteArray and decode it in the receiving activity.
ByteArrayOutputStream stream = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent in1 = new Intent(this, Share.class);
in1.putExtra("image",byteArray);
in the second activity
byte[] byteArray = getIntent().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
You can also try like this
Bundle bundle = new Bundle();
bundle.putParcelable("createdImg", b);
intent.putExtras(bundle);
and in next activity
Bundle bundle = this.getIntent().getExtras();
Bitmap bmap = bundle.getParcelable("createdImg");
as given in this tutorial http://android-er.blogspot.com/2011/10/pass-bitmap-between-activities.html
I took it a little bit from every answer, plus other researches, and I came up with the code below that worked nicely:
Calling the new activity:
public void onClickShare(View v) {
Intent myIntent = new Intent(this, Share.class);
// create bitmap screen capture
Bitmap bitmap;
View v1 = v.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
// create a new one with the area I want
Bitmap bmpLast = Bitmap.createBitmap(bitmap, 25, 185, 430, 520);
v1.setDrawingCacheEnabled(false);
//create an empty bitmap with the size I need
Bitmap bmOverlay = Bitmap.createBitmap(480, 760, Bitmap.Config.ARGB_8888);
//draw the content on the bitmap
Canvas c = new Canvas(bmOverlay);
c.drawBitmap(bmpLast, 0, 0, null);
//compress the bitmap to send to other activity
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmpLast.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
//store compressed bitmap
myIntent.putExtra("createdImg", byteArray);
startActivity(myIntent);
}
And on the new activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.share);
//decompress the bitmap
byte[] byteArray = getIntent().getByteArrayExtra("createdImg");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
//set the imageview with the bitmap
backgroundImg = (ImageView)findViewById(R.id.imageView1);
backgroundImg.setImageDrawable(new BitmapDrawable(getResources(), bmp));
}
Thanks everyone! =)