我正在将照片保存在android画廊中,但是照片保存时间有所延迟

问题描述 投票:0回答:1

我想用我的应用拍照,然后将其发送到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();
}

任何人都可以帮助我吗?

android android-camera instagram android-gallery save-image
1个回答
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);

以及我用来接受照片并将其发送到Instagram的代码更改为

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

退出光标。

© www.soinside.com 2019 - 2024. All rights reserved.