Reputation: 483
I made a class to take and save pictures to internal storage, but the the problem is i save thumbnail and not full sized photo.
This is the class :
public class PhotoManager {
private final static String DEBUG_TAG = "PHOTOMANAGER";
private String mCurrentPhotoPath = "";
private String filePath = "";
private Intent takePictureIntent;
private Activity activity;
private String type;
private int Q_ID;
private PhotoCallbackListener mListener;
private FragmentManager fragManager;
public void setListener(PhotoCallbackListener listener) {
mListener = listener;
}
public PhotoManager(Activity activity, int Q_ID, String type, FragmentManager fm, PhotoCallbackListener listener) {
this.activity = activity;
this.Q_ID = Q_ID;
this.type = type;
this.mListener = listener;
this.fragManager = fm;
}
// init la vue de la camera
public void init(){
dispatchTakePictureIntent(11);
}
// affiche la camera
private void dispatchTakePictureIntent(final int actionCode) {
Fragment f = new Fragment() {
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, actionCode);
}
@Override
public void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (data != null) {
try {
handleSmallCameraPhoto(data);
} catch (IOException e) {
e.printStackTrace();
}
// efface la photo créee de la galerie
activity.getContentResolver().delete(data.getData(), null, null);
}
}
};
FragmentTransaction fragmentTransaction = this.fragManager
.beginTransaction();
fragmentTransaction.add(f, "getpicture");
fragmentTransaction.commit();
}
private void handleSmallCameraPhoto(Intent intent) throws IOException {
Bundle extras = intent.getExtras();
Bitmap mImageBitmap = (Bitmap) extras.get("data");
File f = createImageFile();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
filePath = saveToInternalStorage(mImageBitmap);
galleryAddPic();
mListener.callback(mImageBitmap, filePath);
}
private File createImageFile() throws IOException {
// Create an image file name
Log.d(DEBUG_TAG,"createImageFile");
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date(0));
String imageFileName = timeStamp + "_";
File image = File.createTempFile(
imageFileName,
".jpg",
null //default location for temporary files
);
mCurrentPhotoPath = image.getAbsolutePath();
Log.d(DEBUG_TAG,"return "+image.getAbsolutePath());
return image;
}
private String saveToInternalStorage(Bitmap bitmapImage){
ContextWrapper cw = new ContextWrapper(activity.getApplicationContext());
Log.d(DEBUG_TAG,"saveToInternalStorage");
File directory = cw.getDir("imageDir_"+type, Context.MODE_PRIVATE);
String fileName = "q"+Q_ID+".jpg";
// Create imageDir
File mypath = new File(directory, fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
Log.d(DEBUG_TAG,"return "+directory.getAbsolutePath());
return directory.getAbsolutePath();
}
private void galleryAddPic() {
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,contentUri);
activity.sendBroadcast(mediaScanIntent);
}
}
I use it like this :
pm = new PhotoManager(getActivity(), Q_ID, getArguments().getString("type"), fm, new PhotoCallbackListener() {
@Override
public void callback(Bitmap mImageBitmap, String file) {
imagePreview.setImageBitmap(mImageBitmap);
filePath = file;
}
});
Button buttonPhoto = (Button) view.findViewById(R.id.q4BtnPhoto);
buttonPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pm.init();
}
});
I return the bitmap thumbnail to put it in the layout, and the filePath just for test.
Can anyone help me to understand why the full sized photo is not saved ?
Thanks !
Upvotes: 1
Views: 743
Reputation: 483
Solved by myself :
I made a big mistake, i saved the thumbnail to internal storage not the full sized.
in the method handleSmallCameraPhoto i can get the full sized saved image with :
Uri fullsizeImage = intent.getData();
I get the bitmap of the image like this :
Bitmap mBitmap = MediaStore.Images.Media.getBitmap(activity.getContentResolver(), fullsizeImage);
Then after i call my saveToInternalStorage method.
Upvotes: 3