问题
I have wrote an app with 2 activities. One activity took a picture and the second one use it with some filters.
Activity 1:
Intent FilterSelectionIntent = new Intent(getActivity(), PulsFiltersActivity.class);
FilterSelectionIntent.putExtra("PicTaken", currentBitmap);
startActivity(FilterSelectionIntent);
Acitivity 2:
Bundle bd = intent.getExtras();
mBitmap = bd.getParcelable("PicTaken");
I have put some breakpoints in the Activity 2 and it never stop at. As soon as I comment the "putExtra" in comment, I can reach the breakpoints. In my case, the activity is not started, I think the intent is wrong.
I know that one solution is to use Bitmap.compress and forward the result in the Output stream. but in my case, it take too much time. My android device is a very basic one and it takes 2s to save the bmp. this why I try to use the intent to pass argument but it seems not working.
I'm also open to save the bmp as tmp file but I can lose 2 sec.
Any idea .
回答1:
Bitmap implements Parcelable, so you could always pass it in the intent:
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("BitmapImg", bitmap);
and retrieve it on the other end:
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImg");
Check it here : How can I pass a Bitmap object from one activity to another
回答2:
I think you have confused a couple of concepts of sending objects. In your Activity 1
, you put the extra directly to the intent. In Activity 2
, you try to get it back from a Bundle
, which you did not put the Bitmap
to. So try this in your activity 2:
mBitmap = (Bitmap) intent.getParcelableExtra("PicTaken");
Edit: I found some reports of Bitmaps being too big to send and activites not starting when sending Bitmaps. Converting them to ByteArray may help.
回答3:
You also can store the bitmap into internal storage like this:
final static String IMG_PATH= "bmp2Store"
FileOutputStream streamOut;
Bitmap image = currentBitmap;
try {
// Store bitmap into internal storage
streamOut = mContext.openFileOutput(IMG_PATH, Context.MODE_PRIVATE);
image.compress(Bitmap.CompressFormat.PNG, 100, streamOut);
streamOut.close();
// Call ActivityB
Intent intent= new Intent(this, ConsumirOfertaActivity.class);
startActivity(intent);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
And in the activityB:
final static String IMG_PATH= "bmp2Store"
Bitmap bitmap;
FileInputStream streamIn;
try {
streamIn = openFileInput(IMG_PATH);
bitmap = BitmapFactory.decodeStream(streamIn);
streamIn.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
回答4:
You should not pass bitmap inside bundle:
Size of the data you are allowed to pass through Bundle or Intent is very less (~1MB). Considering a object like Bitmap, there are high chances that memory allocated to it greater than 1MB. This can happen for any kind of object including byte array, String etc.
Solution: Store your bitmap into File, FileProvider, Singleton (as WeakReference of course) class, etc before starting Activity B, and pass just URI as parameter in Bundle.
Also check out this posts: Intent.putExtras size limit? and Maximum length of Intent putExtra method? (Force close)
来源:https://stackoverflow.com/questions/31672342/intent-seems-not-sent-between-activity