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();
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 am trying to implement shared element transition on 2 SimpleDraweeView.
Now everything works well except that the image blinks when I navigate from first activity to the next one.
As we know, in order to avoid such phenomenon, we need to call postponeEnterTransition() and call startPostponedEnterTransition() when the image is ready.
However I cannot figure out where should I call this if I am using Fresco.
Is there a simple callback that I can use for SimpleDraweeView's image ready?
Edited on 2018-01-29
Thanks to #Alexander Oprisnik 's answer, I have now gone a bit further. However, after setting everything, I am still able to see the placeholder image during the transition.
Below is my related code:
First, I called postponeEnterTransition() in Activity onCreate.
And then after the Image URI has been loaded from the internet, the below method is called:
public static void addSharedElementControllerToDrawee (String uri, Activity activity, SimpleDraweeView view) {
try {
DraweeController controller = Fresco.newDraweeControllerBuilder()
.setImageRequest(ImageRequestBuilder.newBuilderWithSource(Uri.parse(uri)).build())
.setControllerListener(new SharedElementControllerListener(activity))
.build();
view.setController(controller);
} catch (Exception e) {
if (BuildConfig.DEBUG) e.printStackTrace();
view.setImageURI(uri);
}
}
While the code for SharedElementControllerListener is:
public class SharedElementControllerListener extends BaseControllerListener {
private Activity activity;
public SharedElementControllerListener(Activity activity) {
this.activity = activity;
}
#Override
public void onFinalImageSet(String id, #Nullable Object imageInfo, #Nullable Animatable animatable) {
if (Build.VERSION.SDK_INT >= 21 && activity != null) {
activity.startPostponedEnterTransition();
}
activity = null;
}
}
I set breakpoints on both postponeEnterTransition() and startPostponedEnterTransition(), and observed that they were both called.
So it looks like onFinalImageSet() is not really called after the image is loaded into the SimpleDraweeView.
P.S. I have also tried to remove placeholder, but it simply changes to blink between transparency and the image.
You can simply create a controller listener and wait for onFinalImageSet(...) to be triggered, see http://frescolib.org/docs/listening-to-events.html
I have a method that needs to be called from another activity, and I need to use it to set an ImageView in it's own activity. I have this method in MainActivity:
public static void setImageView(String fileName){
Log.i(TAG, fileName);
imageView = (ImageView) findViewById(R.id.imageView);
bmp = BitmapFactory.decodeFile(fileName);
imageView.setBackgroundResource(0);
imageView.setImageBitmap(bmp);
}
But I can't make a static reference to findViewById because it isn't a static method. This method is being called in a Camera Activity after the photo has been saved, I want to pass in the fileName (file URI) and set the imageView such that when the Camera Activity finishes and the user return to the MainActivity the ImageView is already set. As such, in CameraView I am trying to call this:
...code...
mCamera.takePicture(null, null, callback);
MainActivity.setImageView(fileName);
Is there a cheeky way around this? I know there are other posts on this but I can't quite work out how to apply the advice given there to my situation.
Thanks!
Maybe you should launch your CameraActivity using startActivityForResult() method, and after take the photo put the fileName as an Extra into an Intent and set it as result. Then in your MainActivity you can get the fileName back from the Intent arg of onActivityResult().
Something like:
public class MainActivity extends Activity{
public void aMethod(){
...
Intent i = new Intent(this, CameraActivity.class);
startActivityForResult(i, REQUEST_CODE);
...
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
Bundle b = data.getExtras();
String fileName = b.getString(RETURN_FILE_PARAMETER);
doSomething(fileName);
}
}
}
...
public class CameraActivity extends Activity{
private void returnFileFinishActivity(String fileName) {
Intent retIntent = new Intent();
retIntent.putExtra(RETURN_FILE_PARAMETER, fileName);
setResult(RESULT_OK, retIntent);
finish();
}
}
Regards.
A few things.
1) Since you should only be displaying one activity at a time, why not just start with startActivityForResult in the activity with the ImageView and override onActivityResult in your camera activity?
2) I'm not sure of your application, but it may be easier if you implement taking a picture by following this: http://mobile.tutsplus.com/tutorials/android/android-sdk-quick-tip-launching-the-camera/ .
3) You can expose a static reference to your main activity, in your main activity's onCreate method, do something like staticRef = this; and in your camera activity simply access it via MainActivity.staticRef... (I would not recommend this approach)
4) You can register a broadcast receiver in your Main activity that has a reference to your main activity or image view and in your camera activity you send a broadcast to it which you can set the image view
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
I am developing playing video application and taking screenshot of running video and display a screenshot in next activity, i am playing video and taking screenshot and i am not able to display screenshot in next activity please check my code and give me changes.
BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
image = (ImageView) findViewById(R.id.ImageView01);
// image.setBackgroundDrawable(bitmapDrawable);
String bitmap = image.toString();
System.out.println("Image getting++++++ : " + bitmap);
Intent intent = new Intent(VideoDemo.this, ScreenshotView.class);
intent.putExtra("BitmapImage", bitmap);
startActivity(intent);
public class ScreenshotView extends Activity
{ private String filename;
private ImageButton back;
private ImageView screenshot;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.screenshot);
screenshot =(ImageView)findViewById(R.id.screen);
back = (ImageButton)findViewById(R.id.backbutton);
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
finish();
}
});
System.gc();
Intent i = getIntent();
Bitmap bitmap = (Bitmap) i.getParcelableExtra("BitmapImage");
screenshot.setImageBitmap(bitmap);
}
}
Here your "bitmap" object is a string.
And you are passing a string object to your next activity.
That is why, you are not able to set image in you ImageView screenshot.
Can you try the below code and lemme know whether you fixed it.
Sending Object
Here is the code to send the Object from one to other class. One Important thing to send the Object is the class should implement the Serializable class.
The below Red Colored text should be same.
//MainActivity.java
Intent i = new Intent(MainActivity.this,startActivity.class);
ObjectClassName object = new ObjectClassName();
i.putExtra("THIS", Object);
Receiving Object
// startActivity.java
Intent i = getIntent();
ObjectClassName obj = (ObjectClassName) getIntent().getSerializableExtra("THIS");//
TypeCasting needed