代码用于从外部存储中选择文件以执行OTA 从Android App更新。这在Kitkat中运行良好,但涉及到 选择Path App时,Nougat手机(Moto G4)正在崩溃。
我尝试过调试,问题在于此行file = new File(item.getFilePath());
这会在Nougat版本中创建NullPointerException
,我也尝试在Manifest中为读取外部存储提供权限,但仍然存在问题仍然存在。可能是问题?我已经发布了以下代码。请帮助
public class ChooseImageFileDialog extends Activity {
private FileListAdapter mAdaptor;
private ArrayList<FileFolderItem> mFileFolderList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mChooseButton = (Button) findViewById(R.id.select_button);
mChooseButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mSelectedFile != null) {
getIntent().putExtra(RETURN_PATH_RESULT,
mSelectedFile.getPath());
setResult(RESULT_OK, getIntent());
finish();
}
}
});
mFileFolderList = new ArrayList<FileFolderItem>();
mAdaptor = new FileListAdapter(this, mFileFolderList);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listview, View view, int
position, long id) {
FileFolderItem item = mFileFolderList.get(position);
if (item == null)
return;
File file = new File(item.getFilePath());
highlightSelectedItem(listview, view);
}
});
修改:2017年11月27日
mChooseButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mSelectedFile != null) {
if (Build.VERSION.SDK_INT >= 23) {
if (checkPermission()) {
Log.e("value", "Permission already Granted, Now you
can save image.");
}
else {
requestPermission();
}
}else {
Log.e("value", "Not required for requesting runtime
permission");
getIntent().putExtra(RETURN_PATH_RESULT,
mSelectedFile.getPath());
setResult(RESULT_OK, getIntent());
finish();
}
}
}
});
private boolean checkPermission() {
int result =
ActivityCompat.checkSelfPermission(ChooseImageFileDialog.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (result == PackageManager.PERMISSION_GRANTED) {
return true;
}else{
return false;
}
}
private void requestPermission()
{
if(ActivityCompat.
shouldShowRequestPermissionRationale(ChooseImageFileDialog.th
is,Manifest.permission.WRITE_EXTERNAL_STORAGE))
{
Toast.makeText(ChooseImageFileDialog.this, "Write External Storage
permission allows us to do store images. Please allow this
permission in App Setting", Toast.LENGTH_LONG).show();
}
else
{
ActivityCompat.requestPermissions(ChooseImageFileDialog.this, new
String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_REQUEST_CODE);
}
}
我已修改代码以获得运行时权限但仍然如此 应用程序崩溃。仍然有同样的问题
答案 0 :(得分:0)
您必须在运行时为Android 5.1以上的设备请求权限。
所以你需要要求WRITE_EXTERNAL_STORAGE
许可:)
阅读以下文档:
https://developer.android.com/training/permissions/requesting.html