{"id":1515,"date":"2022-08-30T15:17:10","date_gmt":"2022-08-30T15:17:10","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/26\/android-set-bitmap-image-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:17:10","modified_gmt":"2022-08-30T15:17:10","slug":"android-set-bitmap-image-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/android-set-bitmap-image-collection-of-common-programming-errors\/","title":{"rendered":"android set Bitmap image-Collection of common programming errors"},"content":{"rendered":"<p>I am now making a painting apps, and would like to ask how to load a picture and set to a Bitmap?<\/p>\n<p>I have set the coding as follows, and links Class A and Class DrawView.<\/p>\n<h2>Question:<\/h2>\n<p>The code reports error &#8220;The method setImageBitmap(Bitmap) is undefined for the type Bitmap&#8221; in <code>DrawView Class<\/code> for the line<\/p>\n<blockquote>\n<p>bitmap.setImageBitmap(BitmapFactory.decodeFile(picturePath));<\/p>\n<\/blockquote>\n<p>, I do not know how to load a picture to Bitmap.<\/p>\n<h2>in Class A:<\/h2>\n<pre><code>private DrawView drawView;\n...\n...\n\npublic void go_load_pic() \n{       \n    Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);                \n    startActivityForResult(i, RESULT_LOAD_IMAGE);   \n}   \n\nprotected void onActivityResult(int requestCode, int resultCode, Intent data) \n{\n    super.onActivityResult(requestCode, resultCode, data);\n\n    if (requestCode == RESULT_LOAD_IMAGE &amp;&amp; resultCode == RESULT_OK &amp;&amp; null != data) \n    {\n        Uri selectedImage = data.getData();\n        String[] filePathColumn = { MediaStore.Images.Media.DATA };\n\n        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);\n        cursor.moveToFirst();\n\n        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);\n        String picturePath = cursor.getString(columnIndex);\n        cursor.close();\n\n        drawView.load_pic(picturePath);     \n    }   \n}   \n<\/code><\/pre>\n<h2>in Class DrawView:<\/h2>\n<pre><code>public class DrawView extends View  \/\/ the main screen that is painted\n{\n   \/\/ used to determine whether user moved a finger enough to draw again   \n   private static final float TOUCH_TOLERANCE = 10;\n\n   private Bitmap bitmap; \/\/ drawing area for display or saving\n   private Canvas bitmapCanvas; \/\/ used to draw on bitmap\n   private Paint paintScreen; \/\/ use to draw bitmap onto screen\n   private Paint paintLine; \/\/ used to draw lines onto bitmap\n   private HashMap pathMap; \/\/ current Paths being drawn\n   private HashMap previousPointMap; \/\/ current Points\n   ...\n\npublic void load_pic(String picturePath) \/\/ load a picture from gallery\n   {\n      bitmap.setImageBitmap(BitmapFactory.decodeFile(picturePath)); \/\/ERROR LINE\n      invalidate(); \/\/ refresh the screen\n   }\n<\/code><\/pre>\n<ol>\n<li>\n<p>You&#8217;re calling a method that doesn&#8217;t exist on the <code>Bitmap<\/code> class. That method is found on framework widgets like <code>ImageView<\/code> and <code>ImageButton<\/code>. <code>BitmapFactory<\/code> returns a <code>Bitmap<\/code> already, so just assign the instance.<\/p>\n<pre><code>bitmap = BitmapFactory.decodeFile(picturePath);\n<\/code><\/pre>\n<\/li>\n<li>\n<p>The decodeFile call will create a Bitmap from a file. Your variable bitmap- what&#8217;s its type? For that call it ought to be an ImageView, is it?<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-26 18:02:59. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am now making a painting apps, and would like to ask how to load a picture and set to a Bitmap? I have set the coding as follows, and links Class A and Class DrawView. Question: The code reports error &#8220;The method setImageBitmap(Bitmap) is undefined for the type Bitmap&#8221; in DrawView Class for the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1515","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1515"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1515\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}