Saving photo in my application - java

I'm trying to save a photo that I have already captured in my application (to use it later like as an icon for my contact) but I don't know if I must change it from uri to bitmap or any other type. I would like to save it as a .png like my others photos
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, 1020);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1020 && resultCode == RESULT_OK) {
mImageUri = data.getData();
Uri mImageUri2 = mImageUri;
Uri aux1=mImageUri;
Bitmap foto = photoUtils.getImage(aux1);
Drawable fotofinal =new BitmapDrawable(getResources(),foto);
Thanks for the help.

Related

Delete image in the gallery after camera intent photo taken

i'm creating an image cropping application and i want to delete the image automatically in the gallery after the camera intent photo taken.
my goal is to delete automatically the image taken from the camera app after cropping the image.
this is the code for opening camera
private void pickCamera() {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "NewPic");
values.put(MediaStore.Images.Media.DESCRIPTION,"Image to Text");
image_uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,image_uri);
startActivityForResult(cameraIntent,IMAGE_PICK_CAMERA_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == IMAGE_PICK_GALLERY_CODE) {
CropImage.activity(data.getData()).setGuidelines(CropImageView.Guidelines.ON).start(this);
}
if (requestCode == IMAGE_PICK_CAMERA_CODE){
CropImage.activity(image_uri).setGuidelines(CropImageView.Guidelines.ON).start(this);
}
}
if(requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE){
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
mPreviewIv.setImageURI(resultUri);
BitmapDrawable bitmapDrawable = (BitmapDrawable)mPreviewIv.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
TextRecognizer recognizer = new TextRecognizer.Builder(getApplicationContext()).build();
if(!recognizer.isOperational()){
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
}

Putting an image to a Bundle

How can I save an image captured by the camera in a bundle and move it to the second activity?
Where am I doing wrong here?
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAM_REQUEST) {
if (resultCode == RESULT_OK) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
Intent i = new Intent(this, PostActivity.class);
i.putExtra("name", thumbnail);
startActivity(i);
}
}
}
Bitmap implements Parcelable, so you should use:
Bitmap thumbnail = (Bitmap) data.getParcelableExtra("data");
There is a few way how to pass bitmap to second Activity
You can pass Bundle form Intent.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAM_REQUEST) {
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras()
Intent i = new Intent(this, PostActivity.class);
i.putExtra("extras", extras);
startActivity(i);
}
}
}
And in the PostActivity you can call
Bundle extras = getIntent().getBundleExtra("extras")
Bitmap thumbnail = (Bitmap) extras.get("data");
Or if you want to pass only image you have to convert Bitmap to byte array
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAM_REQUEST) {
if (resultCode == RESULT_OK) {
Intent intent = new Intent(this, PostActivity.class);
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
intent.putExtra("image",byteArray);
startActivity(i);
}
}
}
Or your method should also right. You have to get Bitmap by getParcelableExtra(String) method.
Bitmap thumbnail = getIntent().getParcelableExtra("data");

aFileChoser in fragmentActivity

i made this example: swipe tab
but i can't make the aFileChooser to work with fragmanactivity.
I added it as it has to, but cant get the result_ok int the onActivityresult.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CHOOSER:
if (resultCode == RESULT_OK) {
final Uri uri = data.getData();
File file = FileUtils.getFile(uri);
Log.e("", file.getAbsolutePath());
}
super.onActivityResult(requestCode, resultCode, data);
break;
}
}
How can i manage to get the RESUL_OK to work,because the eclipse said:
RESULT_OK cannot be resolved to a variable
replace RESULT_OK with Activity.RESULT_OK then it will work..

getting uri from android camera on all versions

I'm trying to take a picture with my app, save it and retrieve the url. Currently I have:
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, TAKE_PICTURE);
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case TAKE_PICTURE:
{
if (resultCode == RESULT_OK)
{
File file = new File(getPath(data.getData()));
Date dt = new Date(file.lastModified());
String datum = dt.toLocaleString();
Comment_Resource photo = new Comment_Resource(file.getAbsolutePath(), datum, true);
}
break;
}
}
}
this works on some phones but a lot of phones don't save the picture causing a nullpointer.
try this:
Uri uri = Uri.fromFile(file);

How to process image once selected?

I'm retriving image from a IO file manager (one's installed). Anyhow when I try to retrieve an image I'm not sure where it's gone? Or how to save it as a bitmap?
Code below:
public void onClick(View v)
{
if(v.getId() == R.id.facebook_icon || v.getId() == R.id.facebook_text)
{
//Facebook
}
if(v.getId() == R.id.camera_icon || v.getId() == R.id.camera_text)
{
//Camera
}
if(v.getId() == R.id.folder_icon || v.getId() == R.id.folder_text)
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), MODE_WORLD_READABLE);
}
}
protected void onActivityForResult()
{
//Where is the image? How to get?
System.out.println("Image gotton");
}
First of all, you are not overriding onActivityResult. Note the method signature: http://developer.android.com/reference/android/app/Activity.html#onActivityResult(int, int, android.content.Intent)
You should be using #Override annotations so you know when something's wrong.
Try something like:
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
Uri imageUri = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
}
}

Categories

Resources