0
我想通过gallery.I图像中的位置的位置加载资源到位图。我得到正确的文本设置“drawableId”,但当它设置为字符串和引用在代码的位图行在代码行Bitmap b = BitmapFactory.decodeResource(getResources(), a)
中出现错误“a”。如何从字符串获取资源变量为bitmapFactory
如果我用a
替代R.drawable.pg1
它可以正常工作。
String a = drawableId.getText().toString();
Bitmap b = BitmapFactory.decodeResource(getResources(), a);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
postPicture();
}
public boolean postPicture() {
String a = drawableId.getText().toString();
Bitmap b = BitmapFactory.decodeResource(getResources(), (a));
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg"));
startActivity(Intent.createChooser(share, "Share image File"));
有关如何解决此问题的任何想法?提前致谢!
你需要检查的文档。所需的第二个参数是一个int,它是不需要字符串的资源ID。所以这** R.drawable.pg1 **应该工作正常 – Raghunandan
[decodeResource](https://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeResource(android.content.res.Resources,% 20int))接受一个int作为第二个参数,而不是一个字符串。您应该确实阅读您的错误消息 –
https://stackoverflow.com/a/38447612/115145 – CommonsWare