【发布时间】:2020-04-01 04:54:38
【问题描述】:
我正在使用 Android Studio 3.6 的模拟器开发 android 应用程序。我正在使用带有 andoird 10 的 Nexus 7 虚拟设备。
我已授予清单文件的权限,并已要求用户授予对外部存储的读写权限。
虽然权限没问题,但我无法在外部存储上创建文件夹。我尝试编写一个后台进程。
这是 og 清单文件的摘录
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
这是我的主要活动的摘录,我要求用户授予权限并调用后台进程:UnitDataUpdater
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
public static final int MY_PERMISSIONS_REQUEST_EXTERNAL_STORAGE = 1;
// public static String SERVER_API_URL = "http://local.xxxx.xxxxx.eu/index.php?option=com_alumincocc&view=examlist&Itemid=122&format=json&unitCode=";
// public static LinearLayout examList;
public JSONArray uiData;
ListView examsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkPermissions();
setContentView(R.layout.activity_main);
String url = "http://xxxxx.xxxxx.eu/index.php?option=com_alumincocc&view=examlist&Itemid=122&format=json&unitCode=478050";
String result = "";
examsList = (ListView) findViewById(R.id.examsList);
UnitDataUpdater process = new UnitDataUpdater(this);
try {
result = process.execute(url).get();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
........
public void checkPermissions() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED
||
ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_EXTERNAL_STORAGE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {
super
.onRequestPermissionsResult(requestCode,
permissions,
grantResults);
if (requestCode == MY_PERMISSIONS_REQUEST_EXTERNAL_STORAGE) {
// Checking whether user granted the permission or not.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Showing the toast message
Toast.makeText(MainActivity.this,
"Write Permission Granted",
Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(MainActivity.this,
"Write Permission Denied",
Toast.LENGTH_SHORT)
.show();
}
}
}
}
这是我尝试写入外部存储的后台进程。 getAppDi 方法是我尝试创建新文件夹的地方。永远不会创建此文件夹,因此该类的其余部分会引发错误。
奇怪的是,在模拟器中我看不到 Document 文件夹。我在“下载”文件夹上尝试过同样的事情,但还是一样。
public class UnitDataUpdater extends AsyncTask<String, Void, String> {
// private static String REMOTE_URL = "http://local.xxxxxx.xxxxx.eu/index.php?option=com_alumincocc&view=examlist&Itemid=122&format=json&unitCode=";
private String data = "";
public static final String REQUEST_METHOD = "GET";
public static final int READ_TIMEOUT = 15000;
public static final int CONNECTION_TIMEOUT = 15000;
private String filePrefix = "i";
private MainActivity mainActivity;
public UnitDataUpdater(Activity mainActivity) {
this.mainActivity = (MainActivity) mainActivity;
}
........
private String getAppDir() {
String externalStoragePublicDirectory = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
.getAbsolutePath();
String appDirPath = externalStoragePublicDirectory + "/XXXXUnitData";
File appDir = new File(appDirPath);
if (!appDir.exists()) {
if (!appDir.mkdir()) {
try {
throw new Exception("Storage Error");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
return appDir.getAbsolutePath();
}
}
感谢任何帮助
【问题讨论】:
-
您是否看到任何错误消息指出权限不可用?另外我认为我们授予的权限仅适用于主进程,不适用于在同一应用程序下运行的子进程。例如;在单独的进程下运行,例如 (:xyz)
-
您使用的是 Android Q?
-
@blackapps 是的,我在 Android Studio 虚拟设备上的 android Q 上
-
@TomTaylor 不,我没有看到任何错误,至少在 logcat 中没有。我不知道还能去哪里寻找错误。
-
@Christoforos : 我们可以像检查权限是否存在或者要求用户在运行时授予权限然后继续吗?
标签: android android-emulator nexus-7