AsyncTask Permission denied (missing INTERNET permission?) - android

I am trying use AsyncTask to extract an image over the internet and display on the RecyclerViewer but I am getting the following error message, which's captured from my Exception in my doInBackground from my RecyclerViewAdapter class:
08-02 00:11:25.608: E/ImageDownload(5753): Download failed: Permission denied (missing INTERNET permission?)
See full version of AsyncTask sub-class below:
class GetImageFromNet extends AsyncTask<String, Void, Bitmap> {
private RVAdapter.PersonViewHolder personViewHolder;
private String url = "http://ia.media-imdb.com/images/M/MV5BNDMyODU3ODk3Ml5BMl5BanBnXkFtZTgwNDc1ODkwNjE#._V1_SX300.jpg";
public GetImageFromNet(RVAdapter.PersonViewHolder personViewHolder){
this.personViewHolder = personViewHolder;
}
protected Bitmap doInBackground(String... params) {
try {
InputStream in = new java.net.URL(url).openStream();
Bitmap bitmap = BitmapFactory.decodeStream(in);
return bitmap;
//NOTE: it is not thread-safe to set the ImageView from inside this method. It must be done in onPostExecute()
} catch (Exception e) {
Log.e("ImageDownload", "Download failed: " + e.getMessage());
}
return null;
}
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
}
personViewHolder.personPhoto.setImageBitmap(bitmap);
}
I have no idea why I am getting above error since I have already added the following permission access in my AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxx" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<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>
</application>
</manifest>
Please help!

Move your <uses-permission> elements to be immediate children of <manifest>, above your <application> element.

Update your manifest as:-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxx" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/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>
</application>

Related

Volley jsonArray Request not working

I am making jsonArray request using volley but onError() gets executed all the time though I have added dependency, internet permission.
Here is my request code
RequestQueue requestQueue = Volley.newRequestQueue(this);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, "http://api.aladhan.com/v1/timingsByCity?city=Dubai&country=United%20Arab%20Emirates", null, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
try {
JSONObject obj = (JSONObject) response.get(0);
Toast.makeText(City_Timing.this, obj.getString("Fajr"), Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(City_Timing.this, "Error", Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(jsonArrayRequest);
My AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="anandroid.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
</activity>
<activity android:name=".Search_CIty"
android:label="SEARCH CITY"
android:parentActivityName=".MainActivity"/>
<activity android:name=".City_Timing"
android:label="TIMING"
android:parentActivityName=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".History"
android:label="HISTORY"
android:parentActivityName=".MainActivity"/>
<activity android:name=".AboutUS"
android:label="ABOUT US"
android:parentActivityName=".MainActivity"/>
</application>
Only toast of onError is displaying. I have tried to change the request type to string, JsonObject request but nothing working, only on error is executed all the time.
You have got a typo error in internet permission, change
<uses-permission android:name="anandroid.permission.INTERNET"/>
to
<uses-permission android:name="android.permission.INTERNET" />
it should be android not anandroid
please check your menifest:
there is a spelling mistake in anandroid
<uses-permission android:name="anandroid.permission.INTERNET"/>
change to
<uses-permission android:name="android.permission.INTERNET"/>
it works fine try this and let me know
Just change this,
<uses-permission android:name="anandroid.permission.INTERNET"/>
To,
<uses-permission android:name="android.permission.INTERNET" />
You just got a typo error, nothing else.
Try This
<uses-permission android:name="anandroid.permission.INTERNET"/>
instead of this
<uses-permission android:name="android.permission.INTERNET" />

Run Android app in different devices

I run my application in two different Android devices. The first is an Arnova 9 G2 Tablet and the second a Sony Xperia P. In the Arnova tablet my application executed successfully, but in the Sony Xperia P phone, the LogCat displays me the following error:
/data/data/com.example.androidjnetpcaptest/interfaces: open failed:
EACCES (Permission denied) FATAL EXCEPTION: main
java.lang.NullPointerException at
com.example.androidjnetpcaptest.InterfacesFragment.onCreateView(InterfacesFragment.java:35)
My manifest XML file is:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidjnetpcaptest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.pemission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".SplashScreenActivity"
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=".MainActivity"
android:label="#string/app_name" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.Default" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".InformationsActivity"
android:theme="#android:style/Theme.Holo"
android:label="#string/app_name" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.Default" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
And the InterfacesFragment class is:
public class InterfacesFragment extends Fragment
{
private Scanner inputStream = null;
private TextView interfacesTextView = null;
#Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState )
{
try
{
inputStream = new Scanner( new FileInputStream( "/data/data/com.example.androidjnetpcaptest/interfaces" ) );
}
catch ( FileNotFoundException e )
{
Log.e( "InterfacesFragment_ERROR: ", e.getMessage() );
}
String lines = "";
while ( inputStream.hasNextLine() )
{
lines = lines + inputStream.nextLine() + "\n";
}
inputStream.close();
final ProgressDialog ringProgressDialog = ProgressDialog.show( getActivity(), "Please wait ...", "Finding interfaces ...", true );
ringProgressDialog.setCancelable( true );
new Thread(new Runnable()
{
#Override
public void run()
{
try
{
Thread.sleep( 4000 );
}
catch ( Exception e )
{
}
ringProgressDialog.dismiss();
}
}).start();
View rootView = inflater.inflate(R.layout.interfaces, container, false);
interfacesTextView = ( TextView ) rootView.findViewById( R.id.interfacesTextView );
interfacesTextView.setText( lines );
interfacesTextView.setMovementMethod( new ScrollingMovementMethod() );
return rootView;
}
}
The trouble is the use of the absolute path /data/data/com.example. androidjnetpcaptest/interfaces. You should not assume this is the data location for the app. Apps can be moved to external storage and since ICS the system supports multiple users (people) where the app gets different storage per user. Instead, use Context.getFilesDir() to get the app-private location where files can be created and stored.
You do not need any special permissions to read/write this space.

How to write text file via emulator?

Yesterday i post this question and some one replied me by peace of codes but the problem is permission denied. i do not know why happen this error because i have allowed permission in my manifest.xml. below is peace of code.
This is java code
public void WriteText() {
EditText txt= (EditText) findViewById(R.id.txtwrite);
try {
BufferedWriter fos = new BufferedWriter(new FileWriter(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+"File.txt"));
fos.write(txt.getText().toString().trim());
fos.close();
Toast.makeText(this, "Saved", Toast.LENGTH_LONG);
} catch (Exception e) {
Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG);
}
}
This is manifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/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>
</application>
Please any one help me i was so Straggled about it.

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