1

///已编辑

我尝试在主线程上运行程序,它可以读取和保存。

我认为是异步任务的问题!

///

首先,我已经将用户权限添加到 android manifest

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

我尝试从mnt/sdcard/Bluetooth读取原始图像数据并对其进行处理,然后写入内部 sdcard,写入路径为mnt/sdcard/Bluetooth

该程序可以在我的android模拟器中运行并保存文件。

但是,在真机上运行时,似乎手机只能读取我的文件,而不能保存文件。

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





}
}
4

4 回答 4

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.

于 2013-03-05T05:36:47.887 回答
0

首先你应该确认真实设备有SDCARD存储有些设备没有“SDCARD”存储)。
请参阅以下参考以获取所有存储列表
如何列出其他外部存储文件夹(安装点)?

于 2013-03-05T05:41:05.227 回答
0

尝试使用 file.writable(true) 使您的新文件可写。也可能是您的新文件并不真正存在。

于 2013-03-05T03:31:00.080 回答
0

显然,如果您没有写入权限,您只需要设置外部读取权限,这可能会覆盖您的写入权限? http://developer.android.com/reference/android/Manifest.permission.html#READ_EXTERNAL_STORAGE

于 2013-03-05T04:28:38.977 回答