Java 位图压缩器()返回空指针异常
如果我没有弄错的话,我正在片段中使用此方法压缩图像,然后将其上载到google firebase服务器:Java 位图压缩器()返回空指针异常,java,android,firebase,firebase-realtime-database,firebase-storage,Java,Android,Firebase,Firebase Realtime Database,Firebase Storage,如果我没有弄错的话,我正在片段中使用此方法压缩图像,然后将其上载到google firebase服务器: Bitmap thumb_bitmap = new Compressor(this.getActivity()) .setMaxWidth(200) .setMaxHeight(200) .setQuality(75) .compre
Bitmap thumb_bitmap = new Compressor(this.getActivity())
.setMaxWidth(200)
.setMaxHeight(200)
.setQuality(75)
.compressToBitmap(thumb_filePath);
我最终从Android Studio得到以下错误:
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat,int,java.io.OutputStream)”
以下是该方法的其余代码:
private void uploadImage() {
if (filePath[0] != null && filePath[1] != null) {
final ProgressDialog progressDialog = new ProgressDialog(getActivity());
//progressDialog.setTitle("Uploading...");
//progressDialog.show();
for ( Uri path : filePath ) {
id_or_proof += 1;
File thumb_filePath = new File(path.getPath());
Log.d ( "THUMB FILE PATH", path.getPath() );
String current_user_id = mCurrentUser.getUid();
Bitmap thumb_bitmap = new Compressor(this.getActivity())
.setMaxWidth(200)
.setMaxHeight(200)
.setQuality(75)
.compressToBitmap(thumb_filePath);
Log.d ( "BITMAP", String.valueOf(thumb_bitmap));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
thumb_bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
final byte[] thumb_byte = baos.toByteArray();
StorageReference filepath = mImageStorage.child("profile_images").child(current_user_id + ".jpg");
final StorageReference thumb_filepath = mImageStorage.child("profile_images").child("thumbs").child(current_user_id + ".jpg");
filepath.putFile(path).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if(task.isSuccessful()){
final String download_url = task.getResult().getDownloadUrl().toString();
UploadTask uploadTask = thumb_filepath.putBytes(thumb_byte);
uploadTask.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> thumb_task) {
String thumb_downloadUrl = thumb_task.getResult().getDownloadUrl().toString();
if(thumb_task.isSuccessful()){
if ( id_or_proof == 1 ) {
Map update_hashMap = new HashMap();
update_hashMap.put("id_image", download_url);
update_hashMap.put("thumb_id_image", thumb_downloadUrl);
mUserDatabase.updateChildren(update_hashMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
mProgressDialog.dismiss();
Toast.makeText(getActivity(), "Success Uploading.", Toast.LENGTH_LONG).show();
}
}
});
}
if ( id_or_proof == 2 ) {
Map update_hashMap = new HashMap();
update_hashMap.put("proof_image", download_url);
update_hashMap.put("thumb_proof_image", thumb_downloadUrl);
mUserDatabase.updateChildren(update_hashMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
mProgressDialog.dismiss();
Toast.makeText(getActivity(), "Success Uploading.", Toast.LENGTH_LONG).show();
}
}
});
}
} else {
Toast.makeText(getActivity(), "Error in uploading thumbnail.", Toast.LENGTH_LONG).show();
mProgressDialog.dismiss();
}
}
});
} else {
Toast.makeText(getActivity(), "Error in uploading.", Toast.LENGTH_LONG).show();
mProgressDialog.dismiss();
}
}
});
}// end of for
}// end of if ()
} // end of uploadImage()
private void uploadImage(){
if(文件路径[0]!=null&&filePath[1]!=null){
final ProgressDialog ProgressDialog=新建ProgressDialog(getActivity());
//progressDialog.setTitle(“上载…”);
//progressDialog.show();
for(Uri路径:filePath){
id_或_-proof+=1;
File thumb_filePath=新文件(path.getPath());
Log.d(“THUMB文件路径”,PATH.getPath());
字符串current_user_id=mCurrentUser.getUid();
位图thumb\u Bitmap=新压缩器(this.getActivity())
.setMaxWidth(200)
.setMaxHeight(200)
.setQuality(75)
.compressToBitmap(thumb_文件路径);
Log.d(“位图”,String.valueOf(thumb_位图));
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
thumb_bitmap.compress(bitmap.CompressFormat.JPEG,100,baos);
最后一个字节[]thumb_byte=bas.toByteArray();
StorageReference filepath=mImageStorage.child(“profile_images”).child(当前_用户_id+“.jpg”);
final StorageReference thumb_filepath=mImageStorage.child(“profile_images”).child(“thumbs”).child(当前_用户_id+“.jpg”);
filepath.putFile(path).addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
最后一个字符串download_url=task.getResult().getDownloadUrl().toString();
UploadTask UploadTask=thumb\u filepath.putBytes(thumb\u字节);
uploadTask.addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务thumb\u任务){
字符串thumb_downloadUrl=thumb_task.getResult().getDownloadUrl().toString();
if(thumb_task.issusccessful()){
if(id\u或\u-proof==1){
Map update_hashMap=new hashMap();
更新\u hashMap.put(“id\u image”,下载\u url);
更新\u hashMap.put(“thumb\u id\u image”,thumb\u downloadUrl);
mUserDatabase.updateChildren(update_hashMap).addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
mProgressDialog.disclose();
Toast.makeText(getActivity(),“成功上传”,Toast.LENGTH_LONG.show();
}
}
});
}
if(id\u或\u-proof==2){
Map update_hashMap=new hashMap();
更新hashMap.put(“校对图像”,下载url);
更新hashMap.put(“thumb\u-proof\u-image”,thumb\u-downloadUrl);
mUserDatabase.updateChildren(update_hashMap).addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
mProgressDialog.disclose();
Toast.makeText(getActivity(),“成功上传”,Toast.LENGTH_LONG.show();
}
}
});
}
}否则{
Toast.makeText(getActivity(),“上载缩略图时出错”,Toast.LENGTH_LONG.show();
mProgressDialog.disclose();
}
}
});
}否则{
Toast.makeText(getActivity(),“上传时出错”,Toast.LENGTH_LONG.show();
mProgressDialog.disclose();
}
}
});
}//结束
}//if()结束
}//上传图像()结束
我需要帮助,试图找出我为什么会出现错误,以及如何修复它
非常感谢
下面是日志:
03-14 15:36:57.383 2780-2780/in.tvac.akshaye.lapitchat E/BitmapFactory:无法解码流:java.io.FileNotFoundException:/document/60(无此类文件或目录)
03-14 15:36:57.383 2780-2780/in.tvac.akshay
Bitmap thumb_bitmap = new Compressor(this.getActivity())
.setMaxWidth(200)
.setMaxHeight(200)
.setQuality(75)
.compressToBitmap(thumb_filePath);
Bitmap thumb_bitmap = new Compressor(getActivity())
.setMaxWidth(200)
.setMaxHeight(200)
.setQuality(75)
.compressToBitmap(thumb_filePath);
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
String filePath = Environment.getExternalStorageDirectory() + "/document/60";
Uri path = Uri.parse(filePath);
String filePath = getActivity().getFilesDir() + "/document/60";
Uri path = Uri.parse(filePath);