场景如下:
如果在本地路径找到图像文件,则将使用它,否则将首先下载图像文件。图像由其 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:我忘了说错误出现应用程序崩溃:The application ****** has stopped unexpectedly. Please try again.
这是 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
安卓清单:
<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清单没有错,因为添加了活动。我认为问题出在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;
}