1

///edited

I try to run the program on the main thread and it can read and save.

I think is problem of the asynctask!!

///

First I already add the user permission to the android manifest

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

I try to read a raw image data from mnt/sdcard/Bluetooth and process it and then write to the internal sdcard and the write path is mnt/sdcard/Bluetooth.

The program can run in my android emulator and save File.

However,when run it real phone, it seems that the phone can only read my file,but not save file.

    public class MainActivity extends Activity {
String filenameIn ="ardrone.raw"; 
File file = new File(Environment.getExternalStorageDirectory()+"/Bluetooth/",            filenameIn);
String filename ="convertA.jpg"; 
File outputfile = new File(Environment.getExternalStorageDirectory() +"/Bluetooth/",filename);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myImageView = (ImageView)findViewById(R.id.imageView);
    Toast.makeText(this, "search raw file", Toast.LENGTH_LONG).show();
    new imageProcess().execute(); 
     sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://" + Environment.getExternalStorageDirectory())));        
     Bitmap myBitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()
             .getAbsolutePath()+"/Bluetooth/convertA.jpg");
     myImageView.setImageBitmap(myBitmap);

    if (file.exists()){
         Toast.makeText(this, "InFile:)", Toast.LENGTH_LONG).show();
    }
    else{
         Toast.makeText(this, "InFile:(((((", Toast.LENGTH_LONG).show();
    }
    if (outputfile.exists()){
         Toast.makeText(this, "outputfile:)", Toast.LENGTH_LONG).show();
    }
    else{
         Toast.makeText(this, "outputfile:(((((", Toast.LENGTH_LONG).show();
    }

}

public class imageProcess extends AsyncTask<String, Integer, Boolean>{
    Boolean check_point= false;
    @Override
    protected Boolean doInBackground(String... arg0) {

        try {
                receiveVideoRawData();

        } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }

            return null;
      }  


}   




public void receiveVideoRawData() throws IOException{
    byte[] buf_rcv = new byte[153600];
    ByteArrayOutputStream ous = new ByteArrayOutputStream();
    InputStream ios = new FileInputStream(file);
    int read = 0;
     while ( (read = ios.read(buf_rcv)) != -1 ) {
               ous.write(buf_rcv, 0, read);
            }
                ous.close();
                ios.close();

        ReadRawFileImage readMyRawData=new ReadRawFileImage();      
        image = readMyRawData.readUINT_RGBImage(buf_rcv);
                    //transfer data
        OutputStream _outStream  = new FileOutputStream(outputfile);
         Bitmap pBitmap = image ;
         pBitmap.compress(Bitmap.CompressFormat.JPEG, 90, _outStream); 
          _outStream.flush();
          _outStream.close();





}
}
DekangHu
  • 155
  • 4
  • 14
  • 1
    You are saving the image file in a background task, but attempting to decode and display the image on the main thread immediately after starting the task. Are you sure the background save has finished by the point where you are attempting to read it? This looks like a race condition. – devunwired Mar 05 '13 at 03:51

4 Answers4

1

The first thing is to check if your path exists; on different phones the sd card may be mounted at different locations. On my Linux,

$ ~/android-sdk-linux/platform-tools/adb shell ls -l /mnt/sdcard/

The 2nd thing is to check permissions on the file and the containing directory.

Then, you might examine the logs (they may already contain an error message); then, add debug output to your code and examine the logs with that debug output.

18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
0

try making your new file writable using file.writable(true). It could also be that your new file doesnt really exist.

James andresakis
  • 5,335
  • 9
  • 53
  • 88
0

Apparently you only need to set the external read permission if you don't have the write permission, this might be overwriting your write privileges? http://developer.android.com/reference/android/Manifest.permission.html#READ_EXTERNAL_STORAGE

Mike
  • 413
  • 2
  • 12
0

First you should confirm that the real device has SDCARD storage(Some device have not "SDCARD" storage).
Please see below reference to get all storage list
How to list additional external storage folders (mount points)?

Community
  • 1
  • 1
boiledwater
  • 10,372
  • 4
  • 37
  • 38