2013年9月16日星期一

[Android] Sister ask SD card reading the image from a folder unsuccessful problem !

With a picture I read the SD card root directory there is no problem !

in one folder is read Bitmap return null!

I ask you why this is ! Where is not set !

please predecessors advise !


package com.myAndroid.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+"firstmobile/abc.jpg";
        String rootpath=Environment.getExternalStorageDirectory()+File.separator+"abc.jpg";
        
        Bitmap bitmap1=BitmapFactory.decodeFile(filepath);//这里得到的结果是null
        Bitmap bitmap2=BitmapFactory.decodeFile(rootpath);
        
        imageView01.setImageBitmap(bitmap1);
        imageView02.setImageBitmap(bitmap2);
        
    }
}

------ Solution ------------------------------------- -------






------ Solution --------- -----------------------------------
written as absolute path to try
---- - Solution --------------------------------------------
/ / Bitmap bitmap1 = BitmapFactory.decodeFile (filepath) ;/ / here the result is null
In front of this code print what filepath values:

Log.i ("Log:", "------------->" + filepath);

look under the DDMS values ​​printed on the right.
------ Solution ---------------------------------------- ----
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);

    }
}

I suggest you create a new file in sdcard folder , reboot and then run the simulator under the program should be able to read out
------ Solution ------------- -------------------------------


you go to see your SDCARD inside that store pictures in the folder permissions , it should be this folder does not allow you to read , and you read and write permissions set MANIFEST nothing , you first use ADB SHELL into the folder permissions to change it.
------ Solution ---------------------------------------- ----


seems to be no authority
you use adb shell into it,
then use the chmod 777 command to modify folder permissions
------ Solution --------------------- -----------------------
problem solving
casually assign a BitmapFactory.Options () on the line
optsa = new ; BitmapFactory.Options ();
optsa.inSampleSize = 10;
System.out.println ("iv" + i + "" + list.get (i));
Bitmap originalImage = BitmapFactory.decodeFile (list.get (i), optsa);

sleep , I rely on bone
------ Solution ------------------------------- -------------
found two errors :
1, downloadFile () method in the String filepath = rootPath + fileNa + "." + fileEx; are local variables , global variables declared as the best
2, downloading the picture of the code in question , the generated image to open after no preview

I follow their wording slightly modified , the real machine test was successful :


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();
    }
}


------ Solution ------------------------------------ --------
47 positive solution, I also tested successfully on a real machine , the code is not changed , that is not even the beginning of the Internet . Just reboot the machine can connect to the next
------ eference ------------------------------ ---------
program no problem , it should be the file name wrong it
------ eference ---------------- -----------------------
impossible ah ! ! I checked more than N times already ! File names and folder names can not be wrong !
------ eference --------------------------------------- < br> String filepath = Environment.getExternalStorageDirectory () + "/ firstmobile/abc1.jpg"
a change of name to try.
------ eference --------------------------------------- < br>

should not be a question of procedure ,
is the difference between the two paths are different , you can be sure that the path in question

I tried it , no problem ah !

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);

------ eference ------------------------------------ ---
I put the folder via adb shell removed.
then re- build folders and pictures. Total no mistake about it !

but still read is null. Please advise seniors .

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>

------ eference ------------------------------------- -

estimate is wrong , right , exercise
-----
-topic
------ eference --------------------------------- ------
or null! I always feel where to set right !
------ eference --------------------------------------- < br> you step through to see the String filepath = Environment.getExternalStorageDirectory () + File.separator + "firstmobile / abc.jpg" that no one inside the data are calculated in debug state look correct , then look at the entire path is correct
------ eference --------------------------------- ------
I put the code changed a bit ! But still does not work !
I used to read the file stream ways and ways have failed ! Oh ~ ~ ~

headache !


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);
}
    }

}

------ eference ------------------------------------- -
friendship passing
------ eference --------------------------------- ------
absolute path really never wrote ! How to write !
------ eference --------------------------------------- < br> add their own log
------ eference ----------------------------------- ----
no error !
------ eference --------------------------------------- < br> folder permissions may be not enough, you can try to modify the next folder permissions !
------ eference --------------------------------------- < br> Why you can read ! I can not read ! Is it not the same as our development environment !

I android 2.2
------ eference ------------------------------- --------

If the error is basically do not add their own log it. . .
------ eference --------------------------------------- < br> to add anything Log! Where plus ! Please correct me !
------ eference --------------------------------------- < br> strange run the code the first time made the landlord is no problem you can get pictures
------ eference ---------------- -----------------------
  This reply was moderator deleted at 2011-05-25 09:58:59

------ eference ------------------------------------ ---
DDMS print above is this ah !
/ mnt / sdcard / first / abc.jpg
------ eference -------------------------- -------------
right ! I Eclipse character encoding format is utf-8 , and this is not a relationship !
------ eference --------------------------------------- < br> settings should be no problem to say !
String filepath = "/ mnt / sdcard / first / abc.jpg";
String rootpath = "/ mnt / sdcard / abc.jpg";
I wrote all this !

in sdcard directory under the picture it is entirely possible to read out !
in sdcard / first / directory under the picture to read it out !

I think it is a problem where the set ! Code should be no problem ! !
------ eference --------------------------------------- < br> the need to look at your external storage path right , some devices external storage path is not the default . Make sure the path is correct and then try.
------ eference --------------------------------------- < br> sdcard permissions are d --- rwxr-x
sdcard / first permission is d --- rwxr-x
sdcard / first / abc.jpg permissions are --- rwxr-x
------ eference ----------------- ----------------------
I use the adb shell command as follows:
adb shell
cd sdcard
chmod 777 first

Is not it so ! If this is still unsuccessful !
------ eference --------------------------------------- < br>
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);

    }
}

my rights and permissions okay too, in fact, need only read and write permissions on it
sdcard permissions are d --- rwxr-x
sdcard / first permission is d --- rwxr-x
sdcard / first / Sunset.jpg permissions are --- rwxr-x
I give the landlord two suggestions:
1, re- build a simulator, the simulator previously deleted
2, my landlord does not know how to push into the picture , is still eclipse DDMS command adb push the push button to go into the next , there is a possibility that the landlord did not even put pictures into the first folder ( misuse ) , your code is not wrong
------ eference ---------------------------- -----------
is this the legendary gotcha ! Feeling procedure is no problem. . .
------ eference --------------------------------------- < br> is very tangled , such a simple question that I do not know where is the problem ?

landlord to create a new project and then try it ! I was a real test should
------ eference --------------------------------- ------
seeking final result , learning !
------ eference --------------------------------------- < br> I went , ran into the same problem. . .
I also read the file onto the java code below over and over, able to read , to the inside android TMD has been reported null pointer
incredibly well done

------ eference ------------------------------------ ---
path assignment when I have used the get absolute path , log in printouts are all right, to write bitmap on the wrong
------ eference ------ ---------------------------------
sister then I would like to ask you which version of the SDK use . After testing 2.1 above are not the problem. Be sure to add the SD card operating authority
------ eference --------------------------------- ------
  This reply was moderator deleted at 2011-05-25 09:59:03

------ eference ------------------------------------ ---
rebuild a simulator to try
------ eference ---------------------------- -----------
I follow the 38th floor predecessors method changed ! Does not work !

too incurable diseases of the ! Oh ~ ~

Options optsa = new BitmapFactory.Options();
optsa.inSampleSize = 10;


    Bitmap bitmap1=BitmapFactory.decodeFile(filepath,optsa);//这里得到的结果是null


I have deleted the simulator ! And then re- built a simulator !
in Hardware Lane added !
SD Card support yes
Abstracted LCD density 160
Max VM application heap size 24

should be right , right ! But still does not work !
------ eference --------------------------------------- < br> speechless ah
------ eference ------------------------------------ ---
Oh ~ ~ I am speechless ! !

to do has been done ! Where there will be issues that !
------ eference --------------------------------------- < br> Why do not you all made ​​up of works that can help you with , I think it is the landlord 's operating problems
------ eference --------------- ------------------------
I posted my source again ! Including how to download !


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();
}
}

I have deleted the simulator ! And then re- built a simulator !
in Hardware Lane added !
SD Card support yes
Abstracted LCD density 160
Max VM application heap size 24

sdcard permissions are d --- rwxr-x
sdcard / first permission is d --- rwxr-x
sdcard/first/icon01.gif permissions are --- rwxr-x

very gotcha !
------ eference --------------------------------------- < br> somehow able to read it ! 47th Floor seniors ! And where I write is not the same !
------ eference --------------------------------------- < br> Results posted ! ! ! Inexplicable success ! Oh ~ ~

I guess the question is simulator ! ! !
------ eference --------------------------------------- < br> hey depressed seduce landlord very clever ah ah
my question exhausting all my points nobody Browse Results
you that several sister provoke so much . . . . . .
I change the name of another gender Register ah
weighs what people ah
------ eference --------------------------- ------------
this post is good !
------ eference --------------------------------------- < br> seeking exchanges ! 550239802 ! Development
------ eference ---------------------------------------
the same types of problems encountered drifting
Later seems to be the new simulator, new construction and the like , nor how do just fine.
This is indeed a mysterious thing
------ eference ------------------------------ ---------
results did not find the answer, I would have tangled brother for a day . Waiting in the baffling , or wait for real test to see.
have seen say that the underlying problem, the current level of less than
------ eference ---------------------- -----------------
2.2 path is "String filepath =" / mnt / sdcard / first / abc.jpg ";"
2.1 path is "String filepath =" / sdcard / first / abc.jpg ";"
------ eference -------------- -------------------------
encounter the same problem. Depressed
------ eference -------------------------------------- -
solution to this problem is what
------ eference ----------------------------- ----------
string filename = new File ("/ mnt / sdcard / ...."). getAbsolutePath ();
bitmap = BitmapFactory.decodeFile (fname);

没有评论:

发表评论