我正在使用相机捕获图像并将其保存在图库中,然后我想将此图像发送到一个本地主机,尝试将其转换为base64字符串。谁能帮我这个?这是我的代码。
package com.accenture.kshitizgoyal.data_image;
import android.app.Activity;
import android.app.DownloadManager;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
ImageView imageViewLoad;
Button capturePhoto;
Button submit;
EditText PONumber, CustomerId;
String CustID="";
String PONum="";
Bitmap photo;
private static final int CAPTURE_PHOTO = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomerId = (EditText) findViewById(R.id.editText1);
PONumber = (EditText) findViewById(R.id.editText2);
submit = (Button) findViewById(R.id.button);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
new Thread(new Runnable() {
public void run() {
try {
URL url = new URL("http://192.168.0.110:8080/SanjeevSir/Final_API");
URLConnection connection = url.openConnection();
String inputString1 = CustomerId.getText().toString();
String inputString2 = PONumber.getText().toString();
String inputString3= convertBitmapToString(photo);
//inputString = URLEncoder.encode(inputString, "UTF-8");
Log.d("inputString", inputString1);
Log.d("inputString", inputString2);
Log.d("inputString",inputString3);
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(inputString1+","+inputString2);
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String returnString1="";
String returnString2="";
String returnString3="";
while (((returnString1 = in.readLine()) != null) && ((returnString2 = in.readLine()) != null))
{
CustID = returnString1;
PONum = returnString2;
}
in.close();
runOnUiThread(new Runnable() {
public void run() {
CustomerId.setText(CustID.toString());
PONumber.setText(PONum.toString());
}
});
} catch (Exception e) {
Log.d("Exception", e.toString());
}
}
}).start();
break;
}
}
});
imageViewLoad = (ImageView) findViewById(R.id.imageView1);
capturePhoto = (Button) findViewById(R.id.takephoto);
//Clicking on TakePhoto Button
capturePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAPTURE_PHOTO);
}
});
}
// The OnActivityResult helps to handle the functionality of the feature( After Capturing Photo/Video)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == CAPTURE_PHOTO && resultCode == Activity.RESULT_OK) {
String appDirectoryName = "/FirstApp/";
//Create folder in Gallery to store your Captured Images/Videos
final File imageRoot = new File(Environment.getExternalStorageDirectory(), appDirectoryName);
if (!imageRoot.exists()) {
imageRoot.mkdir();
}
String dirName = Environment.getExternalStorageDirectory().getPath() + appDirectoryName;
Toast.makeText(this, dirName, Toast.LENGTH_LONG).show();
Bitmap photo = (Bitmap) intent.getExtras().get("data");
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
File file = new File(dirName, "IMG" + timestamp.toString() + ".PNG");
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(file);
photo.compress(Bitmap.CompressFormat.PNG, 100, fOut);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ContentValues values = new ContentValues();
values.put("_data", file.getAbsolutePath());
ContentResolver cr = getContentResolver();
cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
// displays the captured Photo on the app screen
imageViewLoad.setImageBitmap(photo);
}
}
public String convertBitmapToString(Bitmap photo){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG,0,stream); //compress to which format you want.
byte[] byte_arr = stream.toByteArray();
String imageStr = Base64.encodeToString(byte_arr,Base64.DEFAULT);
return imageStr;
}
}
我已经将位图转换为Base64字符串,并尝试使用Log语句在控制台中将其打印出来,但是什么也没看到,而是得到了空指针异常。我无法弄清楚该怎么做。预先感谢。