Android : how to start camera intent and save a non-compressed picture

on Sunday, August 10, 2014


I am a very young self taught developer and I'm working on my first major project, which requires to start a camera intent once pressed, save the image that the user took and display it in a custom dialog.

I got it to work, but i stored the returned bitmap in onactivityresult so the picture is compressed and that destroys the functionality of the app.


HERE IS THE CODE THAT DOES WORK:


start intent:



Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);


Recieve Intent and send data to dialog:



Bundle bundle = data.getExtras();
File file = new File(getCacheDir() + "/app"
+ System.currentTimeMillis() + ".jpg");
Bitmap bitmap = (Bitmap) bundle.get("data");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100 /* ignored for PNG */,
bos);
byte[] bitmapdata = bos.toByteArray();

// write the bytes in file
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos = new FileOutputStream(file);
fos.write(bitmapdata);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

mdialog.setPic(file.getAbsolutePath());


Display the picture in the custom dialog:



public void setPic(final String mURi) {
this.mURI = mURi;

if (mURI != null) {
hwPic.postDelayed(new Runnable() {

@Override
public void run() {
Drawable d = Drawable.createFromPath(mURI);

hwPic.setImageDrawable(d);;
hwPic.setVisibility(View.VISIBLE);

}
}, 1000);
}
}


This works fine but since the picture is compressed any reasonably sized font in the picture is blury and illegible.


HERE IS THE CODE THAT DOES NOT WORK:


Initialize Variable:



private String MURID;


Start intent:


File file = new File(getCacheDir() + "/app" + System.currentTimeMillis() + ".jpg");



if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
file.delete();
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
MURID=file.getAbsolutePath();
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT , Uri.parse(MURID));
startActivityForResult(intent, 1);


recieve intent and send to mydialog:



@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {// camera intent for the dialog picture
if (resultCode == RESULT_OK) {


mdialog.setPic(MURID);

}
}
}


setpic remains the same(in the dialog):



public void setPic(final String mURi) {
this.mURI = mURi;

if (mURI != null) {
hwPic.postDelayed(new Runnable() {

@Override
public void run() {
Drawable d = Drawable.createFromPath(mURI);

hwPic.setImageDrawable(d);;
hwPic.setVisibility(View.VISIBLE);

}
}, 1000);
}
}


Im not getting any response from it and logcat didnt give me any errors either, what seems to be the problem? any help would be greatly apprecieated.


BTW: i want this to work with phones without sdcards as well.


0 comments:

Post a Comment