繁体   English   中英

我正在将照片保存在 android 图库上,但照片有延迟保存

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

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

任何人都可以帮助我吗?

行。 我通过这样做解决了这个问题。

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

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

并退出光标。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM