我正在按照视频指南/教程从Treehouse制作Android自毁消息应用程序。在我从github下载了一个FileHelper类之前,事情进展顺利(由视频指南中的人员编写)。
现在当我运行应用程序并尝试通过设备的图库选择照片/视频后向后端发送消息时,它工作正常,但是当我从设备相机中选择照片/视频时崩溃,我在logcat中得到一个nullPointerException。
04-16 19:35:19.007: E/AndroidRuntime(6299): FATAL EXCEPTION: main
04-16 19:35:19.007: E/AndroidRuntime(6299): java.lang.NullPointerException
04-16 19:35:19.007: E/AndroidRuntime(6299): at
com.pbg.swapzy.FileHelper.getByteArrayFromFile(FileHelper.java:27)
04-16 19:35:19.007: E/AndroidRuntime(6299): at
com.pbg.swapzy.RecipientsActivity.createMessage(RecipientsActivity.java:159)
04-16 19:35:19.007: E/AndroidRuntime(6299): at
com.pbg.swapzy.RecipientsActivity.onOptionsItemSelected(RecipientsActivity.java:122)
04-16 19:35:19.007: E/AndroidRuntime(6299): at
android.app.Activity.onMenuItemSelected(Activity.java:2606)
04-16 19:35:19.007: E/AndroidRuntime(6299): at
com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1045)
04-16 19:35:19.007: E/AndroidRuntime(6299): at
com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected
(MenuBuilder.java:735)
04-16 19:35:19.007: E/AndroidRuntime(6299): at
com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
04-16 19:35:19.007: E/AndroidRuntime(6299): at
com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
04-16 19:35:19.007: E/AndroidRuntime(6299): at
com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:592)
04-16 19:35:19.007: E/AndroidRuntime(6299): at
com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:149)
04-16 19:35:19.007: E/AndroidRuntime(6299): at
android.view.View.performClick(View.java:4222)
04-16 19:35:19.007: E/AndroidRuntime(6299): at
android.view.View$PerformClick.run(View.java:17273)
04-16 19:35:19.007: E/AndroidRuntime(6299): at
android.os.Handler.handleCallback(Handler.java:615)
04-16 19:35:19.007: E/AndroidRuntime(6299): at
android.os.Handler.dispatchMessage(Handler.java:92)
04-16 19:35:19.007: E/AndroidRuntime(6299): at
android.os.Looper.loop(Looper.java:137)
04-16 19:35:19.007: E/AndroidRuntime(6299): at
android.app.ActivityThread.main(ActivityThread.java:4895)
04-16 19:35:19.007: E/AndroidRuntime(6299): at
java.lang.reflect.Method.invokeNative(Native Method)
04-16 19:35:19.007: E/AndroidRuntime(6299): at
java.lang.reflect.Method.invoke(Method.java:511)
04-16 19:35:19.007: E/AndroidRuntime(6299): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
04-16 19:35:19.007: E/AndroidRuntime(6299): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
04-16 19:35:19.007: E/AndroidRuntime(6299): at
dalvik.system.NativeStart.main(Native Method)
以下是有问题的代码
public static byte[] getByteArrayFromFile(Context context, Uri uri) {
byte[] fileBytes = null;
InputStream inStream = null;
ByteArrayOutputStream outStream = null;
if (uri.getScheme().equals("content")) {
try {
inStream = context.getContentResolver().openInputStream(uri);
outStream = new ByteArrayOutputStream();
byte[] bytesFromFile = new byte[1024*1024]; // buffer size (1 MB)
int bytesRead = inStream.read(bytesFromFile);
while (bytesRead != -1) {
outStream.write(bytesFromFile, 0, bytesRead);
bytesRead = inStream.read(bytesFromFile);
}
fileBytes = outStream.toByteArray();
}
catch (IOException e) {
Log.e(TAG, e.getMessage());
}
finally {
try {
inStream.close();
outStream.close();
}
catch (IOException e) { /*( Intentionally blank */ }
}
}
else {
try {
File file = new File(uri.getPath());
FileInputStream fileInput = new FileInputStream(file);
fileBytes = IOUtils.toByteArray(fileInput);
}
catch (IOException e) {
Log.e(TAG, e.getMessage());
}
}
return fileBytes;
}
第27行
if (uri.getScheme().equals("content")) {
感谢您的帮助!
编辑getByteArrayFromFile用于此代码
protected ParseObject createMessage() {
ParseObject message = new ParseObject(ParseConstants.CLASS_MESSAGES);
message.put(ParseConstants.KEY_SENDER_ID, ParseUser.getCurrentUser().getObjectId());
message.put(ParseConstants.KEY_SENDER_NAME, ParseUser.getCurrentUser().getUsername());
message.put(ParseConstants.KEY_RECIPIENT_IDS, getRecipientIds());
message.put(ParseConstants.KEY_FILE_TYPE, mFileType);
byte[] fileBytes = FileHelper.getByteArrayFromFile(this, mMediaUri);
if (fileBytes == null){
return null;
}
else {
if (mFileType.equals(ParseConstants.TYPE_IMAGE)) {
fileBytes = FileHelper.reduceImageForUpload(fileBytes);
}
String fileName = FileHelper.getFileName(this, mMediaUri, mFileType);
ParseFile file = new ParseFile(fileName, fileBytes);
message.put(ParseConstants.KEY_FILE, file);
return message;
}
}
编辑我已经在模拟器上测试代码并且它可以正常工作,但是,它在真实设备上崩溃了。这是我的测试设备的问题吗?相机软件?该应用程序有一个minsdkversion 14,tagetsdk是19,我测试的设备是16(4.1.2)。
我完全失去了!
编辑我运行调试器并在byte[] fileBytes = FileHelper.getByteArrayFromFile(this, mMediaUri);
一切看起来都不错,所以我让它一直运行到最后,现在一切都在设备和模拟器上完美运行。
真的很奇怪。