The similar SO question-answers is crashing my app.
I am taking a photo through my camera intent and sending it to my php server over net which is then saving it to a directory. It is working fine. But the photo being saved is of compromised quality (~20KB).
I know my mistake. I read Android documentation to realize I am actually sending the photo thumbnail instead of the photo itself. Here is my code
Open Camera Intent to take a photo.
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);
}
}
Receiving the Image.
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);
}
}
What do I want? I simply want to convert my original image (and not the thumbnail) to Base64 encoded string (Just like what I did with the thumbnail).
Any help will be appreciated.
[ Feel free to suggest edits. :) ]
I simply want to convert my original image (and not the thumbnail)
You do not have an "original image", because you did not include EXTRA_OUTPUT
in your ACTION_IMAGE_CAPTURE
Intent
. Add that, and then you have your full-size image (except with buggy camera apps):
/***
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
}
}
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments