为什么总是报FileNotFoundException

coder_cp 2012-05-20 11:43:00
今天写一个例子程序,就是把android文件夹res下面的图片拷贝到sd卡里面,但是到:
FileOutputStream fos = new FileOutputStream(photoFile);的时候就报FileNotFoundException异常。

package com.test.aiai;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class UpActivity extends Activity {
private String TAG="fileuploaddownload" ;
public static String cloudUrl ;
public static String cloudUrl_path="";
public static final String PATH="/sdcard/fileio/";
public final String SD_PATH=android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
//通过测试SD_PATH为:/mnt/sdcard
public static final String FILE_PATH ="/fileio";
private final String NATIVE_FILENAME ="sa_native.png";
private final String NETWORD_FILENAME="sa_network.jpg";
private ImageView img1 ,img2 ;
private Button mbt1 ;
private OnClickListener up_listener = null ;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);;
this.img1=(ImageView)findViewById(R.id.img1) ;
this.mbt1=(Button)findViewById(R.id.but);
try
{//把文件夹下面的图片转换成流的形式
InputStream in =getResources().openRawResource(R.drawable.ic_launcher);
//从流中读取图片成为Bitmap格式
Bitmap bm = BitmapFactory.decodeStream(in);
in.close();
//设置图片显示,从Bitmap中读取
img1.setImageBitmap(bm);
}
catch(IOException e)
{
e.printStackTrace();
}
String photoFilename = SD_PATH+FILE_PATH+"/"+NATIVE_FILENAME ;
File photoFile =new File(photoFilename); //指明需要的文件夹的全路径
try
{
Toast.makeText(this, "3", Toast.LENGTH_LONG).show();
InputStream in = getResources().openRawResource(R.drawable.ic_launcher);
Toast.makeText(this, "4", Toast.LENGTH_LONG).show();
FileOutputStream fos = new FileOutputStream(photoFile);

byte[] buffer = new byte[8192];
int count = 0 ;
while((count=in.read(buffer))>0)
{
fos.write(buffer,0,count);
}
fos.close();
in.close();
Toast.makeText(this, "chenggon", Toast.LENGTH_LONG).show();
}
catch(FileNotFoundException e)
{
Toast.makeText(this, "文件没有找到异常", Toast.LENGTH_LONG).show();
e.printStackTrace();
}catch(IOException e)
{
e.printStackTrace();
Toast.makeText(this, "io", Toast.LENGTH_LONG).show();
}catch(Exception e)
{
Toast.makeText(this, "cuowu", Toast.LENGTH_LONG).show();
}


}
}

运行结果:可以输出3数字,但是之后直接输出“文件没有找到异常”,为什么?????
...全文
992 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
coder_cp 2012-05-20
  • 打赏
  • 举报
回复
行了。
life02 2012-05-20
  • 打赏
  • 举报
回复
public static File createDIR(String dirpath) {

File dir = new File(dirpath);
if (!dir.exists()) {
dir.mkdir();
}
return dir;

}
life02 2012-05-20
  • 打赏
  • 举报
回复
文件夹都不存在啊
你应该先 file = new File(SD_PATH+FILE_PATH);
然后再
File photoFile =new File(photoFilename); //指明需要的文件夹的全路径

80,348

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧