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(); }

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

相关内容

热门资讯

揭秘几款!aapoker俱乐部... 揭秘几款!aapoker俱乐部靠谱吗,拱趴大菠萝开挂方法,解说技巧(有挂攻略)是一款可以让一直输的玩...
今日公布!werplan怎么作... 今日公布!werplan怎么作弊,pokermaster脚本,揭秘教程(有挂技巧)是一款可以让一直输...
一分钟教会你!约局吧辅助器,w... 1、一分钟教会你!约局吧辅助器,wepoker私局代打,透视教程(有挂软件)(UU poker、we...
科技介绍!wepoker透视底... 科技介绍!wepoker透视底牌脚本,hhpoker透视脚本,扑克教程(有挂技巧);支持2-10人实...
科普常识!德普之星透视辅助,w... 科普常识!德普之星透视辅助,wepoker代打辅助机器人,第三方教程(有挂软件)准备好在wepoke...
玩家必知教程!拱趴大菠萝作弊方... 玩家必知教程!拱趴大菠萝作弊方法,wepoker透视脚本下载,透明挂教程(有挂透视);《WPK辅助透...
科普攻略!德普之星透视辅助软件... 科普攻略!德普之星透视辅助软件是真的吗,hhpoker有作弊的吗,线上教程(有挂攻略);玩家必备必赢...
科普攻略!sohoo poke... 科普攻略!sohoo poker辅助器,wepokerplus辅助,必备教程(有挂软件);sohoo...
交流学习经验!德普辅助器辅助器... 交流学习经验!德普辅助器辅助器怎么用,pokermaster破解版,教你攻略(有挂软件);德普辅助器...
总算了解!fishpoker透... 总算了解!fishpoker透视,约局吧开挂神器是真的吗,可靠教程(有挂软件);原来确实真的有挂(需...