String filepath=Environment.getExternalStorageDirectory()+"/firstmobile/abc1.jpg"
改个名试试。

解决方案 »

  1.   


    应该不是程序的问题,
    两者的区别也就是路径不同,可以肯定的是路径有问题我试了一下,完全没问题啊!ImageView imageView01=(ImageView)findViewById(R.id.image1);
            ImageView imageView02=(ImageView)findViewById(R.id.image2);
            
            String filepath=Environment.getExternalStorageDirectory()+File.separator+"dcim/Camera/img.jpg";
            String rootpath=Environment.getExternalStorageDirectory()+File.separator+"img.jpg";
            
            Bitmap bitmap1=BitmapFactory.decodeFile(filepath);        
            Bitmap bitmap2=BitmapFactory.decodeFile(rootpath);
            
            imageView01.setImageBitmap(bitmap1);
            imageView02.setImageBitmap(bitmap2);
      

  2.   

    我把文件夹通过adb shell删除掉。
    然后重新建个文件夹和图片。总不会有错吧!但是还是读取是null。请各位前辈指教。package com.myAndroid.execrise;import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.URL;
    import java.net.URLConnection;import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.os.Environment;
    import android.util.Log;
    import android.widget.ImageView;public class ImageActivity extends Activity 
    {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            ImageView imageView01=(ImageView)findViewById(R.id.imageView01);
            ImageView imageView02=(ImageView)findViewById(R.id.imageView02);
            
            String filepath=Environment.getExternalStorageDirectory()+File.separator+"firstmobile/abc.jpg";
            String rootpath=Environment.getExternalStorageDirectory()+File.separator+"abc.jpg";
            
            try {
    downloadFile("http://10.0.2.2/lhwx/abc.jpg");//地址换一下
    File file=new File(filepath);
    if(file.exists())
    {
        Bitmap bitmap1=BitmapFactory.decodeFile(filepath);//这里得到的结果是null
        Bitmap bitmap2=BitmapFactory.decodeFile(rootpath);
        
        imageView01.setImageBitmap(bitmap1);
        imageView02.setImageBitmap(bitmap2);
    }
    } catch (IOException e) {
    e.printStackTrace();
    }

            
        }
    public void downloadFile(String url) throws IOException 
    {
    String fileEx = url.substring(url.lastIndexOf(".")+1,url.length()).toLowerCase();
            String fileNa = url.substring(url.lastIndexOf("/")+1,url.lastIndexOf("."));
            
    URL fileUrl = new URL(url);
    URLConnection conn = fileUrl.openConnection();
    conn.connect();
    InputStream inputStream = conn.getInputStream();
    if (inputStream == null){
    Log.e("Error", "流为空");
    throw new RuntimeException("流为空");
    }
    File sdCard = Environment.getExternalStorageDirectory();
            String rootPath = sdCard.getPath()+"/firstmobile/";
            File file=new File(rootPath);
            file.delete();
            if(!file.exists())
            {
                file.mkdir();
            }
            
            String filepath=rootPath+fileNa+"."+fileEx;
            
    OutputStream os = new FileOutputStream(filepath);
    byte buf[] = new byte[1024];
    do {
    int numread = inputStream.read(buf);
    if (inputStream.read(buf) == -1) {
    break;
    }
    os.write(buf, 0, numread);
    } while (true); inputStream.close();
    os.close();
    }
    /*删除文件方法*/
    private void delFile(String strFileName) 

      File myFile = new File(strFileName); 
      if(myFile.exists()) 
      { 
        myFile.delete(); 
      }
    }}
    //在AndroidManifest.xml加一些权限!
    //<uses-permission android:name="android.permission.INTERNET"></uses-permission>
    //<uses-permission android:name="android.permission.INSTALL_PACKAGES"></uses-permission>
    //<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
    //<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    //<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
      

  3.   

    <!-- 添加sdcard权限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <!-- 在SDCard中创建与删除文件权限 -->
    <use-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></use-permission>
    <use-permission android:name="android.permission.RESTART_PACKAGES"></use-permission>
      

  4.   

    估计是写错了吧,exercise
    -----
    与主题无关
      

  5.   

    还是null!我总感觉哪里设置不对!
      

  6.   

    你单步调试看看,把 String filepath=Environment.getExternalStorageDirectory()+File.separator+"firstmobile/abc.jpg"这个里面的没一个数据在调试状态都计算看看,是否正确,然后看看整个路径是否正确
      

  7.   

    我把代码在改了一下!但是还是不行!
    我用流的方式和读取文件的方式均告失败!呵呵~~~头疼!
    package com.myAndroid.execrise;import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;import android.app.Activity;
    import android.content.ContentResolver;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.util.Log;
    import android.widget.ImageView;public class ImageActivity extends Activity 
    {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            ImageView imageView01=(ImageView)findViewById(R.id.imageView01);
            ImageView imageView02=(ImageView)findViewById(R.id.imageView02);
            
            //String filepath=Environment.getExternalStorageDirectory()+File.separator+"first/abc.jpg";
            //String rootpath=Environment.getExternalStorageDirectory()+File.separator+"abc.jpg";
            
            String filepath="/mnt/sdcard/first/abc.jpg";
            String rootpath="/mnt/sdcard/abc.jpg";
    File file=new File(filepath);
    if(file.exists())
    {
    Bitmap bitmap1 = null;
    try {
    InputStream  is = new FileInputStream(filepath);// 读出指定的文件,二进制流
    bitmap1 = BitmapFactory.decodeStream(is); } catch (FileNotFoundException e1) {
    e1.printStackTrace();
    }
        //Bitmap bitmap1=BitmapFactory.decodeFile(filepath);//这里得到的结果是null
        Bitmap bitmap2=BitmapFactory.decodeFile(rootpath);
        
        imageView01.setImageBitmap(bitmap1);
        imageView02.setImageBitmap(bitmap2);
    }
        }}
      

  8.   

    为什么你们的能读取!我的不能读取!难道我们的开发环境不一样!我是android 2.2
      

  9.   

    要是报错基本上就不用自己加log了
      

  10.   

    要加什么Log!在哪里加!请指正!
      

  11.   

    //Bitmap bitmap1=BitmapFactory.decodeFile(filepath);//这里得到的结果是null
    在这句代码前面打印一下filepath的值:Log.i("Log:","------------->"+filepath);在DDMS下看一下打印的值对不对。
      

  12.   

    DDMS上面打印的也是这个啊!
    /mnt/sdcard/first/abc.jpg
      

  13.   

    对了!我Eclipse的字符编码格式是utf-8的是不是和这个有关系!
      

  14.   

    要说设置也应该没问题!
            String filepath="/mnt/sdcard/first/abc.jpg";
            String rootpath="/mnt/sdcard/abc.jpg";
    我都这样写了!在sdcard目录下的图片就完全可以读取出来!
    在sdcard/first/目录下的图片就读取不出来!我想是哪里设置有问题!代码应该是没问题!!
      

  15.   

    package com.myandrod.execrise;import java.io.File;import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.os.Environment;
    import android.widget.ImageView;public class ImageActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            ImageView imageView01=(ImageView)findViewById(R.id.imageview01);
            ImageView imageView02=(ImageView)findViewById(R.id.imageview02);
            
            String filepath=Environment.getExternalStorageDirectory()+File.separator+"DCIM/3_1161090452.jpg";
            String rootpath=Environment.getExternalStorageDirectory()+File.separator+"00.JPG";
            
            Bitmap bitmap1=BitmapFactory.decodeFile(filepath);//我在系统默认的子目录DCIM下面读取图片不为null
            Bitmap bitmap2=BitmapFactory.decodeFile(rootpath);
            
            imageView01.setImageBitmap(bitmap1);
            imageView02.setImageBitmap(bitmap2);    }
    }
    我建议你在sdcard下新建一个文件夹之后,重启下模拟器再运行程序应该可以读出来
      

  16.   


    你去看看你在SDCARD里面那个存放图片的文件夹权限,应该是这个文件夹是不允许你读取的,和你在MANIFEST中设置的读写权限无关,你先用ADB SHELL进去把文件夹的权限改一下。
      

  17.   

    sdcard 的权限是          d---rwxr-x
    sdcard/first 的权限也是 d---rwxr-x
    sdcard/first/abc.jpg 的权限是 ---rwxr-x
      

  18.   


    好像是没有权限
    你用adb shell进去,
    然后用chmod 777 命令修改一下文件夹的权限 
      

  19.   

    我用的adb shell命令如下:
    adb shell
    cd sdcard
    chmod 777 first是不是应该这样!如果是这样还是不成功!
      

  20.   

    package com.myandrod.execrise;import java.io.File;import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.os.Environment;
    import android.widget.ImageView;public class ImageActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            ImageView imageView01=(ImageView)findViewById(R.id.imageview01);
            ImageView imageView02=(ImageView)findViewById(R.id.imageview02);
            
            String filepath=Environment.getExternalStorageDirectory()+File.separator+"first/Sunset.jpg";
            String rootpath=Environment.getExternalStorageDirectory()+File.separator+"Sunset.jpg";
            
            Bitmap bitmap1=BitmapFactory.decodeFile(filepath);//此处读取图片不为null
            Bitmap bitmap2=BitmapFactory.decodeFile(rootpath);
            
            imageView01.setImageBitmap(bitmap1);
            imageView02.setImageBitmap(bitmap2);    }
    }
    和权限没关系我的权限也是这样,其实只需要读写权限就可以了
    sdcard 的权限是 d---rwxr-x
    sdcard/first 的权限也是 d---rwxr-x
    sdcard/first/Sunset.jpg 的权限是 ---rwxr-x
    我给楼主2个建议:
    1,重新建个模拟器,把以前的模拟器删掉
    2,我不知道楼主怎么把图片push进去的,是命令adb push还是eclipse DDMS 下那个push按钮放进去的,有一种可能是楼主根本就没把图片放进first文件夹(误操作),你的代码是没有错的
      

  21.   

    我去,碰上了同样的问题
    我还把读文件的代码拖到java下面过了遍,能读出来,到android里面就TMD一直报空指针
    火大的不得了
      

  22.   

    我把路径赋值的时候,都用了get绝对路径,log里打印出来的也全对,到写bitmap就出错
      

  23.   

    问题解决
    随便赋值个BitmapFactory.Options() 就行了
                        optsa = new BitmapFactory.Options();
        optsa.inSampleSize = 10;
    System.out.println("iv"+i+"  "+list.get(i));
    Bitmap originalImage = BitmapFactory.decodeFile(list.get(i),optsa);睡觉,我靠骨骼
      

  24.   

    那么我想问下小妹你用的SDK是哪个版本。经过测试2.1以上是没问题的。一定要加SD卡操作权限
      

  25.   

    我按照38楼前辈的方法改了!也不行!太疑难杂症了!呵呵~~ Options optsa = new BitmapFactory.Options();
    optsa.inSampleSize = 10;
        Bitmap bitmap1=BitmapFactory.decodeFile(filepath,optsa);//这里得到的结果是null
    我把模拟器都删除掉了!然后重新建了一个模拟器!
    在Hardware里加入了!
    SD Card support   yes
    Abstracted LCD density      160
    Max VM application heap size  24应该没错了吧!但是还是不行!
      

  26.   

    我在把我源码贴一遍!包括如何下载的!
    package com.myAndroid.execrise;import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.BitmapFactory.Options;
    import android.os.Bundle;
    import android.os.Environment;
    import android.util.Log;
    import android.widget.ImageView;public class ImageActivity extends Activity 
    {
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            ImageView imageView01=(ImageView)findViewById(R.id.imageView01);
            ImageView imageView02=(ImageView)findViewById(R.id.imageView02);
            
            
            String filepath="/mnt/sdcard/first/icon01.gif";
            String rootpath="/mnt/sdcard/abc.jpg";
            try {
    downloadFile("http://www.sohu.com/upload/hp/images/icon01.gif");
    } catch (IOException e) {
    e.printStackTrace();
    }
    File file=new File(filepath);
    if(file.exists())
    {
    Options optsa = new BitmapFactory.Options();
    optsa.inSampleSize = 10;
        Bitmap bitmap1=BitmapFactory.decodeFile(filepath,optsa);//这里得到的结果是null
    Log.i("Log:","------------->"+filepath);     Bitmap bitmap2=BitmapFactory.decodeFile(rootpath);
        
        imageView01.setImageBitmap(bitmap1);
        imageView02.setImageBitmap(bitmap2);
    }
        }
    public void downloadFile(String url) throws IOException 
    {
    String fileEx = url.substring(url.lastIndexOf(".")+1,url.length()).toLowerCase();
            String fileNa = url.substring(url.lastIndexOf("/")+1,url.lastIndexOf("."));
            
    URL fileUrl = new URL(url);
    URLConnection conn = fileUrl.openConnection();
    conn.connect();
    InputStream inputStream = conn.getInputStream();
    if (inputStream == null){
    Log.e("Error", "流为空");
    throw new RuntimeException("流为空");
    }
    File sdCard = Environment.getExternalStorageDirectory();
            String rootPath = sdCard.getPath()+"/first/";
            File file=new File(rootPath);
            if(!file.exists())
            {
                file.mkdir();
            }
            
            String filepath=rootPath+fileNa+"."+fileEx;
            
    OutputStream os = new FileOutputStream(filepath);
    byte buf[] = new byte[1024];
    do {
    int numread = inputStream.read(buf);
    if (inputStream.read(buf) == -1) {
    break;
    }
    os.write(buf, 0, numread);
    } while (true); inputStream.close();
    os.close();
    }
    }我把模拟器都删除掉了!然后重新建了一个模拟器!
    在Hardware里加入了!
    SD Card support yes
    Abstracted LCD density 160
    Max VM application heap size 24sdcard 的权限是 d---rwxr-x
    sdcard/first 的权限也是 d---rwxr-x
    sdcard/first/icon01.gif 的权限是 ---rwxr-x很疑难杂症!
      

  27.   

    发现了两处错误:
    1、downloadFile()方法里的 String filepath=rootPath+fileNa+"."+fileEx; 是局部变量,最好声明成全局变量
    2、下载图片时的代码有问题,生成的图片打开后没有预览我按照自己的写法稍微修改了一下,在真机上测试成功了:
    package com.test.android;import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.BitmapFactory.Options;
    import android.os.Bundle;
    import android.os.Environment;
    import android.util.Log;
    import android.widget.ImageView;public class DownloadImage extends Activity 
    {

    String filepath;
    String rootpath="/sdcard/img.jpg";

        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            ImageView imageView01=(ImageView)findViewById(R.id.image1);
            ImageView imageView02=(ImageView)findViewById(R.id.image2);
            
            try {
                downloadFile("http://www.sohu.com/upload/hp/images/icon01.gif");
            } catch (IOException e) {
                e.printStackTrace();
            }
            File file=new File(filepath);
            if(file.exists())
            {
                Bitmap bitmap1=BitmapFactory.decodeFile(filepath);//这里得到的结果是null
                Bitmap bitmap2=BitmapFactory.decodeFile(rootpath);
                
                imageView01.setImageBitmap(bitmap1);
                imageView02.setImageBitmap(bitmap2);
            }
        }
        public void downloadFile(String url) throws IOException 
        {
            String fileEx = url.substring(url.lastIndexOf(".")+1,url.length()).toLowerCase();
            String fileNa = url.substring(url.lastIndexOf("/")+1,url.lastIndexOf("."));
            
            URL fileUrl = new URL(url);
            URLConnection conn = fileUrl.openConnection();
            conn.connect();
            InputStream inputStream = conn.getInputStream();
            if (inputStream == null){
                Log.e("Error", "流为空");
                throw new RuntimeException("流为空");
            }
            File sdCard = Environment.getExternalStorageDirectory();
            String rootPath = sdCard.getPath()+"/first/";
            File file=new File(rootPath);
            if(!file.exists())
            {
                file.mkdir();
            }
            
            filepath=rootPath+fileNa+"."+fileEx;
            
            OutputStream os = new FileOutputStream(filepath);
            byte buf[] = new byte[1024];
            int numread = 0;
            while ((numread = inputStream.read(buf))!= -1) {
             os.write(buf, 0, numread);
            }
            inputStream.close();
            os.close();
        }
    }