How to get resource ID from Image Path?(如何从图像路径中获取资源 ID?)
问题描述
我在 android 中有一个图像路径.该图像存在于设备的 sdcard 上.我怎样才能找到它的resourceID?我需要将它发送到 decodeResource 函数.我正在尝试检测用户从图库中选择的图像上的面孔.我写的代码是
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);
在detectFaces类中
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);
}
有一个按钮,其onClick事件关联
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);
}
我需要 decodeResource 函数中的 ID.有什么提示吗?
I need ID in the decodeResource function. Any hints ?
推荐答案
试试这个,也许对你有帮助
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);
}
这篇关于如何从图像路径中获取资源 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从图像路径中获取资源 ID?


- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01