无法访问SD卡

发布于 2024-12-03 17:54:36 字数 860 浏览 2 评论 0原文

我正在尝试将文件读/写到 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());
        }

我没有例外。 有什么想法吗?这个问题快要死我了!

I'm trying to read/write files to the sdcard. I've tried doing this both on my real phone and on an emulator in eclipse. On both devices the permission to the /mnt/sdcard/ or /sdcard is only "d--------", which I guess means I can't read or write.

I cant open the folder from the "File view" in eclipse, and also when I also try the "adb pull /mnt/sdcard/test.txt test.txt" I get the remote object does not exist.

The android app does have the permission to "WRITE_EXTERNAL_STORAGE".

Here's for example what I do when running the app;

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

I do not get an exception.
Any ideas? This problem is killing me!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘香 2024-12-10 17:54:36

这是一个示例,您可以从特定的 url 显示 sdcard 中的图像,因此您需要权限,例如 ...

权限 ::

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

Java 文件 ::

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卡

This is example where you can show image in sdcard from perticular url so you need permission like ...

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

And some time also face problem in device that our Internet connection missed or we cant able to find sdcard

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文