gpt4 book ai didi

java - 照片压缩上传至数据库

转载 作者:行者123 更新时间:2023-12-02 02:28:14 27 4
gpt4 key购买 nike

我正在尝试拍照然后将其上传到数据库,但是每次我拍照并尝试发布它时都会收到错误:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)”。

每当我拍照时,我都不会在 ImageView 中看到预览。

所以我假设我没有将照片正确添加到位图中,以便 ImageUploadImageToServerFunction() 存储图像。然而,它不起作用对我来说毫无意义。

这是我遇到问题的函数。

Button CaptureImageFromCamera,UploadImageToServer;

ImageView ImageViewHolder;

EditText imageName;

ProgressDialog progressDialog ;

Intent intent ;

public static final int RequestPermissionCode = 1 ;

Bitmap bitmap;

boolean check = true;

String GetImageNameFromEditText;

String ImageNameFieldOnServer = "image_name" ;

String ImagePathFieldOnServer = "image_path" ;

String ImageUploadPathOnSever ="https://androidjsonblog.000webhostapp.com/capture_img_upload_to_server.php" ;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photo_activity);

CaptureImageFromCamera = (Button)findViewById(R.id.mapsPicButton);
ImageViewHolder = (ImageView)findViewById(R.id.imageView);
UploadImageToServer = (Button) findViewById(R.id.postPicBtn);
imageName = (EditText)findViewById(R.id.editText);

EnableRuntimePermissionToAccessCamera();

CaptureImageFromCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

startActivityForResult(intent, 7);

}
});

UploadImageToServer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

GetImageNameFromEditText = imageName.getText().toString();

ImageUploadToServerFunction();

}
});
}

// Start activity for result method to Set captured image on image view after click.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 7 && resultCode == RESULT_OK && data != null && data.getData() != null) {

Uri uri = data.getData();

try {

// Adding captured image in bitmap.
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);

// adding captured image in imageview.
ImageViewHolder.setImageBitmap(bitmap);

} catch (IOException e) {

e.printStackTrace();
}
}

}

// Upload captured image online on server function.
public void ImageUploadToServerFunction(){

ByteArrayOutputStream byteArrayOutputStreamObject ;

byteArrayOutputStreamObject = new ByteArrayOutputStream();

// Converting bitmap image to jpeg format, so by default image will upload in jpeg format.
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStreamObject);

byte[] byteArrayVar = byteArrayOutputStreamObject.toByteArray();

final String ConvertImage = Base64.encodeToString(byteArrayVar, Base64.DEFAULT);

class AsyncTaskUploadClass extends AsyncTask<Void,Void,String> {

@Override
protected void onPreExecute() {

super.onPreExecute();

// Showing progress dialog at image upload time.
progressDialog = ProgressDialog.show(Photo.this,"Image is Uploading","Please Wait",false,false);
}

@Override
protected void onPostExecute(String string1) {

super.onPostExecute(string1);

// Dismiss the progress dialog after done uploading.
progressDialog.dismiss();

// Printing uploading success message coming from server on android app.
Toast.makeText(Photo.this,string1,Toast.LENGTH_LONG).show();

// Setting image as transparent after done uploading.
ImageViewHolder.setImageResource(android.R.color.transparent);


}

@Override
protected String doInBackground(Void... params) {

ImageProcessClass imageProcessClass = new ImageProcessClass();

HashMap<String,String> HashMapParams = new HashMap<String,String>();

HashMapParams.put(ImageNameFieldOnServer, GetImageNameFromEditText);

HashMapParams.put(ImagePathFieldOnServer, ConvertImage);

String FinalData = imageProcessClass.ImageHttpRequest(ImageUploadPathOnSever, HashMapParams);

return FinalData;
}
}
AsyncTaskUploadClass AsyncTaskUploadClassOBJ = new AsyncTaskUploadClass();

AsyncTaskUploadClassOBJ.execute();
}

最佳答案

您可以使用此库来压缩图像

 compile 'id.zelory:compressor:2.1.0'

这是这个链接 https://android-arsenal.com/details/1/3758您可以自定义质量、高度、体重等。

compressedImageBitmap = new Compressor(this).compressToBitmap(actualImageFile);
compressedImageFile = new Compressor(this).compressToFile(actualImageFile);

如果您想要一些定制:

compressedImage = new Compressor(this)
.setMaxWidth(640)
.setMaxHeight(480)
.setQuality(75)
.setCompressFormat(Bitmap.CompressFormat.WEBP)
.setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).getAbsolutePath())
.compressToFile(actualImage);

关于java - 照片压缩上传至数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47521660/

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