从Bitmap获取图片ID

时间:2013-05-21 16:43:02

标签: android image thumbnails

有没有办法从以下ActivityResult方法中获取图片ID?

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if(requestCode == IMAGE_CAPTURE){
            if(resultCode == RESULT_OK){
                try{
                    Bitmap b1 = MediaStore.Images.Media.getBitmap(getContentResolver(),imageUri);

                    System.out.println("GGEGEGGEGEGEGGEGEGEGEG" + imageUri.getPath());
//                   Intent i2 = new Intent(Intent.ACTION_VIEW, imageUri);
//                   startActivity(i2);
                }
                catch(FileNotFoundException e ){
                    System.out.println("OnActivityResult hat einen Fehler geworfen");
                } catch (IOException e) {
                    System.out.println("ONACTIVITYRESULT hat einen Fehler geworfen");
                }
            }
        }
    }

我需要它来缩略图到某个图像。

编辑:我捕获了这样的图像:

private void takePic() {
        ContentValues values = new ContentValues();
        String TITLE = null;
        values.put(MediaColumns.TITLE, TITLE);
        String DESCRIPTION  = null;
        long id = (Long) null;
        values.put(ImageColumns.DESCRIPTION, DESCRIPTION);
        values.put(MediaColumns.MIME_TYPE, "image/jpeg");
        values.put(MediaColumns._ID, id);
        imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);


        System.out.println("VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVvv" + imageUri.getPath());
        startActivityForResult(intent, IMAGE_CAPTURE);


    }

1 个答案:

答案 0 :(得分:0)

创建imageUri后,您可以通过以下方式获取ID:

    String[] columns = {MediaColumns._ID}; 
    Cursor cursor =  getContentResolver().query(imageUri, columns, null, null, null); 
    cursor.moveToFirst(); 
    long id = cursor.getLong(0);

注意:您应该从takePic()中的代码中删除此行,因为行ID是由内容提供商为您创建的:

    values.put(MediaColumns._ID, id);