我在Android画廊保存照片,但照片有延迟保存

时间:2013-06-05 06:30:39

标签: android android-camera instagram android-gallery save-image

我想用我的应用程序拍照并发送到Instagram,但我遇到了一些问题,因为当我拍摄照片时,我的应用程序有一些延迟保存。

我的应用正在以这种方式进行

首先,我用我的应用程序拍照并将此照片保存在图库中。

用户将在是否开始图像之后。如果他批准他点击一个按钮并发送到Instagram,但我不知道为什么该应用程序需要几秒钟来保存我的图像。

获取我想要使用的图像的方法是获取我保存在文件夹库中的最后一张图片。

这是我打电话拍照的代码。

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())));

这是我用来获取图片并保存

的类
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;
}

以下是我接受照片并发送到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();
}

任何人都可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

确定。我解决了这个问题。

我用来保存图片的课我改变了这部分...

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

out.flush();

现在我用这个......

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

我用来接受照片并发送到Intagram的代码我改为

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");

退出光标。