Monday, February 17, 2014

Sister ask SD card folder to read images from a file unsuccessful problem!


I read the same picture in the root directory of the SD card is no problem!

Read Bitmap returned in one folder is null!

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

Please advise seniors!

 
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) ;/ / result obtained here is null
Bitmap bitmap2 = BitmapFactory.decodeFile (rootpath);

imageView01.setImageBitmap (bitmap1);
imageView02.setImageBitmap (bitmap2);

}
}
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Program no problem, should it be wrong filename
Reply:
Impossible ah! ! I checked more than N times! File names and folder names can not be wrong!
Reply:
String filepath = Environment.getExternalStorageDirectory () + "/ firstmobile/abc1.jpg"
A change of name to try.
Reply:
cited landlord wangtiantian23 reply:
with a picture I read in the root directory of the SD card is no problem!

Read Bitmap returned in one folder is null!


The program should not be a problem,
The difference is a different path, a path can be sure that there is a problem

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

Reply:
I deleted the folder via adb shell.
And then re-build folders and images. They would not have it wrong!

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") ;/ / address change my
File file = new File (filepath);
if (file.exists ())
{
Bitmap bitmap1 = BitmapFactory.decodeFile (filepath) ;/ / result obtained here is 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", "stream is null");
throw new RuntimeException ("stream is null");
}
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 ();
}
/ * Delete the file method * /
private void delFile (String strFileName)
{
File myFile = new File (strFileName);
if (myFile.exists ())
{
myFile.delete ();
}
}

}
/ / Add some permissions in AndroidManifest.xml!
/ /
/ /
/ /
/ /
/ /

Reply:
<-! Added sdcard Permissions ->


<-! Permissions to create and delete files in SDCard in ->


Reply:
cited landlord wangtiantian23 reply:
with a picture I read in the root directory of the SD card is no problem!

Read Bitmap returned in one folder is null!

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

Please advise seniors!

C # code

. package com.myAndroid execrise;

import java.io.File;

import android.app.Activity;
import android.gra ......

Estimated to be wrong, right, exercise
-----
Nothing to do with the theme
Reply:
Or null! I always feel where to set right!
Reply:
You see single-step debugging, the String filepath = Environment.getExternalStorageDirectory () + File.separator + "firstmobile / abc.jpg" This is not a data which are calculated in debug state to see whether it is correct, then look at the whole path is correct
Reply:
Try to write the absolute path
Reply:
I put the code changed a bit! But still does not work!
Manner as I used to read the file and streams 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) ;/ / read the specified file, binary stream
bitmap1 = BitmapFactory.decodeStream (is);

} Catch (FileNotFoundException e1) {
e1.printStackTrace ();
}
/ / Bitmap bitmap1 = BitmapFactory.decodeFile (filepath) ;/ / here the result is null
Bitmap bitmap2 = BitmapFactory.decodeFile (rootpath);

imageView01.setImageBitmap (bitmap1);
imageView02.setImageBitmap (bitmap2);
}
}

}

Reply:
Passing friendship
Reply:
Absolute paths really never wrote! How to write!
Reply:
Add their own log
Reply:
No error!
Reply:
Folder permissions may be not enough, you can try to modify folder permissions next!
Reply:
Why you can read! I can not read! Is it not the same as our development environment!

I was android 2.2
Reply:
cited 15 floor wangtiantian23 reply:
no error!

If the error log basically do not have to add up. . .
Reply:
To add any Log! Where plus! Please correct me!
Reply:
Strange run code issued by the landlord for the first time there is no problem you can get the picture
Reply:
The reply was deleted at the moderator 2011-05-25 09:58:59

Reply:
/ / Bitmap bitmap1 = BitmapFactory.decodeFile (filepath) ;/ / result obtained here is null
Printed on the front of the code phrase about the filepath value:

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

Look at what value printed DDMS right.
Reply:
DDMS printed above is this ah!
/ Mnt / sdcard / first / abc.jpg
Reply:
Yes! My Eclipse character encoding format is utf-8, and this is not a relationship!
Reply:
Set say it should be no problem!
String filepath = "/ mnt / sdcard / first / abc.jpg";
String rootpath = "/ mnt / sdcard / abc.jpg";
I wrote it!

In the picture on the sdcard directory can be read out!
In sdcard / first / catalog picture reads it out!

I think it is a problem where setting! Code should be no problem! !
Reply:
The need to look at your external storage right path, the path that some external storage device than the default. Make sure the path is correct and try again.
Reply:
 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) ;/ / I read the image in the default subdirectory under DCIM is not null
Bitmap bitmap2 = BitmapFactory.decodeFile (rootpath);

imageView01.setImageBitmap (bitmap1);
imageView02.setImageBitmap (bitmap2);

}
}

I suggest you create a new folder under sdcard after the restart and then run the program under the simulator should be able to read out
Reply:
reference to the 25th floor wangtiantian23 reply:
say it should be no problem setting!
String filepath = "/ mnt / sdcard / first / abc.jpg";
String rootpath = "/ mnt / sdcard / abc.jpg";
I wrote it!

In the picture on the sdcard directory can be read out!
In sdcard / first / catalog picture reads it out!

I think it is a problem where setting! Code should be no problem! !


You go to see your picture in the store inside SDCARD folder permissions should be this folder does not allow you to read, and read and write permissions you set in your MANIFEST nothing you first go in the folder ADB SHELL permission to change it.
Reply:
sdcard permissions are d --- rwxr-x
Permissions sdcard / first also d --- rwxr-x
sdcard / first / abc.jpg permissions are --- rwxr-x
Reply:
reference to the 29th floor wangtiantian23 reply: permissions
sdcard is d --- rwxr-x
Permissions sdcard / first also d --- rwxr-x
sdcard / first / abc.jpg permissions are --- rwxr-x


Seems to be no authority
You go in with adb shell,
Then modify the permissions with chmod 777 command-click the folder
Reply:
I use adb shell command as follows:
adb shell
cd sdcard
chmod 777 first

Is not it so! If this is still unsuccessful!
Reply:
 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) ;/ / here to read the picture is not null
Bitmap bitmap2 = BitmapFactory.decodeFile (rootpath);

imageView01.setImageBitmap (bitmap1);
imageView02.setImageBitmap (bitmap2);

}
}

Okay my permission and authority, too, in fact, need only read and write permissions on it
sdcard permissions are d --- rwxr-x
Permissions sdcard / first also d --- rwxr-x
Permissions sdcard / first / Sunset.jpg are --- rwxr-x
I give the landlord two suggestions:
A re-build a simulator, the simulator previously deleted
2, I do not know how the landlord to push into the picture, is the command adb push the push button or eclipse DDMS go into the next, there is a possibility the landlord did not even put the picture into the first folder (misuse), you code is not wrong
Reply:
Is this the legendary gotcha! Feeling procedure is no problem. . .
Reply:
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 had to be the real test of
Reply:
Seeking final results, the study!
Reply:
I have to go, hit the same problem. . .
I also read the code onto the java files over the following times, to read, to the inside android TMD has been reported null pointer
Well done incredibly

Reply:
I took the path assignment, they both used the get the absolute path, log in to print out are all right, to write bitmap mistake
Reply:
Problem solving
Just 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 skeletal
Reply:
So I ask you to use the SDK sister is which version. After testing more than 2.1 is not the problem. Be sure to add SD card operating authority
Reply:
The reply was deleted at the moderator 2011-05-25 09:59:03

Reply:
Try rebuilding a simulator
Reply:
I changed the method according to the 38th floor of the seniors! Does not work!

Too incurable diseases it! Oh ~ ~
 
Options optsa = new BitmapFactory.Options ();
optsa.inSampleSize = 10;


Bitmap bitmap1 = BitmapFactory.decodeFile (filepath, optsa) ;/ / result obtained here is null


I have deleted the simulator! Then re-built a simulator!
In Hardware Lane joined!
SD Card support yes
Abstracted LCD density 160
Max VM application heap size 24

Should be right now! But still does not work!
Reply:
Speechless ah
Reply:
Oh ~ ~ I am speechless! !

The do's are done! Where there is a problem that!
Reply:
Why do not you put the whole project made up, can help you with, I think it is a problem with the operation of the landlord
Reply:
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) ;/ / result obtained here is 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", "stream is null");
throw new RuntimeException ("stream is null");
}
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! Then re-built a simulator!
In Hardware Lane joined!
SD Card support yes
Abstracted LCD density 160
Max VM application heap size 24

sdcard permissions are d --- rwxr-x
Permissions sdcard / first also d --- rwxr-x
sdcard/first/icon01.gif permissions are --- rwxr-x

It gotcha!
Reply:
Found two errors:
"." 1, downloadFile () method in the String filepath = rootPath + fileNa + + fileEx; are local variables, global variables declared as the best
2, download pictures when there is a problem, no preview images generated after opening

I follow their wording slightly modified, tested successfully on a real machine:

 
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) ;/ / result obtained here is 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", "stream is null");
throw new RuntimeException ("stream is null");
}
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 ();
}
}


Reply:
Somehow able to read! 47th Floor seniors! And where I write is not the same!
Reply:
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
Reply:
Results posted! ! ! Inexplicable success! Oh ~ ~

I guess the problem is the simulator! ! !

No comments:

Post a Comment