Android系统。 onActivityResult出错

时间:2011-10-21 17:26:10

标签: android android-activity camera

我创建了活动,它为相机设备提供标准程序。创建图片后,它将存储在手机存储器中,并将图片名称存入数据库。问题是,如果您创建一些照片,那么数据库中的照片和记录的名称就不匹配。 例如: 那堆:

10-21 20:22:37.617: I / System.out (22 016): Debug - name of foto - 70,134,596
10-21 20:22:45.195: I / System.out (22 016): Debug - name of foto - 89,410,152
10-21 20:22:45.382: I / System.out (22 016): Debug - name of database record - 89,410,152

在这种情况下,创建了一张名为 - 70134596的照片,但数据库获得了一个记录号 - 89410152.这是代码:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        File photo = null;

        date = (int) (Math.random() * 100000000);

        photo = new File(Environment.getExternalStorageDirectory()
                + "/DebtorMini", date + ".jpg");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));

        imageUri = Uri.fromFile(photo);
        System.out.println("Debug - name of foto - " + date);

        startActivityForResult(intent, TAKE_PICTURE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case TAKE_PICTURE:
            if (resultCode == Activity.RESULT_OK) {
                Uri selectedImage = imageUri;
                getContentResolver().notifyChange(selectedImage, null);
                ImageView imageView = new ImageView(ctx);
                setContentView(imageView);
                ContentResolver cr = getContentResolver();
                Bitmap bitmap = null;

                try {
                    bitmap = android.provider.MediaStore.Images.Media
                            .getBitmap(cr, selectedImage);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                imageView.setImageBitmap(bitmap);
                Toast.makeText(this, selectedImage.toString(),
                        Toast.LENGTH_LONG).show();

                DataBaseHelper helper = new DataBaseHelper(this);
                try {
                    helper.createDataBase();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                Bundle extras = getIntent().getExtras();
                String category = extras.getString("category");
                String address = extras.getString("address");

                Toast.makeText(
                        this,
                        "Фотография успешно создана категория - " + category
                                + "  адресс - " + address, Toast.LENGTH_SHORT)
                        .show();
                helper.openDataBase();
                helper.exec("INSERT INTO 'photos' (`id`, `address`, `category`) VALUES ('"
                        + date + "', '" + address + "', '" + category + "')");
                System.out.println("Debug - name of database record - " + date);

                helper.close();
            }
        }

    }

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我想你回到你发布的活动时,你的日期字段会被设置为一个新的随机数。在onCreate函数中放置一个断点,我怀疑它被多次调用。这很可能是由于手机的旋转造成的。

话虽如此,您应该只是查询媒体提供程序以获取存储媒体的文件的名称。或者,您应该使用onSaveInstanceState和onRestoreInstanceState来保留日期字段。

修改

Cursor c = getContentResolver().query(mediaUri, new String[] { MediaStore.Images.ImageColumns.DATA }, null,null,null);
if(c != null) { 
    try {
        if(c.moveToFirst()) {
            String fullFilePath = c.getString(0); 
            String fileName = new File(fullFilePath).getName();
        }
    } finally { 
         c.close();
    }
 }