Im a bit new to Java and Android. Im developing a program to android that one of the needed features is to be able to take a picture with the correct name.
my main class is AmplaCoordenadasActivity
so i made the following code inside it:
public void tirarFoto(String arquivo)
{
Intent camera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Uri uriSavedImage=Uri.fromFile(new File(arquivo));
camera.putExtra("output", uriSavedImage);
startActivityForResult(camera, 1);
}
and it works fine.
But when i try to put it in another class and call it , it throws many exceptions.
The class:
package ampla.giesta.coordenadas;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
public class Photo extends Activity{
public void tirarFoto(String arquivo){
Intent camera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Uri uriSavedImage=Uri.fromFile(new File(arquivo));
camera.putExtra("output", uriSavedImage);
startActivityForResult(camera, 1);
}
}
And the I write in the main class
public void Camera(){
Photo camera = new Photo();
camera.tirarFoto("/sdcard/x.jpg");
}
Can anyone help?
You need to include the new activity in your android manifest.
Read up on it here
http://developer.android.com/guide/topics/manifest/manifest-intro.html
That's a good intro to the permissions you need to set in the android manifest. The manifest controls what other app's functions your app can call and respond to, you have to declare services and recievers in your manifest, any special permissions your app needs need to be declared in your manifest, and finally, any additional activities besides your main app activity also need to be declared here.
You also manage intent filters for your activities in the manifest.
try adding
<activity android:name=".Photo" android:label="#string/app_name" />
to your manifest.xml file
Related
I'm basically attempting to accomplish this, using the first answer provided. The question has answers around the web and SO, but I'm having trouble getting it to work. Is there something I need to enable, add to my manifest, ect.? I want to react to a beacon coming within range of the phone. I'm working in Android Studio, targeting Android 4.3 and working with the Android Beacon Library. Based on their documentation, all I need is to implement BootstrapNotifier, set a Region, and then anytime it scans a beacon, it will automatically call didEnterRegion. My Region is Region region = new Region("all-beacons-region", null, null, null);.
I have also built a very simple app which scans for and finds beacons in the foreground. So no problems there, I am definitely able to pick up my beacons and pull basic info from them.
My main activity looks like this:
package com.example.justin.backgroundscantest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
And my class looks like this:
import android.app.Application;
import android.content.Intent;
import android.util.Log;
import com.example.justin.backgroundscantest.MainActivity;
import org.altbeacon.beacon.startup.BootstrapNotifier;
import org.altbeacon.beacon.startup.RegionBootstrap;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.Region;
public class TestApp extends Application implements BootstrapNotifier {
private static final String TAG = ".TestApp";
private RegionBootstrap regionBootstrap;
#Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "App started up");
BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19"));
// wake up the app when any beacon is seen (you can specify specific id filers in the parameters below)
Region region = new Region("com.example.myapp.boostrapRegion", null, null, null);
regionBootstrap = new RegionBootstrap(this, region);
}
#Override
public void didEnterRegion(Region arg0) {
Log.d(TAG, "Got a didEnterRegion call");
// This call to disable will make it so the activity below only gets launched the first time a beacon is seen (until the next time the app is launched)
// if you want the Activity to launch every single time beacons come into view, remove this call.
regionBootstrap.disable();
Intent intent = new Intent(this, MainActivity.class);
// IMPORTANT: in the AndroidManifest.xml definition of this activity, you must set android:launchMode="singleInstance" or you will get two instances
// created when a user launches the activity manually and it gets launched from here.
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
}
}
(Edit)
Finally, AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.justin.backgroundscantest">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
It all feels pretty straightforward, which is I'm curious if I'm missing something simple or silly. I've found this example in several places around the web, but no mention of anything that I might have missed. I have a pretty solid coding background but am new to Android and very new to Eddystone/BLE technology. And just to clarify the actual problem: there is no reaction from my app when I move the phone within range of the beacon. My expectation is that it will "wake up" and begin MainActivity. I am definitely in range of the beacon, the beacon is definitely on, and again, I am able to scan it in the foreground. It just won't wake up my app. Thanks!
When creating a custom Android Application class like TestApp, you must declare it in your manifest with a name attribute. Like this:
<application
android:name="TestApp"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
If you don't do that, Android won't use your custom TestApp class, and will instead default to its built-in base Application class, causing none of the TestApp code to get executed.
I updated several of my files but they do not appear as id #s in R.java. I tried updating my imports, I tried closing then opening Eclipse, saving, and closing/opening the Emulator, but it is not working to get R.Java to recognize the updates in the files. Of the items that are missing from R.java are 2 buttons (with 2 services) which would activate a toast when pressed, I will list the files below, each one. I run the emulator (Virtual, Nexus API 17) and it shows an old version of my app that has no buttons, only text that says "Hello World". Thanks in advance.
UPDATE: I tried to clean my project (thinking that if the R.java got regenerated, then it might update itself), but it just made the R.java disappear, and created an error in the main folder, HelloWorld in the Package Explorer. Oddly enough, there were no other errors in the entire series of files that I edited. No yellow warnings, nothing. Plus I never edited the R.java file (I know it's a no-no). So then after I tried to run my project anyway, this error posted in the LogCat below.
I found this in the LogCat:
01-03 06:54:13.977: E/ThrottleService(300): problem during
onPollAlarm: java.lang.IllegalStateException: problem parsing stats:
java.io.FileNotFoundException: /proc/net/xt_qtaguid/iface_stat_all:
open failed: ENOENT (No such file or directory)
Here are my files:
MainActivity.java
package com.example.helloworld;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
// Method to start the service
public void startService(View view) {
startService(new Intent(getBaseContext(), MyService.class));
}
// Method to stop the service
public void stopService(View view) {
stopService(new Intent(getBaseContext(), MyService.class));
}
}
MyService.java
package com.example.helloworld;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Let it continue running until it is stopped.
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button android:id="#+id/btnStartService"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/start_service"
android:onClick="startService"/>
<Button android:id="#+id/btnStopService"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/stop_service"
android:onClick="stopService" />
</LinearLayout>
strings.xml
<resources>
<string name="app_name">HelloWorld</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="start_service">Start Service</string>
<string name="stop_service">Stop Service</string>
</resources>
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service android:name=".MyService" />
</application>
</manifest>
and finally, the R.java (which ignores many strings and other names)
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package com.example.helloworld;
public final class R {
public static final class attr {
}
public static final class dimen {
/** Default screen margins, per the Android Design guidelines.
Customize dimensions originally defined in res/values/dimens.xml (such as
screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here.
*/
public static final int activity_horizontal_margin=0x7f040000;
public static final int activity_vertical_margin=0x7f040001;
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int action_settings=0x7f080000;
}
public static final class layout {
public static final int activity_main=0x7f030000;
}
public static final class menu {
public static final int main=0x7f070000;
}
public static final class string {
public static final int action_settings=0x7f050001;
public static final int app_name=0x7f050000;
public static final int hello_world=0x7f050002;
}
public static final class style {
/**
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
API 11 theme customizations can go here.
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
API 14 theme customizations can go here.
*/
public static final int AppBaseTheme=0x7f060000;
/** Application theme.
All customizations that are NOT specific to a particular API-level can go here.
*/
public static final int AppTheme=0x7f060001;
}
}
You code is perfectly fine.I think you might have damaged R.java and put something by your self.I have compiled your code it works fine.You can even download from this link.
After great help from everyone, the final solution was to begin a new project, then copy/paste each important file back into it, including updating the AndroidManifest.xml file. However, the order of operations was very important. The rebuild did not work if I copy and pasted the MainActivity.java file first, rebuilding that way gave me that odd HelloWorld folder error with no other errors in all my other files. So I thought maybe rebuilding in a certain order would work, and it did.
I had to do the rebuild in this order:
strings.xml
activity_main.xml
MyService.java
update the AndroidManifest.xml (with the addition of the MyService.java in <service> tags)
then finally the MainActivity.java
I then updated all imports and saved everything, and low and behold, R.java was updated with the right ids for all of my strings and other name ids.
So while I still don't understand why I had a red error on my HelloWorld project folder in Package Explorer (that did not show a single error or warning anywhere else in any of my edited files), it looks like rebuilding the file by cutting and pasting in a new project, in a careful order, ended up updating the R.java so that I could run successfully in the emulator.
Special thanks to #TalhaQ for suggesting my files were okay and to reuse the code. Although I know I did not edit the R.java file at all, it's still a mystery why it never updated like it should have. Might have had something to do with the order of operations in the original file, since the order of operations mattered a lot in the rebuild.
As you have stated in your question it shows an old version of my app that has no buttons, only text that says "Hello World".
Just try to uninstall the old application from your emulator and try to run the project.
Go to Settings>ApplicationManager> select your application> Uninstall
tl;dr Look at title + netbeans
I've been writing a game as I have been learning both Java and Android*. Most of it has gone without a hitch- but here we go.
To my understanding- each screen (main menu, gameplay, highscores, ect) is a separate Activity and therefore needs a different layout.
Problem 1:
EDIT: Solved. Thanks Daniel.
Problem 2: Then in the GameActivity.java file there is an error of cannot find symbol (symbol main_1 location class layout). Help?
package lolfighter.notriot;
import android.app.Activity;
import android.os.Bundle;
import lolfighter.notriot.nojoke.R.*;
// #author DEVELOPMENT
public class GameActivity extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(layout.main_1);
}
Also why is the bundle icicle? I have not specified this anywhere.
Edit- Not solved
*- I am in a partnership of sorts with a friend- the original plan was that he would handle all the code and I would handle all the graphic resources (He took an actual class for the Java language) But he has ran into a roadblock. I can't ask him because he has limited internet access (lunch breaks only) and lives in a different state (we go to college together)
Problem 2: then in the GameActivity.java file there is an error of cannot find symbol (symbol main_1 location class layout). Help?
Change layout.main_1 to R.layout.main_1.
Also why is the bundle icicle? I have not specified this anywhere.
You can rename the variable to be whatever you want, it doesn't matter.
I have just opened my code from a few weeks off and now it doesn't seem to work. I have a splash screen on open, which then moves to the home screen. The splash screen works, but when trying to move to the home page it falls over saying No Class Def Found. What does this mean and how can i fix it?
It says the problem is found with the below code which is in the splash screen class:
public void run()
{
//Finish the splash activity so it can't be returned to.
SplashScreen.this.finish();
// Create an Intent that will start the main activity.
Intent mainIntent = new Intent(SplashScreen.this, HomeScreen.class);
SplashScreen.this.startActivity(mainIntent);
}
please place HomeScreen in manifest file
<activity android:name=".HomeScreen"
android:label="#string/app_name">
</activity>
So what happened was I updated the Android SDK and apparently there have been a few problems with this, one being changing lib to libs. What I did was removed the activity and re-added. Thanks arcastro.
So if anyone has this problem from 16->17, check the activity's in the manifest file, and the lib folder
I am finding great difficulties to view YouTube videos in my app.
here is my code:
package com.example.webvideo;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
public class WebVideo extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView vv = (VideoView) findViewById(R.id.videoView);
MediaController mc = new MediaController(this);
mc.setAnchorView(vv);
vv.setMediaController(mc);
vv.setVideoURI(Uri.parse("http://youtu.be/2OIOOb-0t44"));
vv.start();
}
}
The emulator is showing an error that the video cannot be played.
What am I doing wrong? Am I giving the URL in a wrong format?
I would expect that the URI you need to give is to the actual media file to be played. I wouldn't count on redirects working either... and anyway that redirect you give seems to point to a YouTube web page, which I sure wouldn't expect the video player to be able to render.
http://youtu.be/2OIOOb-0t44 is certainly an invalid URL. Maybe you meant http://youtube.com/watch?v=2OIOOb-0t44?
The URL you are using is for the webpage where you can view it, not for the video itself. The embedding URL appears to be http://www.youtube.com/embed/2OIOOb-0t44, but I think it's HTML5, not flash... you may have to go old school and track down an AVI or MPEG file.