gpt4 book ai didi

java - FileNotFoundException 从 SD 卡路径加载图像

转载 作者:太空宇宙 更新时间:2023-11-04 15:05:31 26 4
gpt4 key购买 nike

我试图通过打开一个文件从 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。

编辑:稍后在代码中扩展。因此,当用户通过从 Activity B 中调用 Goggle Maps 或 Facebookpost Activity 来导航离开表单 Activity A 时,我将字符串 savingImagePath 保存在 onSavedINStance 状态中。

然后,当用户返回 Activity A 时,我会在创建时重新获取 savingIMagePathway,并将其传递给上面发布的 File 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);
}

最佳答案

正如 @Paf 所建议的,问题是图像路径是在同步类中生成的,因此如果在生成路径之前调用 File open,我会将空字符串传递给 File 构造函数。

为了避免这种情况,我通过禁用在 Pre-Exceute 中执行此操作的按钮来禁用对创建文件的任何调用,并在生成字符串文件路径后在 Aynsch 内部类的 onPostExceute 方法中再次启用它们。

谢谢

关于java - FileNotFoundException 从 SD 卡路径加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22046449/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com