Reputation: 2748
I have Activity A, B and C. In Activity C, it has an image and caption. The image and caption will be return to B when ok
button
is clicked. In activity b, it will return both to activity A.
Activity C
ok.setOnClickListener(new View.OnClickListener() // ok button to return image and caption to B
{
public void onClick(View arg0)
{
Intent returnIntent=new Intent();
text=t.getText().toString();
b.setDrawingCacheEnabled(true);
b.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
b.layout(0, 0, b.getMeasuredWidth(), b.getMeasuredHeight());
b.buildDrawingCache(true);
returnIntent.putExtra("text", text);
if (b.getDrawingCache() != null) {
Bitmap bitmap = Bitmap.createBitmap(b.getDrawingCache());
if (bitmap == null) {
Log.e("TAG", "getDrawingCache() == null");
}
Global.img = bitmap;
}
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
Activity B
public void onActivityResult(int requestCode,int resultCode, Intent data)
{
if(requestCode==PROJECT_REQUEST_CODE) {
if(data!=null&&data.hasExtra("text")) {
c = data.getStringExtra("text");
txt1.setText(c); // caption can show in txt1
viewImage.setImageBitmap(Global.img); // image from C can show in viewImage
}
}
else if (requestCode==CAMERA_REQUEST_CODE)
{
}
}
b.setOnClickListener(new View.OnClickListener() { // button to return image and caption to A
public void onClick(View arg0) {
Intent returnIntent = new Intent();
returnIntent.putExtra("c",c); // return caption
returnIntent.putExtra("globalImg",Global.img);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
Activity A
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Bitmap b = Global.img
if (requestCode==1) {
caption = data.getStringExtra("c"); // caption from A
v.setImageBitmap(Global.img) // image from C
}else if {.....}
}
Global.java
public class Global {
static Bitmap img;
}
When I click button
b in Activity B, I get this
11-05 17:26:47.114 6031-6031/com.example.project.project E/JavaBinder﹕ !!! FAILED BINDER TRANSACTION !!!
Upvotes: 0
Views: 398
Reputation: 2391
try comment this line:
//returnIntent.putExtra("globalImg",Global.img);
in your Activity B
You can use Global.img for all Activity and not use intent
Added:
v.setImageBitmap(Global.img);
-> nullPointerExeption becouse v
is not initialize
Added:
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = false;
bitmapOptions.inPreferredConfig = Config.RGB_565;
bitmapOptions.inDither = true;
For "Out of memory" error.
Upvotes: 0
Reputation: 9315
Provided what you are trying to do is simply to pass along the data returned from C->B on to A, why don't you just get the string again in your onClick and put it into your intent? You could store the text in a String member, or, as the code looks now, even get it back from the TextView:
Intent returnIntent = new Intent();
returnIntent.putExtra("text", txt1.getText().toString);
setResult(Activity.RESULT_OK, returnIntent);
The image is being stored as a static in a global object, which honestly is horrible, but you could of course access that as well from your onClickListener. You should however seriously consider returning the bitmap data in some other way, the most straightforward probably being utilizing that Bitmaps implement the Parcelable interface, and Intents can hold Parcelables as extras, so in theory you can just do putExtra("img", bitmap)
and on the receiving end do intent.getParcelableExtra("img")
.
However, if the bitmap is large this might fail, and it might be better to store the bitmap to a resource such as a file, and pass the location of the file instead. You may also get a way with something like what you did with your static member in the Global class, but 1) you must be careful to remove the object after passing it back or the static reference will waste memory and 2) make sure you don't use this reference incorrectly, e.g. from multiple places simulatenously. A more robust solution is to make sure you create a unique ID for each bitmap and store it in a cache (for example a HashMap with the Bitmaps hashCode() as the key) and identify it by id.
public class BitmapStore {
private static final HashMap<Integer, Bitmap> BITMAPS= new HashMap<>();
public static Bitmap getBitmap(int id) {
return BITMAPS.remove(id);
}
public static int putBitmap(Bitmap b) {
int id = b.hashCode();
BITMAPS.put(id, b);
return id;
}
}
Using this, you could put the ID of your bitmap in your intent:
intent.putExtra("imageId", BitmapStore.putBitmap(bitmap));
setResult(RESULT_OK, intent);
and in onActivityResult
:
Bitmap b = BitmapStore.getBitmap(intent.getIntExtra("imageId", 0));
This bitmap store is not persistant but it should be pretty safe to use as an intermediate store in the transition between two activities. It's also possible to generalize to any kind of object implementing a valid hashCode/equals contract.
Upvotes: 2