i have 2 button on mainactivity. 1 choosing image gallery , 1 camera image capturing. want send these images second activity. while trying gallery image, successful. but, want pass captured image second activity. have tried many code not able desire result.
below code....
mainactivity.java
package com.myfirstapp.myfirstapp; import android.app.activity; import android.content.intent; import android.net.uri; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; public class mainactivity extends activity { button btn_choose_from_gallery, btn_go_to_camera; int request_gallery = 1; int request_camera = 100; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); btn_choose_from_gallery = (button) findviewbyid(r.id.btn_choose_from_gallery); btn_go_to_camera = (button) findviewbyid(r.id.btn_go_to_camera); btn_choose_from_gallery.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub intent = new intent( intent.action_pick, android.provider.mediastore.images.media.external_content_uri); startactivityforresult(i, request_gallery); } }); btn_go_to_camera.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub intent intent_camera = new intent( android.provider.mediastore.action_image_capture); startactivityforresult(intent_camera, request_camera); } }); } /* choose image gallery & camera onactivityresult */ @override protected void onactivityresult(int reqcode, int rescode, intent data) { // todo auto-generated method stub super.onactivityresult(reqcode, rescode, data); if (rescode == result_ok) { if (reqcode == request_camera) { if (data != null) { uri capturedimguri = data.getdata(); /* passing imageuri second activity */ intent intentcamera = new intent(this, second.class); intentcamera.putextra("capturedimguri", capturedimguri); startactivity(intentcamera); } } else if (reqcode == request_gallery) { if (data != null) { uri selectedimguri = data.getdata(); /* passing imageuri second activity */ intent intentgallery = new intent(this, second.class); intentgallery.setdata(selectedimguri); startactivity(intentgallery); } } } } /* choose image gallery onactivityresult */// code works fine when // want choose // image gallery// // @override // protected void onactivityresult(int reqcode, int rescode, intent data) { // // todo auto-generated method stub // super.onactivityresult(reqcode, rescode, data); // // if (reqcode == request_gallery && rescode == result_ok && data != null) { // uri selectedimguri = data.getdata(); // // /* passing imageuri second activity */ // intent img_pass = new intent(this, second.class); // img_pass.setdata(selectedimguri); // startactivity(img_pass); // } // } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } } second.java
package com.myfirstapp.myfirstapp; import android.app.activity; import android.database.cursor; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.matrix; import android.graphics.pointf; import android.graphics.drawable.bitmapdrawable; import android.net.uri; import android.os.bundle; import android.provider.mediastore; import android.util.displaymetrics; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.view.onclicklistener; import android.view.animation.animation; import android.view.animation.animationutils; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.imageview; import android.widget.relativelayout; import com.devsmart.android.ui.horizontallistview; public class second extends activity { touchimageview img_to_be_zoomed, img_to_be_zoomed_second; imageview img_back; horizontallistview hlistview, hlistviewfirst; bitmap bitmap_img; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_second); img_to_be_zoomed = (touchimageview) findviewbyid(r.id.img_to_be_zoomed); img_to_be_zoomed_second = (touchimageview) findviewbyid(r.id.img_to_be_zoomed_second); img_back = (imageview) findviewbyid(r.id.img_back_icon); hlistview = (horizontallistview) findviewbyid(r.id.horizontal_list_view); hlistviewfirst = (horizontallistview) findviewbyid(r.id.horizontal_list_view_first); /* top icon */ img_back.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub finish(); } }); /* left image touch event */ img_to_be_zoomed .setontouchimageviewlistener(new com.myfirstapp.myfirstapp.touchimageview.ontouchimageviewlistener() { @override public void onmove() { img_to_be_zoomed_second.setzoom(img_to_be_zoomed); pointf pointf_img1 = new pointf(); pointf_img1 = img_to_be_zoomed.getscrollposition(); img_to_be_zoomed_second.setscrollposition( 1 - pointf_img1.x, pointf_img1.y); } }); /* right image touch event */ img_to_be_zoomed_second .setontouchimageviewlistener(new com.myfirstapp.myfirstapp.touchimageview.ontouchimageviewlistener() { @override public void onmove() { img_to_be_zoomed.setzoom(img_to_be_zoomed_second); pointf pointf_img1 = new pointf(); pointf_img1 = img_to_be_zoomed_second .getscrollposition(); img_to_be_zoomed.setscrollposition(1 - pointf_img1.x, pointf_img1.y); } }); int[] horizontallistimages = new int[] { r.drawable.icon_grid, r.drawable.icon_text, r.drawable.icon_clip_art }; final int[] horizontallistimagesfirst = new int[] { r.drawable.icon_go_back, r.drawable.icon_horizontal_grid, r.drawable.icon_vertical_grid }; /* animation references */ final animation slideup = animationutils.loadanimation( getapplicationcontext(), r.anim.slide_up); final animation slidedown = animationutils.loadanimation( getapplicationcontext(), r.anim.slide_down); /* setting adapter horizontal list views */ horizontallistviewadapter horizontallistviewadapter = new horizontallistviewadapter( second.this, horizontallistimages); hlistview.setadapter(horizontallistviewadapter); /* horizontal list view item click listener */ hlistview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // todo auto-generated method stub switch (position) { case 0: horizontallistviewadapterfirst horizontallistviewadapterfirst = new horizontallistviewadapterfirst( second.this, horizontallistimagesfirst); hlistviewfirst.setadapter(horizontallistviewadapterfirst); hlistview.startanimation(slidedown); hlistview.setvisibility(view.gone); hlistviewfirst.startanimation(slideup); hlistviewfirst.setvisibility(view.visible); break; default: break; } } }); hlistviewfirst.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // todo auto-generated method stub switch (position) { case 0: hlistviewfirst.startanimation(slidedown); hlistviewfirst.setvisibility(view.gone); hlistview.startanimation(slideup); hlistview.setvisibility(view.visible); break; case 1: displaymetrics displaymetrics = new displaymetrics(); getwindowmanager().getdefaultdisplay().getmetrics( displaymetrics); int height = displaymetrics.heightpixels; int width = displaymetrics.widthpixels; /* setting center layout both images */ relativelayout.layoutparams imagelayoutparams = new relativelayout.layoutparams( width, width); imagelayoutparams.addrule(relativelayout.center_in_parent); ((relativelayout) findviewbyid(r.id.imagelayout)) .setlayoutparams(imagelayoutparams); relativelayout.layoutparams layoutparamsleft = new relativelayout.layoutparams( width / 2, width); layoutparamsleft.setmargins(0, 0, 0, 0); img_to_be_zoomed.setlayoutparams(layoutparamsleft); bitmap_img = ((bitmapdrawable) img_to_be_zoomed .getdrawable()).getbitmap(); bitmap_img = flipimage(bitmap_img, 2); img_to_be_zoomed_second.setimagebitmap(bitmap_img); relativelayout.layoutparams layoutparamsright = new relativelayout.layoutparams( width / 2, width); layoutparamsright.setmargins(width / 2, 0, 0, 0); img_to_be_zoomed_second.setlayoutparams(layoutparamsright); // img_to_be_zoomed.invalidate(); img_to_be_zoomed_second.setvisibility(view.visible); break; default: break; } } }); /* getting imageuri gallery main activity */ uri selectedimguri = getintent().getdata(); string[] selectedimgpath = { mediastore.images.media.data }; cursor cursor = getcontentresolver().query(selectedimguri, selectedimgpath, null, null, null); cursor.movetofirst(); int indexcol = cursor.getcolumnindex(selectedimgpath[0]); string imgpath = cursor.getstring(indexcol); cursor.close(); img_to_be_zoomed.setimagebitmap(bitmapfactory.decodefile(imgpath)); /* getting image camera main activity */ uri capturedimguri = getintent().getparcelableextra("capturedimguri"); log.e("uri", "" + capturedimguri); img_to_be_zoomed.setimageuri(capturedimguri); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.second, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } /* flip image class */ public bitmap flipimage(bitmap src, int type) { matrix matrix = new matrix(); /* flip vertically */ if (type == 1) { matrix.prescale(1.0f, -1.0f); /* flip horizontally */ } else if (type == 2) { matrix.prescale(-1.0f, 1.0f); } else { return null; } return bitmap.createbitmap(src, 0, 0, src.getwidth(), src.getheight(), matrix, true); } } manifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.myfirstapp.myfirstapp" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="11" android:targetsdkversion="17" /> <!-- required permissions --> <uses-permission android:name="android.permission.camera" /> <uses-feature android:name="android.hardware.camera" /> <uses-permission android:name="android.permission.write_external_storage" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".second" android:label="@string/title_activity_second" > </activity> </application> </manifest> i getting error below....
01-05 04:39:21.055: e/androidruntime(7471): fatal exception: main 01-05 04:39:21.055: e/androidruntime(7471): java.lang.runtimeexception: unable start activity componentinfo{com.myfirstapp.myfirstapp/com.myfirstapp.myfirstapp.second}: java.lang.nullpointerexception 01-05 04:39:21.055: e/androidruntime(7471): @ android.app.activitythread.performlaunchactivity(activitythread.java:1956) 01-05 04:39:21.055: e/androidruntime(7471): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1981) 01-05 04:39:21.055: e/androidruntime(7471): @ android.app.activitythread.access$600(activitythread.java:123) 01-05 04:39:21.055: e/androidruntime(7471): @ android.app.activitythread$h.handlemessage(activitythread.java:1147) 01-05 04:39:21.055: e/androidruntime(7471): @ android.os.handler.dispatchmessage(handler.java:99) 01-05 04:39:21.055: e/androidruntime(7471): @ android.os.looper.loop(looper.java:137) 01-05 04:39:21.055: e/androidruntime(7471): @ android.app.activitythread.main(activitythread.java:4424) 01-05 04:39:21.055: e/androidruntime(7471): @ java.lang.reflect.method.invokenative(native method) 01-05 04:39:21.055: e/androidruntime(7471): @ java.lang.reflect.method.invoke(method.java:511) 01-05 04:39:21.055: e/androidruntime(7471): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:784) 01-05 04:39:21.055: e/androidruntime(7471): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:551) 01-05 04:39:21.055: e/androidruntime(7471): @ dalvik.system.nativestart.main(native method) 01-05 04:39:21.055: e/androidruntime(7471): caused by: java.lang.nullpointerexception 01-05 04:39:21.055: e/androidruntime(7471): @ android.content.contentresolver.acquireprovider(contentresolver.java:913) 01-05 04:39:21.055: e/androidruntime(7471): @ android.content.contentresolver.query(contentresolver.java:305) 01-05 04:39:21.055: e/androidruntime(7471): @ com.myfirstapp.myfirstapp.second.oncreate(second.java:185) 01-05 04:39:21.055: e/androidruntime(7471): @ android.app.activity.performcreate(activity.java:4492) 01-05 04:39:21.055: e/androidruntime(7471): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1049) 01-05 04:39:21.055: e/androidruntime(7471): @ android.app.activitythread.performlaunchactivity(activitythread.java:1920) 01-05 04:39:21.055: e/androidruntime(7471): ... 11 more thanks in advance....
i have founded solution, not same way want different way.....
change code mainactivity.java
/* choose image gallery & camera onactivityresult */ @override protected void onactivityresult(int reqcode, int rescode, intent data) { // todo auto-generated method stub super.onactivityresult(reqcode, rescode, data); if (rescode == result_ok) { if (reqcode == request_camera) { if (data != null) { bitmap photo = (bitmap) data.getextras().get("data"); /* passing bitmap second activity */ intent intentcamera = new intent(this, second.class); intentcamera.putextra("bitmapimage", photo); startactivity(intentcamera); } } else if (reqcode == request_gallery) { if (data != null) { uri selectedimguri = data.getdata(); /* passing imageuri second activity */ intent intentgallery = new intent(this, second.class); intentgallery.setdata(selectedimguri); startactivity(intentgallery); } } } } change code second.java
/* getting imageuri gallery main activity */ uri selectedimguri = getintent().getdata(); if (selectedimguri != null) { log.e("gallery imageuri", "" + selectedimguri); string[] selectedimgpath = { mediastore.images.media.data }; cursor cursor = getcontentresolver().query(selectedimguri, selectedimgpath, null, null, null); cursor.movetofirst(); int indexcol = cursor.getcolumnindex(selectedimgpath[0]); string imgpath = cursor.getstring(indexcol); cursor.close(); img_to_be_zoomed.setimagebitmap(bitmapfactory.decodefile(imgpath)); } /* getting imagebitmap camera main activity */ intent intent_camera = getintent(); bitmap camera_img_bitmap = (bitmap) intent_camera .getparcelableextra("bitmapimage"); if (camera_img_bitmap != null) { img_to_be_zoomed.setimagebitmap(camera_img_bitmap); } } hope someone....
Comments
Post a Comment