类似的SO问题解答正在崩溃我的应用程序。
我正在通过我的相机意图拍照并通过网络将其发送到我的php服务器,然后将其保存到目录中。 工作正常。但保存的照片质量受损(约20KB)。
我知道我的错误。 我阅读了Android文档,意识到我实际上是在发送照片缩略图而不是照片本身。这是我的代码
打开相机意图拍摄照片。
addImage = (ImageButton) findViewById(R.id.imageButton);
addImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
}
接收图像。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK){
photo = (Bitmap) data.getExtras().get("data");
addImage.setImageDrawable(null);
addImage.setBackgroundColor(Color.parseColor("#ffffff"));
addImage.setImageBitmap(photo);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] photoByte = baos.toByteArray();
encodedImage = Base64.encodeToString(photoByte,Base64.DEFAULT);
}
}
我想要什么? 我只是想将原始图像(而不是缩略图)转换为Base64编码的字符串(就像我对缩略图所做的那样)。
任何帮助将不胜感激。
[随意建议编辑。 :)]
答案 0 :(得分:1)
我只想转换原始图片(而不是缩略图)
您没有“原始图片”,因为您的EXTRA_OUTPUT
ACTION_IMAGE_CAPTURE
中未包含Intent
。添加它,然后你有你的全尺寸图像(除了有错误的相机应用程序):
/***
Copyright (c) 2008-2016 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
From _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/
package com.commonsware.android.camcon;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import java.io.File;
public class CameraContentDemoActivity extends Activity {
private static final String EXTRA_FILENAME=
"com.commonsware.android.camcon.EXTRA_FILENAME";
private static final String FILENAME="CameraContentDemo.jpeg";
private static final int CONTENT_REQUEST=1337;
private File output=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (savedInstanceState==null) {
File dir=
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
dir.mkdirs();
output=new File(dir, FILENAME);
}
else {
output=(File)savedInstanceState.getSerializable(EXTRA_FILENAME);
}
if (output.exists()) {
output.delete();
}
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
startActivityForResult(i, CONTENT_REQUEST);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(EXTRA_FILENAME, output);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == CONTENT_REQUEST) {
if (resultCode == RESULT_OK) {
// spawn an IntentService to encode and upload your File
}
}
}
}