无法访问SD卡

时间:2011-09-09 09:47:20

标签: android eclipse io sd-card

我正在尝试将文件读/写到SD卡。我已尝试在我的真实手机和eclipse中的模拟器上执行此操作。在两个设备上,/ mnt / sdcard /或/ sdcard的权限只是“d --------”,我猜这意味着我无法读取或写入。

我无法在eclipse中的“文件视图”中打开文件夹,而且当我也尝试“adb pull /mnt/sdcard/test.txt test.txt”时,我得到的远程对象不存在。

Android应用程序确实拥有“WRITE_EXTERNAL_STORAGE”的权限。

以下是我在运行应用时所做的事情;

        try {
            File root = Environment.getExternalStorageDirectory();
            Log.e(TAG,root.getAbsolutePath());
                File gpxfile = new File(root, "test.txt");
                FileWriter gpxwriter = new FileWriter(gpxfile);
                BufferedWriter out = new BufferedWriter(gpxwriter);
                out.write("Hello world");
                out.close();
        } catch (IOException e) {
            Log.e(TAG, "Could not write file " + e.getMessage());
        }

我没有例外。 有任何想法吗?这个问题让我感到害怕!

1 个答案:

答案 0 :(得分:1)

这是您可以从特定网址在SD卡中显示图片的示例,因此您需要获得许可等权限...

permission ::

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

Java file ::

package com.sdcard;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;

public class SdcardActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try{
            URL url = new URL ("http://www.coolpctips.com/wp-content/uploads/2011/05/top-30-android-games.jpg");
            InputStream input = url.openStream();
            try {
                OutputStream output = new FileOutputStream  (Environment.getExternalStorageDirectory()+"/top-30-android-games.jpg");
                int aReasonableSize = 1000;
                byte[] buffer = new byte[aReasonableSize];
                int bytesRead = 0;;
                try {
                    while ((bytesRead = input.read(buffer)) > 0) {
                        output.write(buffer, 0, bytesRead);
                    }
                } finally {
                    output.close();
                }
            } finally {
                input.close();
            }
            }catch (Exception e) {
                e.printStackTrace();
            }
        }

还有一段时间我们的互联网连接错过了设备或我们无法找到SD卡的设备问题