///已编辑
我尝试在主线程上运行程序,它可以读取和保存。
我认为是异步任务的问题!
///
首先,我已经将用户权限添加到 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();
}
}