Android应用程序在添加图像时自动关闭
创始人
2024-10-13 03:00:28
0

这可能是内存不足或持久存储问题导致的。考虑在将图像添加到应用程序中时使用更少的内存。另外,确保在添加图像时正确处理权限。

以下是一个开发者可以使用的代码示例,以确保在添加图像时,内存和权限方面的问题得到正确处理:

// Adding image to ImageView ImageView imageView = (ImageView)findViewById(R.id.imageView); Bitmap bitmap = BitmapFactory.decodeFile(imageFilePath); imageView.setImageBitmap(bitmap);

// Handling memory and permission try { // Get external storage directory File imageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); if (!imageDirectory.exists()) { imageDirectory.mkdirs(); } // Create new file in image directory File imageFile = new File(imageDirectory, "image.jpg");

// Get file output stream
FileOutputStream fileOutputStream = new FileOutputStream(imageFile);

// Compress image to JPEG format and save to file output stream
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);

// Close file output stream
fileOutputStream.close();

// Make sure file is readable
imageFile.setReadable(true, false);

// Broadcast media scanner
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(Uri.fromFile(imageFile));
sendBroadcast(mediaScanIntent);

} catch (Exception e) { e.printStackTrace(); }

注意,这只是一个示例代码,实际使用中可以根据需要进行修改。

相关内容

热门资讯

据目击者称!wepoker辅助... 据目击者称!wepoker辅助器怎么弄,贵阳辅助工具,技法教程(确实有挂)贵阳辅助工具辅助器是一种具...
黑科技辅助!wepoker作弊... 黑科技辅助!wepoker作弊辅助,雀神麻将小程序辅助软件,讲义教程(竟然有挂)雀神麻将小程序辅助软...
总结辅助挂!wepoker破解... 总结辅助挂!wepoker破解器激活码,微信小程序修改器,积累教程(有挂助手)1、起透看视 微信小程...
不少玩家反映!wepoker有... 不少玩家反映!wepoker有脚本吗,0759湛江吴川三脚鸡辅助,秘籍教程(有挂教学)1、玩家可以在...
目前来看!aapoker插件,... 目前来看!aapoker插件,五十k辅助,手段教程(有挂方法)1、打开软件启动之后找到中间准星的标志...
随着!sohoopoker辅助... 随着!sohoopoker辅助,德扑之星插件,办法教程(有挂存在)1、完成德扑之星插件辅助器v3.3...
教学辅助挂!拱趴大菠萝怎么开挂... 教学辅助挂!拱趴大菠萝怎么开挂,蜀渝牌乐汇辅助器,资料教程(有挂猫腻)蜀渝牌乐汇辅助器脚本下载中分为...
2026版辅助挂!约局吧德州真... 2026版辅助挂!约局吧德州真的存在透视吗,潮友会app下载安卓辅助软件,指南教程(确实有挂)1、玩...
方法辅助挂!we-poker正... 方法辅助挂!we-poker正规吗,欢乐达人葫芦鱼辅助器,指引教程(有挂分析)一、欢乐达人葫芦鱼辅助...
第三方辅助!约局吧开挂,微乐小... 第三方辅助!约局吧开挂,微乐小程序挂哪里有,机巧教程(有挂细节)1、上手简单,内置详细流程视频教学,...