androidandroid-camerainstagramandroid-gallerysave-image

I'm saving the photo on the android gallery but the photo has a delay to save


I want to take a picture with my app and send it to Instagram, but I'm with some problem because when I shot the photo my app has some delay to save.

My app is doing in this way

First I take a picture with my app and save this picture in the gallery.

After the user will approve or not the image. If he approves he clicks on a button and send it to Instagram, but I don't know why the app is taking some seconds to save my image.

My method to get the image I want to use is to get the last picture I saved on my folder's gallery.

This is the code I call to take a picture.

camera = cameraSurfaceView.getCamera();
camera.takePicture(new ShutterCallback() {

    @Override
    public void onShutter() {
        AudioManager mgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        mgr.playSoundEffect(AudioManager.FLAG_PLAY_SOUND);
    }
}, null, new HandlePictureStorage(preview,cameraSurfaceView.getFront()));

imageSelected = false;

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

Here is the class I use to get the picture and save

public class HandlePictureStorage implements PictureCallback {

FrameLayout preview;
int wid = 0;
Bitmap bitmapFinal;
boolean front;

File storagePath = new File(Environment.getExternalStorageDirectory() + "/Tubagram/");

public HandlePictureStorage(FrameLayout preview, boolean front){
    this.preview = preview;
    this.front = front;
}

@Override
public void onPictureTaken(byte[] picture, Camera camera) {

    Matrix matrix = new Matrix();

    if (front){
        matrix.postRotate(270);
    }else{
        matrix.postRotate(90);
    }

    Bitmap cameraBitmap = BitmapFactory.decodeByteArray(picture, 0, picture.length);

    wid = cameraBitmap.getWidth();

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(cameraBitmap, wid, wid, true);
    Bitmap imagemRotacionada = Bitmap.createBitmap(scaledBitmap,0,0,scaledBitmap.getWidth(),scaledBitmap.getHeight(),matrix,true);

    Bitmap newImage = Bitmap.createBitmap
            (612, 612, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(newImage);

    canvas.drawBitmap(imagemRotacionada, 0f, 0f, null);

    Drawable drawable = preview.getForeground();
    drawable.setBounds(0, 0, 612, 612);
    drawable.draw(canvas);

    File myImage = new File(storagePath,"TUBAGRAM_" + System.currentTimeMillis() +".png");

    try
    {
        FileOutputStream out = new FileOutputStream(myImage);
        newImage.compress(Bitmap.CompressFormat.PNG, 100, out);
        bitmapFinal = newImage;

        out.flush();
        out.close();
        Log.i("Imagem Tubagram salva em ", ""+  myImage);
    }
    catch(FileNotFoundException e)
    {
        Log.d("In Saving File", e + "");    
    }
    catch(IOException e)
    {
        Log.d("In Saving File", e + "");
    }

    drawable = null;

//      newImage.recycle();
    newImage = null;

    cameraBitmap.recycle();
    cameraBitmap = null;
}

And here is the code when I accept the photo and send to Instagram

if (verificaInstagram()){

    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("image/*");

    Cursor c1 = pickLastPhotoAlbum();

    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+Environment.getExternalStorageDirectory()+ "/Tubagram/" + c1.getString(1)+".png"));
    shareIntent.setPackage("com.instagram.android");

    c1.close();

    startActivity(shareIntent);

}else{
    Toast.makeText(v.getContext(), "Você não possui o Instagram no seu smartphone!", Toast.LENGTH_SHORT).show();
}

Anyone can help me with this?


Solution

  • Ok. I solved this question by doing this.

    The class I use to save the picture I changed this part...

    FileOutputStream out = new FileOutputStream(myImage);
    newImage.compress(Bitmap.CompressFormat.PNG, 100, out);
    bitmapFinal = newImage;
    
    out.flush();
    

    and now I use this one...

    String nomeImagem = "TUBAGRAM_" + System.currentTimeMillis();
    url = MediaStore.Images.Media.insertImage(preview.getContext().getContentResolver(), newImage ,nomeImagem, null);
    

    And the code I use to accept the photo and send to Instagram I changed to

    caminhoImagens = getRealPathFromURI(Uri.parse(hps.getUrl()));
    
    Log.i("Caminho imagem: ", caminhoImagens);
    
    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + caminhoImagens));
    
    shareIntent.setPackage("com.instagram.android");
    

    And retire the cursor.