java - Why am I getting this error "Expected resource of type raw" in Android Studio? -
i trying set default wallpaper using button reason when set inputstream in oncreate method, error "expected resource of type raw". referencing drawable folder , using icon.png image in drawable folder. following tutorials in newboston series , seems work fine travis reason mine doesnt work in android studio. error? thanks
camera.java:
package com.example.user.cameraapplication; import android.app.activity; import android.content.intent; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.os.bundle; import android.provider.mediastore; import android.view.view; import android.widget.button; import android.widget.imagebutton; import android.widget.imageview; import android.widget.switch; import java.io.ioexception; import java.io.inputstream; /** * created user on 16-08-2015. */ public class camera extends activity implements view.onclicklistener{ imageview iv; button b1,b2; imagebutton img; intent i; final static int cameractivity = 0; bitmap b; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.photo); inititalize(); inputstream = getresources().openrawresource(r.drawable.icon); b = bitmapfactory.decodestream(is); } private void inititalize() { iv = (imageview)findviewbyid(r.id.iview1); img = (imagebutton)findviewbyid(r.id.imgbtn); b1 = (button)findviewbyid(r.id.btn1); b2 = (button)findviewbyid(r.id.btn2); b1.setonclicklistener(this); b2.setonclicklistener(this); } @override public void onclick(view v) { switch(v.getid()){ case r.id.btn1: try { getapplicationcontext().setwallpaper(b); } catch (ioexception e) { e.printstacktrace(); } break; case r.id.imgbtn: = new intent(mediastore.action_image_capture); startactivityforresult(i,cameractivity); break; } } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if(resultcode==result_ok) { bundle extras = data.getextras(); b = (bitmap)extras.get("data"); iv.setimagebitmap(b); } } }
image:
the error occurred because android studio expected resource file of type raw.
solution 1:
create new folder in "res" folder called "raw", , put icon there. raw folder should contain media files of app.
then replace
inputstream = getresources().openrawresource(r.drawable.icon);
with
inputstream = getresources().openrawresource(r.raw.icon);
solution 2:
another solution this. doesn't require create raw folder:
inputstream = getresources().openrawresource(+ r.drawable.icon);
Comments
Post a Comment