Ankuj
Ankuj

Reputation: 723

How to get resource ID from Image Path?

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 ?

Upvotes: 3

Views: 4399

Answers (2)

Durairaj Packirisamy
Durairaj Packirisamy

Reputation: 4725

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


    }

Upvotes: -1

RajeshVijayakumar
RajeshVijayakumar

Reputation: 10622

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

Upvotes: 2

Related Questions