外接 sd 卡写文件,却写到了手机内存中
有一个程序。往手机外接的sd卡写文件。为什么总是写到手机内存里?main.java
package mobile.android.ch08.sdcard.file.outputinput;
import
import
import
import
import
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.Settings.System;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class Main extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClick_SaveImageToSDCard(View view)
{
try
{
FileOutputStream fos = new FileOutputStream(
android.os.Environment.getExternalStorageDirectory()
+ "/file.txt");
InputStream is = getResources().getAssets().open("file.txt");
byte[] buffer = new byte[8192];
int count = 0;
while ((count = is.read(buffer)) >= 0)
{
fos.write(buffer, 0, count);
}
fos.close();
is.close();
Toast.makeText(this, "已成功将文件写到SD卡上.", Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
public void onClick_ReadImageFromSDCard(View view)
{
String filename = android.os.Environment.getExternalStorageDirectory()
+ "/file.txt";
if (!new File(filename).exists())
{
Toast.makeText(this, "还没有往SD卡写入文件", Toast.LENGTH_LONG).show();
return;
}
//ImageView imageView = (ImageView) findViewById(R.id.imageview);
TextView textView = (TextView)findViewById(R.id.textview);
try
{
FileInputStream fis = new FileInputStream(filename);
//Bitmap bitmap = BitmapFactory.decodeStream(fis);
//imageView.setImageBitmap(bitmap);
//fis.close();
//?InputStream is = openFileInput("file.txt");
byte[] buffer = new byte[8192];
int byteCount = fis.read(buffer);
String str2 = new String(buffer, 0, byteCount, "utf-8");
//TextView textView = (TextView)findViewById(R.id.textview);
textView.setText(str2);
fis.close();
}
catch (Exception e)
{
// TODO: handle exception
}
}
}
///////////////
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="向SD卡保存文件"
android:onClick="onClick_SaveImageToSDCard" />
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="读取SD卡中的文件"
android:onClick="onClick_ReadImageFromSDCard" />
<TextView android:id="@+id/textview" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textSize="22dp" />
</LinearLayout>
///////////////////
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.
package="mobile.android.ch08.sdcard.file.outputinput"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Main"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>
可以直接从pc机写到手机里。但是却写到了手机内存,不是外接sd卡