【问题标题】:How to get resource ID from Image Path?如何从图像路径获取资源 ID?
【发布时间】:2012-10-14 17:20:18
【问题描述】:

我在 android 中有一个图像路径。该图像存在于设备的 sdcard 上。我怎样才能找到它的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。有什么提示吗?

【问题讨论】:

    标签: android imageview


    【解决方案1】:

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

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

    【讨论】:

    • 但是你在哪里找到图片的resourceID呢?
    • 解码文件不需要资源 ID。仅当此特定图像位于您的资源文件夹中时,您才需要资源 ID。但是您提到该图像在 SD 卡上可用。您将不会获得任何资源 id,这在您的 apk 中不存在。
    • Durairaj P,你应该编辑你的评论作为答案,因为我认为这就是他想要做的。
    【解决方案2】:

    如果要解码图像,则需要使用对 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);
    
    
        }
    

    【讨论】:

    • 我有 imagePath 但现在我需要使用需要 resID 的 decodeResource(),所以我想从 imagePath 中找到它
    猜你喜欢
    • 1970-01-01
    • 2020-12-09
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    • 2020-10-24
    相关资源
    最近更新 更多