照片库一直生成 "blank image"?

Photo Gallery kept having "blank image" generated?

总是有 "empty images" 生成并存储在图库中,即使我决定不拍摄图像并通过按后退按钮退出相机屏幕?有没有一种方法可以消除 MainActivity.java 中这个连续的 "blank image" 问题,而无需在我的库中的 CameraPhoto class 中进行编辑?谢谢

MainActivity.java

        CameraPicture cameraPhoto = new CameraPicture(getApplicationContext());
                Camera.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                                try {
                                    startActivityForResult(cameraPhoto.takePhotoIntent(), CAMERA_REQUEST);

                                    cameraPhoto.addToGallery();
                                }

                                catch (IOException e) {
                                    Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if(resultCode == RESULT_OK){
                if(requestCode == CAMERA_REQUEST){
                    String photoPath = cameraPhoto.getPhotoPath();
                    selectedPhoto = photoPath;
                    Bitmap bitmap = null;
                    try {
                        bitmap = ImageLoader.init().from(photoPath).requestSize(512, 512).getBitmap();
                        ivImage.setImageBitmap(getRotatedBitmap(bitmap,90));


                    } catch (FileNotFoundException e) {
                        Toast.makeText(getApplicationContext(),"Something Wrong while loading the photo!", Toast.LENGTH_SHORT).show();
                    }
                }
}

编辑:创建了另一个名为 CameraPicture 的 java class 来承载更新的推荐代码,而不是从库 CameraPhoto .jar 文件中调用以防止冲突。

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CameraPicture {
    private String photoPath;
    private Context context;

    public String getPhotoPath() {
        return this.photoPath;
    }

    public CameraPicture(Context context) {
        this.context = context;
    }

    public Intent takePhotoIntent() throws IOException {
        Intent in = new Intent("android.media.action.IMAGE_CAPTURE");
        if(in.resolveActivity(this.context.getPackageManager()) != null) {
            File photoFile = this.createImageFileName();
            if(photoFile != null) {
                in.putExtra("output", Uri.fromFile(photoFile));
            }
        }

        return in;
    }

    private File createImageFileName() throws IOException {
        String timeStamp = (new SimpleDateFormat("yyyyMMdd_HHmmss")).format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File image = new File ( storageDir + "/" + imageFileName + ".jpg");
        this.photoPath = image.getAbsolutePath();
        return image;
    }

    public void addToGallery() {
        Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
        File f = new File(this.photoPath);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        this.context.sendBroadcast(mediaScanIntent);
    }
}
cameraPhoto.addToGallery(); 

我的天哪。您在单击侦听器中调用它。非常错误的地方。您应该只在 onActivityResult() 中这样做。

仅在拍照后。

话虽如此,我发现函数名称有误。由于没有画廊,您没有向画廊添加任何内容。只有一个图库应用程序。通常还有更多图库应用程序。您正在将文件添加到 MediaStore。 Gallery 和 Fotos 应用程序在 MediaStore 中查询图像。