小编Muh*_*nna的帖子

如何将位图保存到android中的应用程序文件夹

我有一个位图,我想将其保存到应用程序文件夹中。我尝试使用这些代码:

 ContextWrapper contextWrapper = new ContextWrapper(context.getApplicationContext());
 File directory = contextWrapper.getDir("tabs", Context.MODE_PRIVATE);
 if (!directory.exists())
     directory.mkdir();
     String fname = "Image.jpg";
     File file = new File(directory, fname);
     FileOutputStream fos = null;
     try {
         fos = new FileOutputStream(file);
         bitmap.compress(Bitmap.CompressFormat.JPEG, 50, fos);
         fos.close();
     } catch (Exception e) {
            Log.e("SAVE_IMAGE", e.getMessage(), e);
     }
Run Code Online (Sandbox Code Playgroud)

现在,我有两个问题。

  1. 为什么会显示此警告以及如何修复它?

“File.mkdir()”的结果被忽略

  1. 在应用程序文件夹中创建了目录“app_tabs”,但未保存位图,并且该文件夹中没有照片。如何保存该位图?

截屏

java android android-file

4
推荐指数
1
解决办法
8414
查看次数

为什么在我使用了 LiveData 后还需要启动 notifyDataSetChanged()?

我正在通过https://github.com/googlecodelabs/android-room-with-a-view/tree/kotlin的示例项目 RoomWordsSample 学习 Room

以下代码来自项目。

在我看来,如果观察到数据发生变化,LiveDate 将自动更新 UI。

但是在文件 WordListAdapter.kt 中,我发现notifyDataSetChanged()添加到function setWords(words: List<Word>)? 似乎它必须在数据更改时手动通知 UI。

为什么还需要启动 notifyDataSetChanged()当我使用 LiveData 时,?

主活动.kt

class MainActivity : AppCompatActivity() {

    private val newWordActivityRequestCode = 1
    private lateinit var wordViewModel: WordViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val recyclerView = findViewById<RecyclerView>(R.id.recyclerview)
        val adapter = WordListAdapter(this)
        recyclerView.adapter = adapter
        recyclerView.layoutManager = LinearLayoutManager(this)

        wordViewModel = ViewModelProvider(this).get(WordViewModel::class.java)


        wordViewModel.allWords.observe(this, Observer { words ->         
            words?.let { adapter.setWords(it) }
        })

    }
}
Run Code Online (Sandbox Code Playgroud)

WordViewModel.kt …

android kotlin android-livedata android-jetpack

1
推荐指数
1
解决办法
584
查看次数