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();"
Related
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 am new to android. I am trying to capture the image and store it in firebase. Since we need to convert the image to string and then store it in firebase, I am using the base64 algorithm.
The methods for capturing image and storing it in the database is :
public void capturePhoto(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(photo);
}
}
public void storeDatabase(View v)
{
EditText editRollno = (EditText) findViewById(R.id.rollno);
EditText editname = (EditText) findViewById(R.id.name);
EditText editmarks = (EditText) findViewById(R.id.marks);
Firebase ref1 = ref.child("student_information").child("Student" + n);
ref1.child("Name").setValue(editname.getText().toString());
ref1.child("Rollno").setValue(editRollno.getText().toString());
ref1.child("Marks").setValue(editmarks.getText().toString());
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.abc);//your image
ByteArrayOutputStream bYtE = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, bYtE);
bmp.recycle();
byte[] byteArray = bYtE.toByteArray();
imageFile = Base64.encodeToString(byteArray, Base64.DEFAULT);
ref1.child("Image").setValue(imageFile);
n++;
}
When I click on the submit button, it takes a lot of time to upload the values. Moreover, many a times I can see the line
Skipped 537 frames! The application may be doing too much work on its main thread.
in the Android Monitor.
If I comment the image processing lines, it is working all fine.
Can somebody please tell me how to avoid such error so that the image is uploaded instantly in my database.
For storing it to the database, I would definitely use AsyncTask for this one. So, what you could have is a separate class that does the following:
private class ImageDBManager extends AsyncTask< String, Integer, Void> {
protected Long doInBackground(String... items) {
int count = items.length;
String editRoll = items[0];
String editName = items[1];
String editMarks = items[2];
Firebase ref1 = ref.child("student_information").child("Student" + n);
ref1.child("Name").setValue(editname.getText().toString());
ref1.child("Rollno").setValue(editRoll);
ref1.child("Marks").setValue(editName);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.abc);//your image
ByteArrayOutputStream bYtE = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, bYtE);
bmp.recycle();
byte[] byteArray = bYtE.toByteArray();
imageFile = Base64.encodeToString(byteArray, Base64.DEFAULT);
ref1.child("Image").setValue(imageFile);
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(){
}
}
And then call it like this:
public void storeDatabase(View v) {
ImageDBManager manager = new ImageDBManager();
EditText editRollno = (EditText) findViewById(R.id.rollno);
EditText editname = (EditText) findViewById(R.id.name);
EditText editmarks = (EditText) findViewById(R.id.marks);
manager.execute(new String[] { editRollno.getText(), editname.getText(), editmarks.getText() }
}
If you are capturing the image from the camera, the best reference is here
http://developer.android.com/training/camera/photobasics.html
it's the same idea of starting an activity and implementing onActivityForResult where you get the data from the extras in the result, so instead of getting the bitmap from the R.drawable.abc, you'd get it like this in onActivityForResult
Bitmap imageBitmap = (Bitmap) extras.get("data");
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 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.
First of all im giving the codes what i'm using currently
MainActivity
static final String URL = "http://my .com/images/rss.xml";
static final String KEY_TITLE = "item";
static final String KEY_THUMB_URL = "thumb_url";
// Click event for single list row
gridView.setOnItemClickListener(new OnItemClickListener() {
#SuppressWarnings("unchecked")
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> map2 = (HashMap<String, String>) parent.getItemAtPosition(position);
Intent in = new Intent(getApplicationContext(), FullSize.class);
Bitmap b; // your bitmap
in.putExtra(KEY_TITLE, map2.get(KEY_TITLE));
in.putExtra(KEY_THUMB_URL, KEY_THUMB_URL);
startActivity(in);
}
});
2nd Activity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fullsize);
ImageView image = (ImageView) findViewById(R.id.fullsizeimg2);
TextView txtName = (TextView) findViewById(R.id.txt1);
Intent in = getIntent();
// Receiving the Data
String name = in.getStringExtra("item");
Bitmap bitmap =(Bitmap) in.getParcelableExtra("thumb_url");
// Displaying Received data
txtName.setText(name);
image.setImageBitmap(bitmap);
}
}
in this case, if i use the codes as above , the title works , i can see the text in txt but i cannot get img. i think i need to convert it to bitmap but also it didnt work for me. for converting bitmap i used this
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),"Image ID");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
////////for intent /////
intent.putExtra("imagepass", bytes.toByteArray());
/////////2nd activity//////////
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("imagepass");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView iv=(ImageView) findViewById(R.id.fullsizeimg);
iv.setImageBitmap(bmp);
but in main activity after decoderesource line, it was giving this error :
The method decodeResource(Resources, int) in the type BitmapFactory is not applicable for the arguments (Resources, String)
I will be very happy if you can help.
You are using String as the second parameter in BitmapFactory.decodeResource()
But according to your code the Bitmap creation should be like this
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.yourImageId);