Adria Ciurana
Adria Ciurana

Reputation: 934

How to save and load android image

The scenario is as follows:

If the image file is found at local path then it will be used, Otherwise the image file will be downloaded first. The image is marked by its id and name.

Thanks in advance, and sorry my sentences as short as it is very little English.

FileOutputStream fos;
FileInputStream fis; 
        try {
            fis = openFileInput(Integer.toString(id)+"_"+getName());
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        // exist
        if(fis!=null){
            this.setImage(BitmapFactory.decodeStream(fis));
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        // not exist
        }else{
            this.setImage(getBITMAP.BitmapUrl(image));
            try {
                fos = openFileOutput(Integer.toString(id)+"_"+getName(), Context.MODE_PRIVATE);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            getImage().compress(Bitmap.CompressFormat.PNG, 100, fos);
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

PD:I forgot to say that the error arises application crash: The application ****** has stopped unexpectedly. Please try again.

This is the LogCat:

09-06 09:03:14.253: D/dalvikvm(472): GC freed 404 objects / 23688 bytes in 54ms
09-06 09:03:15.214: I/global(472): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
09-06 09:03:15.233: D/AndroidRuntime(472): Shutting down VM
09-06 09:03:15.233: W/dalvikvm(472): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
09-06 09:03:15.233: E/AndroidRuntime(472): Uncaught handler: thread main exiting due to uncaught exception
09-06 09:03:15.243: E/AndroidRuntime(472): java.lang.RuntimeException: Unable to start activity ComponentInfo{match.now.play/match.now.play.MatchNowGames}: java.lang.NullPointerException
09-06 09:03:15.243: E/AndroidRuntime(472):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
09-06 09:03:15.243: E/AndroidRuntime(472):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
09-06 09:03:15.243: E/AndroidRuntime(472):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
09-06 09:03:15.243: E/AndroidRuntime(472):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
09-06 09:03:15.243: E/AndroidRuntime(472):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 09:03:15.243: E/AndroidRuntime(472):  at android.os.Looper.loop(Looper.java:123)
09-06 09:03:15.243: E/AndroidRuntime(472):  at android.app.ActivityThread.main(ActivityThread.java:4363)
09-06 09:03:15.243: E/AndroidRuntime(472):  at java.lang.reflect.Method.invokeNative(Native Method)
09-06 09:03:15.243: E/AndroidRuntime(472):  at java.lang.reflect.Method.invoke(Method.java:521)
09-06 09:03:15.243: E/AndroidRuntime(472):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
09-06 09:03:15.243: E/AndroidRuntime(472):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-06 09:03:15.243: E/AndroidRuntime(472):  at dalvik.system.NativeStart.main(Native Method)
09-06 09:03:15.243: E/AndroidRuntime(472): Caused by: java.lang.NullPointerException
09-06 09:03:15.243: E/AndroidRuntime(472):  at android.content.ContextWrapper.openFileInput(ContextWrapper.java:152)
09-06 09:03:15.243: E/AndroidRuntime(472):  at match.now.play.functions.Game.<init>(Game.java:32)
09-06 09:03:15.243: E/AndroidRuntime(472):  at match.now.play.functions.Games.addGames(Games.java:43)
09-06 09:03:15.243: E/AndroidRuntime(472):  at match.now.play.MatchNowGames.onCreate(MatchNowGames.java:52)
09-06 09:03:15.243: E/AndroidRuntime(472):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-06 09:03:15.243: E/AndroidRuntime(472):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
09-06 09:03:15.243: E/AndroidRuntime(472):  ... 11 more
09-06 09:03:15.263: I/dalvikvm(472): threadid=7: reacting to signal 3
09-06 09:03:15.273: I/dalvikvm(472): Wrote stack trace to '/data/anr/traces.txt'
09-06 09:03:17.324: I/Process(472): Sending signal. PID: 472 SIG: 9
09-06 09:03:17.873: D/dalvikvm(478): GC freed 631 objects / 50272 bytes in 64ms
09-06 09:03:18.053: D/dalvikvm(478): GC freed 65 objects / 2544 bytes in 53ms
09-06 09:03:18.183: D/dalvikvm(478): GC freed 131 objects / 6072 bytes in 57ms

The android manifest:

    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" /> 
    <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:name=".functions.App" >
        <activity
            android:theme="@android:style/Theme.NoTitleBar"
            android:name=".MatchNow"
            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="MatchNowRegister" android:theme="@android:style/Theme.NoTitleBar">
        <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter></activity>
        <activity android:name="MatchNowTerms" android:theme="@android:style/Theme.NoTitleBar">
        <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="MatchNowMain" android:theme="@android:style/Theme.NoTitleBar">
        <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="MatchNowGames" android:theme="@android:style/Theme.NoTitleBar">
        <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

I think that the android manifest isn't wrong, because the activity is added. I think the problem is in the openFile...

Solved the problem by using another similar code. In case anyone is useful also:

public static Bitmap saveImage(String url, String fichero){
    Bitmap imageAux = null;
    OutputStream outStream = null;
    extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    File file = new File(extStorageDirectory, fichero);
    try {           
        // Exist
        if(file.exists()){
            imageAux=BitmapFactory.decodeFile("/sdcard/"+fichero);
        // No exist
        }else{
            outStream = new FileOutputStream(file);
            imageAux=getBITMAP.BitmapUrl(url);
            imageAux.compress(Bitmap.CompressFormat.PNG, 100, outStream);
            outStream.flush();
            outStream.close();
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return imageAux;
}

Upvotes: 0

Views: 259

Answers (1)

Remi Peuvergne
Remi Peuvergne

Reputation: 279

You have a null pointer somewhere in your call to openFileInput(). You should check what getName() returns (my bet), as well as id.

Upvotes: 1

Related Questions