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