我想要的是, 从相机或画廊拍照并将其转换为base64并将其发送到服务器。 为此,我做了以下代码:
case R.id.btn_take_photo:
boolean iNetStatus = Utility.isNetworkAvaliable(mContext);
if (iNetStatus == true) {
openImageIntent();
} else {
Utility.internetErrorDialog(mContext);
}
break;
private void openImageIntent() {
final File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "MyDir" + File.separator);
root.mkdirs();
final String fname = "img_" + System.currentTimeMillis() + ".jpg";
final File sdImageMainDirectory = new File(root, fname);
outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(
captureIntent, 0);
for (ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName,
res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
final Intent chooserIntent = Intent.createChooser(i, "Select Source");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
cameraIntents.toArray(new Parcelable[] {}));
startActivityForResult(chooserIntent, REQ_IMAGE_CHANGE);
}
public String encodeTobase64(Bitmap image) {
Bitmap immagex = image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
immagex.compress(Bitmap.CompressFormat.JPEG, 60, baos);
immagex.setDensity(100);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
return imageEncoded;
}
switch (requestCode) {
case REQ_IMAGE_CHANGE:
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
final boolean isCamera;
if (data == null) {
isCamera = true;
} else {
isCamera = MediaStore.ACTION_IMAGE_CAPTURE.equals(data
.getAction());
}
Uri selectedImageUri;
if (isCamera) {
selectedImageUri = outputFileUri;
} else {
selectedImageUri = data == null ? null : data.getData();
}
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(
this.getContentResolver(), selectedImageUri);
iUserPhoto.setImageBitmap(Bitmap.createScaledBitmap(
bitmap, 100, 100, false));
base64Image = encodeTobase64(Bitmap.createScaledBitmap(bitmap, 400, 400, false));
boolean iNetStatus = haveInternet(DeviceSearchActivity.this);
if (iNetStatus == true) {
new changePhotoToServer().execute();
} else {
Log.e("INTERNET", "NO INTERNET");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
default:
Log.e(TAG, "Unknown request code");
break;
}
}
当我执行此代码时,我正在选择相机和图库,点击图库我能够选择图片并点击相机我能够捕捉图片,但在这两种情况下我点击确定我的应用程序崩溃了。它给出了以下错误:
03-10 13:01:56.904: E/AndroidRuntime(9506): FATAL EXCEPTION: main
03-10 13:01:56.904: E/AndroidRuntime(9506): java.lang.RuntimeException: Unable to resume activity {com.ampl.bna/com.ampl.bna.RecipeDetailsFragmentActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.ampl.bna/com.ampl.bna.RecipeDetailsFragmentActivity}: java.lang.NullPointerException
03-10 13:01:56.904: E/AndroidRuntime(9506): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3200)
03-10 13:01:56.904: E/AndroidRuntime(9506): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3238)
03-10 13:01:56.904: E/AndroidRuntime(9506): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2525)
03-10 13:01:56.904: E/AndroidRuntime(9506): at android.app.ActivityThread.access$600(ActivityThread.java:162)
03-10 13:01:56.904: E/AndroidRuntime(9506): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
03-10 13:01:56.904: E/AndroidRuntime(9506): at android.os.Handler.dispatchMessage(Handler.java:99)
03-10 13:01:56.904: E/AndroidRuntime(9506): at android.os.Looper.loop(Looper.java:158)
03-10 13:01:56.904: E/AndroidRuntime(9506): at android.app.ActivityThread.main(ActivityThread.java:5751)
03-10 13:01:56.904: E/AndroidRuntime(9506): at java.lang.reflect.Method.invokeNative(Native Method)
03-10 13:01:56.904: E/AndroidRuntime(9506): at java.lang.reflect.Method.invoke(Method.java:511)
03-10 13:01:56.904: E/AndroidRuntime(9506): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
03-10 13:01:56.904: E/AndroidRuntime(9506): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
03-10 13:01:56.904: E/AndroidRuntime(9506): at dalvik.system.NativeStart.main(Native Method)
03-10 13:01:56.904: E/AndroidRuntime(9506): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.ampl.bna/com.ampl.bna.RecipeDetailsFragmentActivity}: java.lang.NullPointerException
03-10 13:01:56.904: E/AndroidRuntime(9506): at android.app.ActivityThread.deliverResults(ActivityThread.java:3821)
03-10 13:01:56.904: E/AndroidRuntime(9506): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3174)
03-10 13:01:56.904: E/AndroidRuntime(9506): ... 12 more
03-10 13:01:56.904: E/AndroidRuntime(9506): Caused by: java.lang.NullPointerException
03-10 13:01:56.904: E/AndroidRuntime(9506): at android.content.ContentResolver.openInputStream(ContentResolver.java:452)
03-10 13:01:56.904: E/AndroidRuntime(9506): at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:855)
03-10 13:01:56.904: E/AndroidRuntime(9506): at com.ampl.bna.RecipeDetailsFragmentActivity.onActivityResult(RecipeDetailsFragmentActivity.java:609)
03-10 13:01:56.904: E/AndroidRuntime(9506): at android.app.Activity.dispatchActivityResult(Activity.java:5385)
03-10 13:01:56.904: E/AndroidRuntime(9506): at android.app.ActivityThread.deliverResults(ActivityThread.java:3817)
03-10 13:01:56.904: E/AndroidRuntime(9506): ... 13 more
你可以请一些时间来解决这个问题吗?
这将是很大的帮助。谢谢。