这是我的代码:
protected S3TaskResult doInBackground(Uri... uris) {
if (uris == null || uris.length != 1) {
return null;
}
// The file location of the image selected.
Uri selectedImage = uris[0];
ContentResolver resolver = activity.getContentResolver();
String fileSizeColumn[] = {OpenableColumns.SIZE};
Cursor cursor = resolver.query(selectedImage,
fileSizeColumn, null, null, null);
cursor.moveToFirst();
int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
// If the size is unknown, the value stored is null. But since an int can't be
// null in java, the behavior is implementation-specific, which is just a fancy
// term for "unpredictable". So as a rule, check if it's null before assigning
// to an int. This will happen often: The storage API allows for remote
// files, whose size might not be locally known.
String size = null;
if (!cursor.isNull(sizeIndex)) {
// Technically the column stores an int, but cursor.getString will do the
// conversion automatically.
size = cursor.getString(sizeIndex);
}
cursor.close();
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(resolver.getType(selectedImage));
if(size != null){
metadata.setContentLength(Long.parseLong(size));
}
S3TaskResult result = new S3TaskResult();
// Put the image data into S3.
try {
s3ClientPasado.createBucket(Constants.getPictureBucket());
PutObjectRequest por = new PutObjectRequest(
Constants.getPictureBucket(), Constants.PICTURE_NAME,
resolver.openInputStream(selectedImage),metadata);
s3ClientPasado.putObject(por);
} catch (Exception exception) {
result.setErrorMessage(exception.getMessage());
}
return result;
}
说执行doInBackground()时出错
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
如有任何帮助,我们将不胜感激。
最佳答案
当我传递的 Uri 变为空值时,我遇到了类似的问题。
问题发生在 resolver.query
方法中。
您应该发布其余代码,以便我们更好地理解它。
例如 - 如果您在从相机获取图像后使用此代码,请发布您如何调用相机 Intent 以及如何获取 Uri。
祝你好运。
关于android - 为什么我在 doInBackground 上出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22882848/