Android java 活页夹失败的活页夹交易?
Android java binder FAILED BINDER TRANSACTION?
我正在尝试从服务下载图像并将其显示在 activity 中,但我一直收到
java binder FAILED BINDER TRANSACTION
这是我的服务代码
public class DownloadImageService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new LoadImageAsync().execute(intent.getStringExtra("type"));
return Service.START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private class LoadImageAsync extends AsyncTask<String, Void, String> {
byte[] compressedImage;
Bitmap bmp;
String img;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
try {
URL url = new URL(imgUrl);
bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
compressedImage = CompressBitmap.compresssImage(bmp);
img = Base64.encodeToString(compressedImage, Base64.DEFAULT);
} catch (IOException e) {
compressedImage = null;
bmp = null;
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (compressedImage != null) {
Intent i = new Intent(getApplicationContext(), OtherCampaignActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("image_byte", img);
startActivity(i);
}
stopService(new Intent(getApplicationContext(), DownloadImageService.class));
}
}
}
压缩功能
public static byte[] compresssImage(Bitmap b) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] compressedByteArray = stream.toByteArray();
return compressedByteArray;
}
我的ACTIVITY
public class OtherActivity extends AppCompatActivity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
iv = (ImageView) findViewById(R.id.imageViewCam);
byte[] byteArray = Base64.decode(getIntent().getStringExtra("image_byte"), Base64.DEFAULT);
// byte[] byteArray = getIntent().getExtras().getByteArray("image_byte");
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
iv.setImageBitmap(bitmap);
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
}
代码中有什么问题。 activity 未启动。
应用没有崩溃。我只在 logcat 中得到这个:
06-30 12:38:36.800 29992-29992/com.vt.enit E/JavaBinder﹕ !!! FAILED BINDER TRANSACTION !!!
我猜可能是因为位图太大了。
创建图片缓存解决了我的问题
private LruCache<String, Bitmap> mMemoryCache;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Get max available VM memory, exceeding this amount will throw an
// OutOfMemory exception. Stored in kilobytes as LruCache takes an
// int in its constructor.
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = maxMemory / 8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
// The cache size will be measured in kilobytes rather than
// number of items.
return bitmap.getByteCount() / 1024;
}
};
...
}
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}
public Bitmap getBitmapFromMemCache(String key) {
return mMemoryCache.get(key);
}
它失败了,因为您正尝试将图像作为 intent extra 发送,但它太大了。您不能使用 Intent 或 service/binder 等 IPC 通信技术发送图像,有 1mb/10mb 的限制,具体取决于 android.
的版本
我正在尝试从服务下载图像并将其显示在 activity 中,但我一直收到
java binder FAILED BINDER TRANSACTION
这是我的服务代码
public class DownloadImageService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new LoadImageAsync().execute(intent.getStringExtra("type"));
return Service.START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private class LoadImageAsync extends AsyncTask<String, Void, String> {
byte[] compressedImage;
Bitmap bmp;
String img;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
try {
URL url = new URL(imgUrl);
bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
compressedImage = CompressBitmap.compresssImage(bmp);
img = Base64.encodeToString(compressedImage, Base64.DEFAULT);
} catch (IOException e) {
compressedImage = null;
bmp = null;
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (compressedImage != null) {
Intent i = new Intent(getApplicationContext(), OtherCampaignActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("image_byte", img);
startActivity(i);
}
stopService(new Intent(getApplicationContext(), DownloadImageService.class));
}
}
}
压缩功能
public static byte[] compresssImage(Bitmap b) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] compressedByteArray = stream.toByteArray();
return compressedByteArray;
}
我的ACTIVITY
public class OtherActivity extends AppCompatActivity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
iv = (ImageView) findViewById(R.id.imageViewCam);
byte[] byteArray = Base64.decode(getIntent().getStringExtra("image_byte"), Base64.DEFAULT);
// byte[] byteArray = getIntent().getExtras().getByteArray("image_byte");
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
iv.setImageBitmap(bitmap);
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
}
代码中有什么问题。 activity 未启动。
应用没有崩溃。我只在 logcat 中得到这个:
06-30 12:38:36.800 29992-29992/com.vt.enit E/JavaBinder﹕ !!! FAILED BINDER TRANSACTION !!!
我猜可能是因为位图太大了。
创建图片缓存解决了我的问题
private LruCache<String, Bitmap> mMemoryCache;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Get max available VM memory, exceeding this amount will throw an
// OutOfMemory exception. Stored in kilobytes as LruCache takes an
// int in its constructor.
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = maxMemory / 8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
// The cache size will be measured in kilobytes rather than
// number of items.
return bitmap.getByteCount() / 1024;
}
};
...
}
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}
public Bitmap getBitmapFromMemCache(String key) {
return mMemoryCache.get(key);
}
它失败了,因为您正尝试将图像作为 intent extra 发送,但它太大了。您不能使用 Intent 或 service/binder 等 IPC 通信技术发送图像,有 1mb/10mb 的限制,具体取决于 android.
的版本