java - 如何保存和加载Android图像

标签 java android fileinputstream fileoutputstream

场景如下:

如果在本地路径找到图像文件,则将使用它, 否则将首先下载图像文件。 该图像由其 id 和名称标记。

预先感谢,抱歉我的句子太短,因为英语很少。

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:我忘了说错误是由应用程序崩溃引起的:应用程序********已意外停止。请重试。

这是 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

Android list :

    <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>

我认为 android list 没有错误,因为该 Activity 已添加。我认为问题出在 openFile...

使用另一个类似的代码解决了该问题。如果有人也有用:

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;
}

最佳答案

在调用 openFileInput() 时,您的某处有一个空指针。 您应该检查 getName() 返回什么(我的赌注),以及id

关于java - 如何保存和加载Android图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12292045/

相关文章:

java - java中如何读取字符串中的字符

java - DateTimeFormatter - java.lang.IllegalArgumentException : Too many pattern letters: a

android - Firebase ANR - 甚至不使用 firebase

java - 如果重新实例化它,我是否应该多次关闭 FileInputStream

java - 文件到流 -> 流到信息 Java

java - 如何检测java中FileCopying的读取错误?

java - 如何将 Java 中的文件逐个字符读取到整数数组中?

java - Swagger 2.0 (OpenApi 3.0) 中的 BeanConfig(或类似的?)

java - 如何将 double 转换为小数点后两位?

java - 生成的 Android espresso 测试运行失败,AndroidJUnitRunner 解析失败 : Landroidx/test/platform/io/FileTestStorage;