开发者

Unable to access sdcard

开发者 https://www.devze.com 2023-04-03 22:18 出处:网络
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----

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("H开发者_Go百科ello 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!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消