gpt4 book ai didi

java - 我想在android中实现文件选择器,但是当我实现它时它给出了空指针异常

转载 作者:太空宇宙 更新时间:2023-11-04 11:50:53 26 4
gpt4 key购买 nike

如何从Android设备中选择文件并发送到服务器。这是我的主要 Activity ,运行后会出现onresume Activity 错误。因为选择文件后,它在运行时异常时给出 nullpointer

        btnUpload = (Button) findViewById(R.id.btnUploadPhoto);
btnUpload.setOnClickListener(this);
btnDownload.setOnClickListener(this);
}

private AndroidAuthSession buildSession() {
AppKeyPair appKeyPair = new AppKeyPair(Constants.DROPBOX_APP_KEY,
Constants.DROPBOX_APP_SECRET);
AndroidAuthSession session;

String[] stored = getKeys();
if (stored != null) {
AccessTokenPair accessToken = new AccessTokenPair(stored[0],
stored[1]);
session = new AndroidAuthSession(appKeyPair, Constants.ACCESS_TYPE,
accessToken);
} else {
session = new AndroidAuthSession(appKeyPair, Constants.ACCESS_TYPE);
}

return session;
}

private String[] getKeys() {
SharedPreferences prefs = getSharedPreferences(
Constants.ACCOUNT_PREFS_NAME, 0);
String key = prefs.getString(Constants.ACCESS_KEY_NAME, null);
String secret = prefs.getString(Constants.ACCESS_SECRET_NAME, null);
if (key != null && secret != null) {
String[] ret = new String[2];
ret[0] = key;
ret[1] = secret;
return ret;
} else {
return null;
}
}

@Override
public void onClick(View v) {
if (v == btnDownload) {
startActivity(new Intent(MainActivity.this, DropboxDownload.class));
} else if (v == btnUpload) {
createDir();
if (mLoggedIn) {
logOut();
}
// Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

// f = new File(Utils.getPath(),new Date().getTime()+".jpg");
// intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));

// intent.setType("file/*");
// startActivityForResult(intent,TAKE_PHOTO);
//Intent intent = new Intent(MediaStore.ACTION_GET_CONTENT);

// startActivityForResult(intent, TAKE_PHOTO);


/* Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, TAKE_PHOTO); */

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
Intent f = Intent.createChooser(intent, "File");
startActivityForResult(f, TAKE_PHOTO);



}
}

private void logOut() {
mApi.getSession().unlink();

clearKeys();
}

private void clearKeys() {
SharedPreferences prefs = getSharedPreferences(
Constants.ACCOUNT_PREFS_NAME, 0);
Editor edit = prefs.edit();
edit.clear();
edit.commit();
}

private void createDir() {
File dir = new File(Utils.getPath());
if (!dir.exists()) {
dir.mkdirs();
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == TAKE_PHOTO) {
// f = new File(Utils.getPath() + "/temp.jpg");
if (Utils.isOnline(MainActivity.this)) {
mApi.getSession().startAuthentication(MainActivity.this);
onResume = true;
} else {
Utils.showNetworkAlert(MainActivity.this);
}
}
}
}

public void setLoggedIn(boolean loggedIn) {
mLoggedIn = loggedIn;
if (loggedIn) {
UploadFile upload = new UploadFile(MainActivity.this, mApi, DIR, f);
upload.execute();
onResume = true;

}
}

private void storeKeys(String key, String secret) {
SharedPreferences prefs = getSharedPreferences(
Constants.ACCOUNT_PREFS_NAME, 0);
Editor edit = prefs.edit();
edit.putString(Constants.ACCESS_KEY_NAME, key);
edit.putString(Constants.ACCESS_SECRET_NAME, secret);
edit.commit();
}

private void showToast(String msg) {
Toast error = Toast.makeText(this, msg, Toast.LENGTH_LONG);
error.show();
}

@Override
protected void onResume() {

AndroidAuthSession session = mApi.getSession();

if (session.authenticationSuccessful()) {
try {
session.finishAuthentication();

TokenPair tokens = session.getAccessTokenPair();
storeKeys(tokens.key, tokens.secret);
setLoggedIn(onResume);
} catch (IllegalStateException e) {
showToast("Couldn't authenticate with Dropbox:"
+ e.getLocalizedMessage());
}
}
super.onResume();
}
}

这是我的崩溃报告:

01-25 13:30:20.629 9008-9008/com.example.pranit.cloudexample W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x419eacf8)
01-25 13:30:20.629 9008-9008/com.example.pranit.cloudexample W/dalvikvm: threadid=1: uncaught exception occurred
01-25 13:30:20.630 9008-9008/com.example.pranit.cloudexample W/System.err: java.lang.RuntimeException: Unable to resume activity {com.example.pranit.cloudexample/com.example.pranit.cloudexample.MainActivity}: java.lang.NullPointerException
01-25 13:30:20.631 9008-9008/com.example.pranit.cloudexample W/System.err: at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3003)
01-25 13:30:20.631 9008-9008/com.example.pranit.cloudexample W/System.err: at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3032)
01-25 13:30:20.631 9008-9008/com.example.pranit.cloudexample W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
01-25 13:30:20.632 9008-9008/com.example.pranit.cloudexample W/System.err: at android.os.Handler.dispatchMessage(Handler.java:110)
01-25 13:30:20.632 9008-9008/com.example.pranit.cloudexample W/System.err: at android.os.Looper.loop(Looper.java:193)
01-25 13:30:20.632 9008-9008/com.example.pranit.cloudexample W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5333)
01-25 13:30:20.632 9008-9008/com.example.pranit.cloudexample W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
01-25 13:30:20.632 9008-9008/com.example.pranit.cloudexample W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
01-25 13:30:20.632 9008-9008/com.example.pranit.cloudexample W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
01-25 13:30:20.632 9008-9008/com.example.pranit.cloudexample W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
01-25 13:30:20.633 9008-9008/com.example.pranit.cloudexample W/System.err: at dalvik.system.NativeStart.main(Native Method)
01-25 13:30:20.633 9008-9008/com.example.pranit.cloudexample W/System.err: Caused by: java.lang.NullPointerException
01-25 13:30:20.638 9008-9008/com.example.pranit.cloudexample W/System.err: at com.example.pranit.cloudexample.UploadFile.<init>(UploadFile.java:76)
01-25 13:30:20.638 9008-9008/com.example.pranit.cloudexample W/System.err: at com.example.pranit.cloudexample.MainActivity.setLoggedIn(MainActivity.java:162)
01-25 13:30:20.638 9008-9008/com.example.pranit.cloudexample W/System.err: at com.example.pranit.cloudexample.MainActivity.onResume(MainActivity.java:194)
01-25 13:30:20.639 9008-9008/com.example.pranit.cloudexample W/System.err: at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1197)
01-25 13:30:20.639 9008-9008/com.example.pranit.cloudexample W/System.err: at android.app.Activity.performResume(Activity.java:5422)
01-25 13:30:20.639 9008-9008/com.example.pranit.cloudexample W/System.err: at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2989)
01-25 13:30:20.640 9008-9008/com.example.pranit.cloudexample W/System.err: ... 10 more
01-25 13:30:20.640 9008-9008/com.example.pranit.cloudexample W/dalvikvm: threadid=1: calling UncaughtExceptionHandler
01-25 13:30:20.647 9008-9008/com.example.pranit.cloudexample E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.pranit.cloudexample, PID: 9008
java.lang.RuntimeException: Unable to resume activity {com.example.pranit.cloudexample/com.example.pranit.cloudexample.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3003)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3032)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.pranit.cloudexample.UploadFile.<init>(UploadFile.java:76)
at com.example.pranit.cloudexample.MainActivity.setLoggedIn(MainActivity.java:162)
at com.example.pranit.cloudexample.MainActivity.onResume(MainActivity.java:194)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1197)
at android.app.Activity.performResume(Activity.java:5422)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2989)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3032) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384) 
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:5333) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645) 
at dalvik.system.NativeStart.main(Native Method) 

最佳答案

如果您的设备/模拟器 > api 24

此崩溃是由于 android M 和 N 的新规则的权限问题而产生的。因此,如果您正在使用文件选择器,请输入代码以向用户请求有关 android.permission.WRITE_EXTERNAL_STORAGEandroid.permission.READ_EXTERNAL_STORAGE

的权限

或引用Android 6.0 Marshmallow. Cannot write to SD Card

否则分享崩溃日志。

关于java - 我想在android中实现文件选择器,但是当我实现它时它给出了空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41844979/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com