How to get resource ID from Image Path?

[亡魂溺海] 提交于 2019-12-05 02:11:26

问题


I have an imagepath in android .This image exits on the sdcard of the device. How can i find resourceID of it ? I need to send it to decodeResource function. I am trying to detect faces on the image user selects from the gallery. The code that I have written is

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

In detectFaces class

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

    } 

There is a button whose onClick event is associated with

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

 }

I need ID in the decodeResource function. Any hints ?


回答1:


Try this, it may help you

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



回答2:


If you want to decode an image, you need to get the get the file path by using a query to mediastore. You can use something like this

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


    }


来源:https://stackoverflow.com/questions/12879035/how-to-get-resource-id-from-image-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!