2012-10-14 97 views
2

我在android中有一个图像路径。此图像在设备的SD卡上退出。我如何找到它的resourceID?我需要将它发送到decodeResource函数。我试图检测用户从图库中选择的图像上的脸部。我写的代码是如何从图像路径获取资源ID?

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

} 

相关的,我需要ID在decodeResource功能。任何提示?

回答

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); 
} 
+0

但你在哪里找到其中的图像resourceID? – Ankuj

+2

您不需要资源ID来解码文件。只有当这个特定的图像在您的资源文件夹中时,您才需要资源ID。但是你提到了SD卡上的图像。你不会得到任何资源ID,这在你的apk中不存在。 –

+0

Durairaj P,你应该编辑你的评论作为答案'因为我认为这就是他正在做的。 – Raz

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


    } 
+0

我有imagePath,但现在我需要使用decodeResource(),它需要resID,所以我想从imagePath中找到它 – Ankuj