如何从Image Path获取资源ID?

时间:2012-10-14 03:25:21

标签: android imageview

我在android中有一个imagepath。这个图像在设备的SD卡上退出。我怎样才能找到它的resourceID?我需要将它发送到decodeResource函数。我试图检测用户从图库中选择的图像上的面部。我写的代码是

    Intent intent = new Intent(this, DetectFaces.class);
    intent.putExtra(PICTURE_PATH,picturePath);//the path of the image
    startActivity(intent);

在detectFaces类中

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detect_faces);
       // getActionBar().setDisplayHomeAsUpEnabled(true);
        Intent intent = getIntent();
        ImageView imageView = (ImageView) findViewById(R.id.imgView);
        picturePath = intent.getStringExtra(GetFaceActivity.PICTURE_PATH);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        //imageView.setOnTouchListener(this);

    } 

有一个按钮,其onClick事件与

相关联
public void DetectFacesInImage(View view)
{
    BitmapFactory.Options bitmapFactoryOptions=new BitmapFactory.Options();
    bitmapFactoryOptions.inPreferredConfig= Bitmap.Config.RGB_565;
myBitmap=BitmapFactory.decodeResource(getResources(),R.drawable.faceswapping,bitmapFactoryOptions);
    int width=myBitmap.getWidth();
    int height=myBitmap.getHeight();
    detectedFaces=new FaceDetector.Face[number_of_faces];
    FaceDetector faceDetector=new FaceDetector(width,height,number_of_faces);
    number_of_faces_detected = faceDetector.findFaces(myBitmap, detectedFaces);

 }

我在decodeResource函数中需要ID。任何提示?

2 个答案:

答案 0 :(得分:2)

试试这个,它可能对你有帮助

File fileObj = new  File(“/sdcard/Images/test_image.jpg”);
if(fileObj .exists()){
    Bitmap bitMapObj= BitmapFactory.decodeFile(fileObj .getAbsolutePath());
    ImageView imgView= (ImageView) findViewById(R.id.imageviewTest);
    imgView.setImageBitmap(bitMapObj);
}

答案 1 :(得分:-1)

如果要解码图像,则需要使用对mediastore的查询来获取文件路径。你可以使用这样的东西

public String getRealPathFromURI(Uri contentUri) {
        String[] proj = { MediaStore.Images.Media.DATA,MediaStore.Images.Media._ID };
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String path = cursor.getString(column_index);


    }