I have an issue displaying a picture taken from the Camera.
I proceed as the following:
Calling Camera Activity -> Saving Picture -> Keeping Picture Location as a string.
Creating a new Intent -> Setting String extra with the Picture Location.
Calling the activity and retreiving the string.
Switching back to an URI using Parse on the string containing the file location.
Calling SetImageURI on the ImageView. <- this crashes.
I get the "Application had to close unexpectedly" thing..
Not really handy to debug.
There's the code for the Activity:
[Activity (Label = "ViewPhoto")]
public class ViewPhoto : Activity
{
ImageView image;
protected override void OnResume ()
{
string PhotoLocation = Intent.GetStringExtra ("PicLoc");
Android.Net.Uri _img = Android.Net.Uri.Parse (PhotoLocation);
image.SetImageURI (_img);
}
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView(Resource.Layout.PhotoViewer);
image = FindViewById<ImageView> (Resource.Id.exCurrentPhoto);
}
}
I checked the picture weight.. it's 9kb (its probably a simple picture generated through the android emulator I use).. so I doubt it's a memory issue o.o
Thanks for helping.
I hate myself for not noticing I had forgotten to call the base.OnResume() function...
Without exception being thrown I didn't realise until I randomly had to go through the Android LogCat x)
Problem solved =D
Related
I have a recyclerView which will display all images from JSON path and I used a click event that will display the clicked image to a new activity.
Now I want to print the image that is displayed. I have put a print button in the activity and while clicking on the print button i should get image to be printed in the network printer. I have used the below code. but I am getting error in getActivity() .
Button print = findViewById(R.id.print);
//imageView.setDrawingCacheEnabled( true );
print.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PrintHelper photoPrinter = new PrintHelper(getActivity());
photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
//Bitmap bitmap = imageView.getDrawingCache( );
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
photoPrinter.printBitmap("test print",bitmap);
}
});
Any help will be greatly appreciated :)
Use Activity.this instead of getActivity() where Activity is the name of your Activity.
The method getActivity() is used when you want to reference Fragments. But here you have an activity so there is an error! You have to do something like this:
//Other code
PrintHelper photoPrinter = new PrintHelper(YourJavaClassName.this);
//Other code
Please be sure to change YourJavaClassName to your activity's class name.
You can use getapplicationcontext() method or YourCurrentActivityName.this
I have 2 class: Main and Image.
My problem is how can I call a function from Image to Main because I have this error: Non-static method 'pickImg()' cannot be referenced from a static context
Main = main program
Image = getting image by camera capture/gallery and cropping
Main.java
public void getImg(){
ImageView iv1 = findViewById(R.id.imgVw);
Bitmap img = Image.pickImg();
iv1.setImageBitmap(img);
}
Image.java
public void pickImg() {
try {
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(captureIntent, CAMERA_CAPTURE);
} catch (ActivityNotFoundException anfe) {
Toast toast = Toast.makeText(this, "This device doesn't support the crop action!", Toast.LENGTH_SHORT);
toast.show();
}
}
I read some topics about the error above and i tried to change public void pickImg() to public static Bitmap pickImg(Bitmap img) and put return at the end to return the image to fulfill the error above but I get an error to startActivityForResult. I also tried using #Override to make another void function inside pickImg() but I get Annotations are not allowed here error.
But when I put (based on some answers that I read)
Image IPick = new Image();
Bitmap newI = IPick.pickImg();
public void pickImg() changes to public Bitmap pickImg() I get no error but I dont know what or how to return the image to Main since that function has try{}catch{} for camera intent only and that function is the first function that is needed and the one that will be called for getting image.
Any help/suggestion/getaround?
For those who will ask why I created 2 class instead of using 1, I created different class to avoid long codes in Main.java since I still have things to put on my app.
[EDIT-TL;DR]
I can take image with camera when i put my codes on Main.java. But if I put my codes on Image.java and call the function on Main.java, that is where im having a problem. And I can't seem to put the cropped image to imageview(app crashes).
Create the constructor of Image class and pass the MainActivity reference in constructor.
for e.g.
class Image
{
private MainActivity mActivity;
public Image(MainActivity activity){
this.mActivity=activity;
}
}
& then call your main activity methods from image class.
mActivity.yourActivityMethod();
If im staying in the app, this works fine
Click button to take me to new activity:
intent.putExtra("invite_id", invite_id);
startActivity(intent);
Receiving Activity:
Bundle b = getIntent().getExtras(); //invite id is in here
Now here is the weird part. If I am in the app, then click home button to leave the app and go to the native contacts app and save ANYTHING (like edit a name or number...the problem only occurs if I actually save something), then go to recent apps and open up my app from there... now if I click the button to launch my intent to take me to a new activity, the receiving activity returns a null bundle
Bundle b = getIntent().getExtras(); //returns null
Why could this be happening?
String b = getIntent().getStringExtra("invite_id");
Intent extras are always persisted across activity death and recreation. So, if you're stashing that extra value, it will continue to be there if you are resuming the app with the recent app switcher.
I would verify that you are stashing values in the intent.
simply that means that your activity being recreated so you have to options to avoid that:
Avoid recreating your activity by setting this to your Activity within the manifest
android:configChanges="keyboardHidden|orientation|screenSize|locale"
Or by saving the instance of the id you received by doing such:
#Override protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("myId",myId);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
if (savedInstanceState == null) {
myId = getIntent().getExtras().getString("myId");
}
}
I am creating an image sharing app which uses a button to share images. I'm getting an unable to resolve the required method error in the code below:
public class ShareActivity extends Activity implements View.OnClickListener {
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
}
private void button1Click()
{
BitmapDrawable bm = (BitmapDrawable) getDrawable(button1);
Bitmap mysharebmp = bm.getBitmap();
String path = Images.Media.insertImage(getContentResolver(),
mysharebmp, "MyImage", null);
On this line:
BitmapDrawable bm = (BitmapDrawable) getDrawable(button1);ShareActivity
I am getting The method getDrawable(Button) is undefined for the type ShareActivity. Why might I be getting this error? The full code is here.
It's extremely unclear as to what you want to do here, and I suspect you're going to encounter a number of other problems with this code. But the reason that you're getting the error "getDrawable is undefined for ShareActivity" is because the Activity class does not have a getDrawable method.
Instead of calling getDrawable, you need to get the app resources and then retrieve the drawable. What you're looking for is:
BitmapDrawable bm = (BitmapDrawable) getResources().getDrawable(R.drawable.my_bitmap);
Where you have some image called "my_bitmap.png" in /res/drawable/.
Edit Feb 7 2016: This answer is no longer correct. Context#getDrawable(int) was added in API 21 (Lollipop) which was released in November 2014.
Note that you probably shouldn't be using it anyway.
If you are trying to get the image displayed inside the button the consider using the function defined inside the image button object.
I mean to say use button1.getDrawable()
The getDrawable() method requires id of the resource and in this case you are passing Button. Try passing R.id.button1
Here is the documentation
I am using Intents to switch between activities in my Android app. I am putting data in the Intent for use in the next activity. When I switch the phone between landscape and portrait modes, the values passed from the intent are lost and I get a NullPointerException.
Can someone please tell me what could be wrong.
There's a lot of code to post it entirely. But if someone needs to look at specific parts of code, I can post it here.
Edit
I solved the issue of state not being saved. But another problem I faced is that none of the buttons on the screen work after the orientation has been changed. On button press, I get this warning in LogCat
02-25 23:07:49.190: WARN/WindowManager(58): No window to dispatch pointer action 0
Please help.
When you switch orientation the activity is recreated and onCreate is recalled so you have to use the bundle to save your current state and restore after an orientation change. You can see this in action if you have just an app with a TextView and you enter text and change orientation. If you bundle your state for onCreate you can curb this. This is probably also why you have a NullPointer after the orientation changes. It is annoying as all hell but something we have to live with.
This link on a series of orientation tutorials and this first one in particular should help you understand exactly what is going on and how to successfully maintain your current state.
Update: There is also a post on SO Activity restart on rotation Android that deals with almost the same thing.
Edit for follow up question:
Did you re-attach your click handlers after the orientation change?
Write this in your manifest file..in which activity you want this--
android:configChanges="orientation|keyboardHidden"
Edited: Use this one for new APIs versions--
android:configChanges="orientation|keyboardHidden|screenSize"
Definitely it will work..
Try this:
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(SOME_KEY, "blah blah blah");
}
#Override
public void onCreate(Bundle savedInstanceState) {
...
somevalue = savedInstanceState.getString(SOME_KEY);
...
}
It possible to declare an attribute android:configChanges with the value of "orientation", this will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.
Declare < android:configChanges="orientation|keyboardHidden"/> in your manifest. This allows you manage the change of Orientation/Keyboard visibility by yourself. Of course, You don't need to override the callback method for manage it.
Hi I also encountered this problem.
what fixed it for me was:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putString("Username", mUsername);
savedInstanceState.putString("Password", mPassword);
savedInstanceState.putString("UserID", mUserID);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
and then in onCreate():
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
mUsername = "?";
mPassword = "?";
mUserID = "?";
} else {
mUsername = extras.getString("Username");
mPassword = extras.getString("Password");
mUserID = extras.getString("UserID");
}
} else {
mUsername = (String) savedInstanceState.getSerializable("Username");
mPassword = (String) savedInstanceState.getSerializable("Password");
mUserID = (String) savedInstanceState.getSerializable("UserID");
}
then you can be sure the objects are not null.