Android raw resource file not readable - android

I have got several mp3 files added as raw resource to my project. I would like to play them with AudioPlayer. I had FileNotfoundException and I backtraced it to find that the files can be found but they are not readable:
mp1.stop();
mp1.reset();
playedSound = R.raw.sip02;
String str = getResources().getString(playedSound);
File file = new File(str);
//((File)file).setReadable(true);
boolean readable = file.canRead(); //returns TRUE
file.setReadOnly();
readable = file.canRead(); //returns TRUE
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace(); //enters this branch
}
I cannot call file.setReadable() because I use API level 7.
My manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.movingcircle"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.movingcircle.MovingCircle"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Try
MediaPlayer mPlayer = MediaPlayer.create(PlayWorld.this, R.raw.your_mp3);

Related

groovy get attribute from AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.emnets.luoly.sample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="24"
android:targetSdkVersion="19" />
<uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name= "android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name="com.emnets.luoly.sample.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I would like to get uses-permission using groovy.
Maybe like this,
def manifestXml = new XmlParser().parse(Manifest)
manifestXml."uses-permission".each { p ->
println p.attribute("android:name")
}
How to get attribute like android.permission.WRITE_EXTERNAL_STORAGE
You can use XmlSlurper
def manifestXml = new XmlSlurper().parse(Manifest)
manifestXml."uses-permission".each { p ->
println p."#android:name"
}
Use XmlSlurper get ActivityName
def manifest = new XmlSlurper().parse(xmlFilePath)
String pkg = manifest.#package
manifest.application.activity.each {
String actName = it.'#android:name'
if (actName.substring(0, 1).equals('.')) {
actName = pkg + actName
}
println(actName)
}

how to resolve host exception?

please take me out this problem ,I am trying to get file from Ftp server .
here is my code which i am continuously getting exception like
java.net.unknownHostException :ftpbrasnet.no-ip.org.
i have tried in browser its working.
FTPClient client = new FTPClient();
FileOutputStream Foutput = null;
try {
client.connect("ftp://ftpbrasnet.no-ip.org/"); // My Exceptionis here
client.login("win7", "123");
// Create an OutputStream for the file
String filename = "files.txt";
Foutput = new FileOutputStream(filename);
// Fetch file from server
client.retrieveFile("/" + filename, Foutput);
}
catch (IOException e)
{
Log.e("ERROR", e.getStackTrace().toString());
}
finally
{
try
{
if (Foutput != null) {
Foutput.close();
}
client.disconnect();
} catch (IOException e) {
Log.e("ERROR", e.getStackTrace().toString());
}
}
}
and here is my manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="Net.estoque"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application android:label="#string/app_name" >
<activity android:name="Netestoque"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
Have you tried removing the protocol from the URL, like so:
client.connect("ftpbrasnet.no-ip.org/");

Translate in Android using Google API

I want to create a translate app in Android,i use Google Translate API,but it has error,please help me for fix it. This is my code
public void onClick(View v) {
// TODO Auto-generated method stub
String InputString;
String OutputString = null;
InputString = InputText.getText().toString();
try {
Translate.setHttpReferrer("http://translate.google.com.vn/");
OutputString = Translate.DEFAULT.execute(InputString, Language.ENGLISH, Language.VIETNAMESE); ////////
} catch (Exception ex) {
ex.printStackTrace();
OutputString = "Error";
}
OutputText.setText(OutputString);
}
Here is my Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.stthing"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<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>
</application>
</manifest>
Error in line Translate.setHttpReferrer("http://translate.google.com.vn/");
The method setHttpReferrer(String) is undefined for the type Translate
I am not sure if you are aware but Translate API is no longer free. so you would need to set the API key as well..
instead of using
Translate.setHttpReferrer("http://translate.google.com.vn/");
use
GoogleAPI.setHttpReferrer("http://translate.google.com.vn/");
GoogleAPI.setKey(/* Enter your API key here */);

permissions denied when saving image on sdcard

This function is used to save image on sdcard (I have tested it on the emulator only):
public String SaveImage(String URL,String imagename){
Bitmap bitmap = null;
InputStream in = null;
String path = Environment.getExternalStorageDirectory().toString();
File dir = new File(path + "/bh");
if(!dir.exists())
new File(path + "/bh").mkdir();
Log.i("in save()", "after file");
File mImageFile = new File(path+"/bh/"+imagename);
if(mImageFile.exists())
Toast.makeText(mContext, "Saved", Toast.LENGTH_LONG).show();
try{
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
FileOutputStream out = new FileOutputStream(mImageFile);
bitmap.compress(CompressFormat.JPEG, 100,out);
out.flush();
out.close();
in.close();
return imagename+".jpg";
}catch(IOException ex){
Log.e("==== Error in saving image ====",ex.getMessage());
return "";
}
}
I have added the required permissions like below :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mobile.bh"
android:versionCode="4"
android:versionName="1.0.3" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
<activity
android:name="mobile.bh.activities.BHActivity"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="mobile.bh.activities.RecipesListActivity"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="mobile.bh.activities.RecipeInfoActivity"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="mobile.bh.activities.IngredientsActivity"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="mobile.bh.activities.MethodActivity"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="mobile.bh.activities.SpicesListActivity"
android:screenOrientation="portrait" >
</activity>
<activity android:name=".activities.SpicesCategoriesActivity" >
</activity>
<activity android:name=".activities.CategoryActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You also need to use the Internet permission since you're connecting to the internet.
<uses-permission android:name="android.permission.INTERNET" />
Does your emulator support SDCard ?
Have a look at this great answer, it might be the issue https://stackoverflow.com/a/11468278/1635817

IntentService is not being called

I am trying to send an Intent from the onCreate in an Activity to start an IntentService. However the IntentService's onHandleIntent is never being received. I have tried changing around the Manifest with Intent-filters but nothing seems to be working. No exceptions are being thrown, but the IntentService is simply not called.
Here is the onCreate
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = new TwitterDB(this);
String[] columns = new String[1];
columns[0] = TwitterDB.TEXT;
tAdapter = new SimpleCursorAdapter(this, 0, null, columns, null, 0);
tweets = (ListView)findViewById(R.id.listtwitter);
tweets.setAdapter(tAdapter);
Intent intent = new Intent(this, SyncService.class);
intent.putExtra("action", SyncService.TWITTER_SYNC);
this.startService(intent);
}
Here is the creator and onHandleIntent of the IntentService class, I know it is not being called because logcat never shows "Retrieved intent". The constructor is not called either (There was a log call in there, but I removed it.
public SyncService(){
super("SyncService");
twitterURI = Uri.parse(TWITTER_URI);
Log.i("SyncService", "Constructed");
}
#Override
public void onHandleIntent(Intent i){
int action = i.getIntExtra("action", -1);
Log.i("SyncService", "Retrieved intent");
switch (action){
case TWITTER_SYNC:
try{
url = new URL(twitterString);
conn = (HttpURLConnection)url.openConnection();
syncTwitterDB();
conn.disconnect();
}catch(MalformedURLException e){
Log.w("SyncService", "Twitter URL is no longer valid.");
e.printStackTrace();
}catch(IOException e){
Log.w("SyncService", "Twitter connection could not be established.");
e.printStackTrace();
}
break;
default:;
// invalid request
}
}
And here is the Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.twitter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TwitterTwoActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<service android:name="SyncService"/>
</activity>
</application>
</manifest>
Move your service declaration outside of the scope of the TwitterTwoActivity in the XML file, as I have done here:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.twitter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TwitterTwoActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="SyncService"/>
</application>
</manifest>
You need to define the path to the service in your manifest, simply putting the name is not sufficient.
Change
<service android:name="SyncService"/>
to
<service android:name=".SyncService"/>
if SyncService is in the root of your package, add respective folder before .SyncService if needed.

Categories

Resources