我正在尝试通过打开一个文件来加载来自SD卡/设备内存的图像,该文件将代表该路径的字符串作为参数:
File imagePathFile = new File(savedImapthPathwayOnPause);
try {
final int IMAGE_MAX_SIZE = 3000;
FileInputStream streamIn = new FileInputStream(imagePathFile);
// important to reduce size of image before it is loaded into memory
// and then resized, otherwise will
// get out of memory error
// Decode image size and setInJBounds = true to avoid auto memory
// allocation for large image
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(streamIn, null, o);
streamIn.close();
//by setting m.pow(scale, 3.85) we get a org image of 2500:1400 beofre loaded and when loaded we get 426:240, no need fro resize method call here
int scale = 1;
while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 3.73)) > IMAGE_MAX_SIZE) {
scale++;
}
// get orginal width of image before loaded into memory
Log.d("VIEW DIVE", "scale = " + scale + ", orig-width: " + o.outWidth
+ " orig-height: " + o.outHeight);
//Toast.makeText(getApplicationContext(), "Orginal Image size from DB: width: "+ o.outWidth + "/height: " + o.outHeight , Toast.LENGTH_LONG).show();
Bitmap b = null;
streamIn = new FileInputStream(imagePathFile);
if (scale > 1) {
scale--;
// scale to max possible inSampleSize that still yields an image
// larger than target, inSampleSize loads the image into memor
// by a factor of its integer value
o = new BitmapFactory.Options();
o.inSampleSize = scale;
// Decode bitmap with inSampleSize set
o.inJustDecodeBounds = false;
b = BitmapFactory.decodeStream(streamIn, null, o);
//makeToast("Image after sacling and now loading: width: "+ o.outWidth + "/height: " + o.outHeight );
//resizedImage = reSizeImage(b); this is blowing uo and alreadt samll image from
//org size 2500:1400 to 73:44, then blown back up in resize does not look good
streamIn.close();
//b.recycle();
System.gc();
但我一直得到以下例外:
java.io.FileNotFoundException: /: open failed: EISDIR (Is a directory)
at libcore.io.IoBridge.open(IoBridge.java:416) at
java.io.FileInputStream.<init>(FileInputStream.java:78) at
com.mooney.diveapp.LogDive.setImageViewFromSavedInstance(LogDive.java:198)
at com.mooney.diveapp.LogDive.onCreate(LogDive.java:134) at
android.app.Activity.performCreate(Activity.java:5255) at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
编辑:如何设置图像路径字符串(在asycnh内部类中完成):
// check if external storage is available to write
Bitmap imageToSave = params[0];// get image
final String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// media available
canSaveToExternalSD = true;
// use a random number to append to image file name until use
// diveNumber according to log entry on
Random diveNum = new Random();
File sdCard = Environment.getExternalStorageDirectory();
final File directory = new File(sdCard.getAbsolutePath()
+ File.separator + diveAlbum);
final File theFile = new File(directory, "image"
+ diveNum.nextInt(5000) + ".png");
directory.getParentFile().mkdirs();
if (!directory.mkdirs()) {
Log.d(TAG, "Directory not created!!!!");
}
try {
FileOutputStream out = new FileOutputStream(theFile);
imageToSave.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
//assign path to a string
savedImagePath = theFile.getAbsolutePath();
稍后在代码中,saveImagePath被分配给savedImapthPathwayOnPause。
编辑:稍后在代码中展开。 因此,当用户通过调用活动B中的Goggle Maps或Facebookpost活动导航表格活动A时,我将字符串savedImagePath保存在onSavedINstance状态。
然后,当用户返回活动A时,我在创建时返回savedIMagePathway,并将其传递给上面发布的文件imagePathFile = new File(savedImagePathwayOnPause)上面发布的方法。
此时savedImagePathwayOnPause的值为:/ storage_emulated/0/dive_photos/image2588.png
但是,有时此值返回空(到其初始化状态=“”)。这似乎是随机的......因为有些时候有效而不是其他......
@Override
protected void onSaveInstanceState(Bundle outState) {
//if onPause called on activity when user selects maps or FB, we lose value of global varibles like
//saveIMageBPath and photoBitMap but keep all view values (Edittexts)
//so we must save our global values here and call them back on onRestoreInstance state and assign to IMageButton
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
outState.putString("savedImapthPathwayOnSaveInstance", savedImagePath);
}
并在onCreate()中:
if (savedInstanceState != null) {
// Restore value of members from saved state
// mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
this.savedImapthPathwayOnPause = savedInstanceState.getString("savedImapthPathwayOnSaveInstance");
Toast.makeText(getApplicationContext(), "on returning the image path is : " + savedImapthPathwayOnPause, Toast.LENGTH_SHORT).show();
//no we ust get the bitmap form the image path and return to IMageView
Log.d("MYTAG", "on returning the image path is : " + savedImapthPathwayOnPause);
if(!savedImapthPathwayOnPause.isEmpty())
setImageViewFromSavedInstance(savedImapthPathwayOnPause);
Log.d("MYTAG", "on returning the image path not found:== " + savedImapthPathwayOnPause);
}
答案 0 :(得分:0)
由于@Paf建议问题是图像路径是在同步类中生成的,所以如果在生成路径之前调用了File open,那么我将一个空字符串传递给File构造函数。
为了避免这种情况,我通过禁用在Pre-Exceute上执行此操作的按钮禁用了对创建文件的任何调用,并在生成String文件路径之后在Aynsch内部类的onPostExceute方法中再次启用它们。
由于