In first Activity:
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bytes = stream.toByteArray();
i.putExtra("image", bytes);
startActivity(i);
In second Activity:
byte[] byteArray = extras.getByteArray("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
if (bmp != null) {
iv_1.setImageBitmap(bmp);
}
This is working for all devices and versions. But it is not working for Kitkat, why?
How to solve the issue in kitkat?
Passing such a huge file through intent is not a good practice. It would slow down the process of launching new activity.
Try to make a static reference of the image and use it in the next activity. As soon as you are done, just make it null
Make a singleton class with a Map<String, Bitmap> which will keep all images you need, and through intent send just their key names.
It is not a good for performance to pass Bitmaps from on activity to another.
Just try to save the bitmap in memory and send "Path" of bitmap to another activity and then just use BitmapFactory.decodeFile(pathName);method in another activity to get bitmap from path.
In your first activity convert imageview to bitmap
imageView.buildDrawingCache();
Bitmap bitmap = imageView.getDrawingCache();
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);
in second activity
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
Its working on kitkat also.
Related
I am new to android studio and stack overflow so I may not make any sense but I want to pass some data from one activity to another. I have managed to do it through the putExtra() on the intent with all the text that I want passed to the activity. However, I do not know how to set a getIntent on an image that has to be produced with setImageResource().
FavActivity
Cursor cursor = (Cursor) mylist.getItemAtPosition(position);
String listName = cursor.getString(1);
String displayName = db.getName(listName);
if (!displayName.isEmpty()) {
String isleName = db.getIsle(listName);
String rowName = db.getRow(listName);
String locationImage = db.getLocationImage(listName);
Intent myIntent = new Intent(view.getContext(),MainActivity.class);
myIntent.putExtra("name", displayName);
myIntent.putExtra("isle", "Isle: " + isleName);
myIntent.putExtra("row", "Row: " +rowName);
myIntent.putExtra("image", locationImage);
startActivity(myIntent);
MainActivity
textView.setText(getIntent().getStringExtra("name"));
textView3.setText(getIntent().getStringExtra("isle"));
textView4.setText(getIntent().getStringExtra("row"));
Image location has to be set like this as its converting from database text
locationView.setImageResource(getResources().getIdentifier(locationImage, "drawable", getPackageName()));
How do I set an intent to do this?
Store the image in a file and pass the file path through the intent to the other activty and you can access the image from that file path in the other activity
You can pass an image but I would suggest storing the image in Internal storage and pass them using Intent.
As of API 24, there is 1 MB restriction else it would throw TransactionTooLarge Exception and may cost you too much memory
Saving image to Internal Storage-
public String createImageFromBitmap(Bitmap bitmap) {
String fileName = "myImage";//no .png or .jpg needed
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
FileOutputStream fo = openFileOutput(fileName, Context.MODE_PRIVATE);
fo.write(bytes.toByteArray());
// remember close file output
fo.close();
} catch (Exception e) {
e.printStackTrace();
fileName = null;
}
return fileName;
}
and then pass that file name in Bundle
Bitmap bitmap = BitmapFactory.decodeStream(context
.openFileInput("myImage"));
An another example to be used
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.my_layout);
Bitmap bitmap = getIntent().getParcelableExtra("image");
ImageView imageView = (ImageView) findViewById(R.id.imageview);
imageView.setImageBitmap(bitmap);
}
Hiii,
You can send an image in Base64 format and convert it to an image after receiving in next Activity.
Though make sure that you can send maximum 50kb data using Intents.
Otherwise, your app might misbehave on sending and receiving data.
When I try to get the result from the camera I keep getting a "NullPointerException: uri" at the line:
InputStream stream = getContentResolver().openInputStream(data.getData());
from the code:
if (requestCode == CAMERA_PIC_REQUEST) {
try {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
pic.setImageBitmap(thumbnail);
InputStream stream = getContentResolver().openInputStream(data.getData());
bitmapPic = BitmapFactory.decodeStream(stream);
Being pic an ImageViewer and the image is successfully placed in the ImageViewer I can't seem to find the reason as to why this is not working.
Also here's the code for the Intent that starts the image capturer.
pic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
}
});
And the part of the code that I will use it on after this is working, or at least trying to:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmapPic.compress(Bitmap.CompressFormat.JPEG, 50, baos);
byte[] data = baos.toByteArray();
mStorageRef.child(animal.getPictureID()).putBytes(data);
Being the last line Firebase related.
Also bitmapPic is a Bitmap.
Edit 1: Tried a suggestion by adding global variable:
private FileProvider mImageUri;
then
public void onClick(View view) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(Intent.EXTRA_STREAM, mImageUri);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
}
But it says that putExtra combination doesn't exist. Not sure if that's what you meant.
Edit 2: Managed to get it working. As of now it only sends the Thumbnail but at least it's something here's how I did it:
if (requestCode == CAMERA_PIC_REQUEST) {
try {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
pic.setImageBitmap(thumbnail);
encodeBitmap(thumbnail);
}
where:
ByteArrayOutputStream baos;
public void encodeBitmap(Bitmap bitmap) {
baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
}
And finally to send it to firebase I used this:
byte[] data = baos.toByteArray();
mStorageRef.child(animal.getPictureID()).putBytes(data);
I am trying to implement a share option for png pictures in my app, but I have been getting a TransactionTooLargeException. What I did: I added code to compress my Bitmap, but I still get the exception. Is there something I am doing wrong?
public void sharePicture(MenuItem shareItem) {
MenuItemCompat.getActionProvider(shareItem);
Drawable drawable = itemImage.getDrawable();
Bitmap picture = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
picture.compress(Bitmap.CompressFormat.PNG, 100, stream);
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/png");
shareIntent.putExtra(Intent.EXTRA_STREAM, picture);
startActivity(Intent.createChooser(shareIntent, "Share image using"));
}
Quoting the documentation, EXTRA_STREAM is:
A content: URI holding a stream of data associated with the Intent, used with ACTION_SEND to supply the data being sent.
You do not put a Bitmap into EXTRA_STREAM for an image/png ACTION_SEND request. You put a Uri pointing to a PNG into EXTRA_STREAM. Ideally, you do that via FileProvider, though if your targetSdkVersion is below 24, you can get away with Uri.fromFile() for the time being.
I have a bitmap file that I created using Android Query:
aq = new AQuery(HomeCategoryActivity.this);
aq.ajax(currentUrl,Bitmap.class,0,new AjaxCallback<Bitmap>(){
#Override
public void callback(String url, Bitmap object, AjaxStatus status) {
if(object != null)
{
bmp = object;
}
}
});
bmp is a globally initialized variable and it gets properly saved by the above code and NOT NULL, I checked.
Now I want to share this bitmap on other apps using this:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(bmp));
startActivity(Intent.createChooser(intent, "Share Product via:"));
This won't work since the code is probably wrong. What changes should I make?
I want to share the image on Fb, insta, etc
Save bitmap to external storage and get path of the bitmap image
then pass the Uri.parse(path)to the intent.
for more information refer to this link http://developer.android.com/training/sharing/send.html
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parseUri(path));
startActivity(Intent.createChooser(intent, "Share Product via:"));
In my app I am trying to share a BMP with the ShareActionProvider. It is not working. Am I doing it wrong or do I need to convert it into a PNG (I do not want to deal with files). If so how can I do it? THanks.
Bitmap bmp = qrUtil.create(WIFIQRCODE, pref2);
isCodeGenerated = true;
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_STREAM, bmp);
provider.setShareIntent(intent);
The Intent.EXTRA_STREAM should be a URI pointing to the image file that you want to use; you can't just add a Bitmap there. You should do something like this:
Uri uri = Uri.fromFile(new File(getFilesDir(), "img.jpg"));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri.toString());
This will change depending on the actual location of your image, but that's the general idea.