| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 533 人关注过本帖
标题:外接 sd 卡写文件,却写到了手机内存中
只看楼主 加入收藏
so_love
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:7
帖 子:812
专家分:4151
注 册:2013-11-25
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:1 
外接 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卡
搜索更多相关主题的帖子: package import 手机 
2014-05-21 15:35
砖家的谎言
Rank: 12Rank: 12Rank: 12
等 级:禁止访问
威 望:30
帖 子:693
专家分:3898
注 册:2013-12-6
收藏
得分:20 
一般默认的是手机内存,外接的内存和手机的内存位置不一样,你检查下盘符是否错了

我不是砖家,要努力成为砖家。
2014-05-21 16:26
快速回复:外接 sd 卡写文件,却写到了手机内存中
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015251 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved