Ver Mensaje Individual
  #1 (permalink)  
Antiguo 13/04/2015, 16:18
valenciahector
 
Fecha de Ingreso: abril-2015
Mensajes: 1
Antigüedad: 9 años
Puntos: 0
Pregunta filenotfoundexception

Hola...
en el momento me encuentro desarrollando una aplicacion de autoactualizacion de un .apk

Tengo una dificultad al descarga el archivo apk desde un servidor remoto, ya he intentado con .txt, .jpg y siguen saliendo el mismo error el cual es : java.io.filenotfoundexception

Les dejo el codigo por si me pueden ayudar y muchas gracias...

Codigo...

Manifest...
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0" >

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_ST ORAGE"/>


<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.test.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>

MAIN ACTIVITY
package com.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundExce ption;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
Context context;
Runnable listener;

private static final String Info_file = ""; //por seguridad no puedo dar el link
String lname, currentname, lUrl;
int lversion, currentversion;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().buil d();
StrictMode.setThreadPolicy(policy);
//

Context context = MainActivity.this;

TextView version= (TextView) findViewById(R.id.version);
TextView name = (TextView) findViewById(R.id.name);

TextView lastv= (TextView) findViewById(R.id.lastversion);
TextView lastn = (TextView) findViewById(R.id.lastname);
TextView lurl = (TextView) findViewById(R.id.Url);

TextView uDate=(TextView)findViewById(R.id.Actualizar);

PackageInfo pInfo;
try {
pInfo = context.getPackageManager().getPackageInfo(context .getPackageName(),0);
currentversion = pInfo.versionCode;
currentname = pInfo.versionName;
version.setText(String.valueOf(currentversion));
name.setText(currentname);
} catch (NameNotFoundException e) {
e.printStackTrace();
}


try {

String data = downloadHttp(Info_file);
JSONObject json = null;
json = new JSONObject(data);
lversion = json.getInt("versionCode");
lname = json.getString("versionName");
lUrl = json.getString("downloadURL");
lastv.setText(String.valueOf(lversion));
lastn.setText(lname);
lurl.setText(lUrl);

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

uDate.setText(compararVersion(currentversion, lversion) ? "true" : "false");

if(compararVersion(currentversion, lversion))
{
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
downloadFile();
}
}).start();
}

}

private static String downloadHttp(String url) {
try {
URL uri= new URL(url);
HttpURLConnection c = (HttpURLConnection) uri.openConnection();
c.setRequestMethod("GET");
c.setReadTimeout(15 * 1000);
c.setUseCaches(false);
c.connect();

BufferedReader reader = new BufferedReader(new InputStreamReader(c.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null){
stringBuilder.append(line + "\n");
}
return stringBuilder.toString();
}
catch (IOException e) {
// TODO Auto-generated catch block
Toast.makeText(null, e.getMessage(), Toast.LENGTH_LONG).show();
return e.getMessage();
}
}

private Boolean compararVersion(int cVersion, int lVersion){
return lVersion>cVersion;
}

private void downloadFile(){
try {
URL uri= new URL(""); //por seguridad no puedo dar el link
HttpURLConnection c= (HttpURLConnection) uri.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();

File SDCardRoot = Environment.getExternalStorageDirectory();
File file = new File(SDCardRoot, "version.txt");
FileOutputStream fos= new FileOutputStream(file);

InputStream iS= c.getInputStream(); // Aqui me genera el error que indica que no se encuentra el archivo.

byte[] buffer = new byte[1024];
int bufferLength = 0;

while ((bufferLength = iS.read())>0) {
fos.write(buffer, 0, bufferLength);
}
fos.close();
iS.close();

} catch (Exception e) {
// TODO: handle exception
showError("Error : Please check your internet connection " + e);
e.printStackTrace();
}



}
void showError(final String err){
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, err, Toast.LENGTH_LONG).show();
}
});
}
}

ACTIVITY MAIN
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:id="@+id/version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/lastversion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/lastname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/Url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/Actualizar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

Agradezco su ayuda y sugerencias...