2012-10-31 14 views
15

मैं एंड्रॉइड के लिए नया हूं, और मुझे एक समस्या है जब मैं एक फ़ाइल को आंतरिक स्टोरेज में सहेजने की कोशिश कर रहा हूं, नया उदाहरण मेरे एसडीके पर काम करता है, लेकिन काम नहीं करता है मेरा फ़ोन।आंतरिक स्टोरेज एंड्रॉइड में सहेजने वाली फ़ाइल

/data/data/com.example.key/files/text/(my_title) 

धन्यवाद: -

मैं एंड्रॉयड 2.1 माध्यम से के साथ एक सोनी एरिक्सन XPERIA में उदाहरण डे को चलाने के लिए, कोशिश कर रहा हूँ ... log.i मुझे अगली पंक्ति देता है।

@Override 
     protected void onCreate(Bundle savedInstanceState) { 
      // TODO Auto-generated method stub 
      super.onCreate(savedInstanceState); 

      setContentView(R.layout.new_text); 

      file = (EditText) findViewById(R.id.title_new); 
      entry = (EditText) findViewById(R.id.entry_new); 

      btn = (Button) findViewById(R.id.save_new); 
      btn.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 

        File myDir = getFilesDir(); 


        NEWFILENAME = file.getText().toString(); 
        if (NEWFILENAME.contentEquals("")){ 
         NEWFILENAME = "UNTITLED"; 
        } 
        NEWENTRY = entry.getText().toString(); 

        try { 

         File file_new = new File(myDir+"/text/", NEWFILENAME); 
         file_new.createNewFile(); 

         Log.i("file", file_new.toString()); 

         if (file_new.mkdirs()) { 
          FileOutputStream fos = new FileOutputStream(file_new); 

          fos.write(NEWENTRY.getBytes()); 
          fos.flush(); 
          fos.close(); 
         } 

        } catch (FileNotFoundException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 

        Intent textPass = new Intent("com.example.TEXTMENU"); 
        startActivity(textPass); 
       } 
      }); 

      } 


//That's for creating... then in other activity i'm reading 

      @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      // TODO Auto-generated method stub 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.text_menu); 

      btn = (Button) findViewById(R.id.newNote); 
      listfinal = (ListView) findViewById(R.id.listView); 

      btn.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        Intent textPass = new Intent("com.example.TEXTNEW"); 
        startActivity(textPass); 

       } 
      }); 

      listfinal.setOnItemClickListener(this); 

      File fileWithinMyDir = getApplicationContext().getFilesDir(); 


      loadbtn = (Button) findViewById(R.id.loadList); 

      loadbtn.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        File myDir = getFilesDir(); 

        File dir = new File(myDir + "/text/"); 

        String[] files = dir.list(); 

        //String[] files = getApplicationContext().fileList(); 
        List<String> list = new ArrayList<String>(); 

        for (int i =0; i < files.length; i++){ 
         list.add(files[i]); 
        } 
        ArrayAdapter<String> ad = new ArrayAdapter<String>(TextMenu.this, android.R.layout.simple_list_item_1, 
            android.R.id.text1, list); 

        listfinal.setAdapter(ad); 
       } 
      }); 
      } 
मेरी एंड्रॉयड manifiest में

मैं अनुमतियाँ

   <uses-sdk 
        android:minSdkVersion="5" 
        android:targetSdkVersion="15" /> 

       <uses-permission android:name="android.hardware.camera" /> 
       <uses-permission android:name="android.permission.INTERNET" /> 
       <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
       <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" /> 
+0

क्या आप लॉगकैट प्रदान कर सकते हैं? – 323go

+1

निर्देशिका बनाने का प्रयास करें जहां आप अपनी फ़ाइल को 'File.mkdirs()' कॉल – vasart

उत्तर

27

मैं भी यकीन है कि जो उदाहरण आप बात कर रहे हैं नहीं कर रहा हूँ, लेकिन मैं दो काम कर नमूने यहां जिनमें से उनमें से कम से कम एक सूट चाहिए आपकी ज़रूरतें। मैं एक X10 चल बिल्ड नंबर 2.1.A.0.435, एक एक्सपीरिया टी चल बिल्ड नंबर 7.0.A.1.303 और एक नेक्सस एस चल रहा बिल्ड नंबर JZO54K

उदाहरण 1

String filename = "myfile"; 
    String outputString = "Hello world!"; 

    try { 
     FileOutputStream outputStream = openFileOutput(filename, Context.MODE_PRIVATE); 
     outputStream.write(outputString.getBytes()); 
     outputStream.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    try { 
     FileInputStream inputStream = openFileInput(filename); 
     BufferedReader r = new BufferedReader(new InputStreamReader(inputStream)); 
     StringBuilder total = new StringBuilder(); 
     String line; 
     while ((line = r.readLine()) != null) { 
      total.append(line); 
     } 
     r.close(); 
     inputStream.close(); 
     Log.d("File", "File contents: " + total); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

उदाहरण 2 पर इन का परीक्षण किया

String filename = "mysecondfile"; 
    String outputString = "Hello world!"; 
    File myDir = getFilesDir(); 

    try { 
     File secondFile = new File(myDir + "/text/", filename); 
     if (secondFile.getParentFile().mkdirs()) { 
      secondFile.createNewFile(); 
      FileOutputStream fos = new FileOutputStream(secondFile); 

      fos.write(outputString.getBytes()); 
      fos.flush(); 
      fos.close(); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    try { 
     File secondInputFile = new File(myDir + "/text/", filename); 
     InputStream secondInputStream = new BufferedInputStream(new FileInputStream(secondInputFile)); 
     BufferedReader r = new BufferedReader(new InputStreamReader(secondInputStream)); 
     StringBuilder total = new StringBuilder(); 
     String line; 
     while ((line = r.readLine()) != null) { 
      total.append(line); 
     } 
     r.close(); 
     secondInputStream.close(); 
     Log.d("File", "File contents: " + total); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
+0

के साथ पहले रखना चाहते हैं चेक 'secondFile.getParentFile()। Mkdirs() 'क्या आप कृपया समझा सकते हैं? – yashhy

+3

यदि आप अभी भी किसी उत्तर में रुचि रखते हैं: यह कॉल सुनिश्चित करने के लिए है कि फ़ाइल का पथ मौजूद है, तो यदि आवश्यक हो तो यह निर्देशिका पदानुक्रम बना रहा है – prom85

+0

यदि कोई व्यक्ति अभी भी इस उत्तर में रुचि लेता है: जब API 23+ को लक्षित करते हैं, रनटाइम पर WRITE_EXTERNAL_STORAGE अनुमति का अनुरोध किया जाना चाहिए। अन्यथा "अनुमति अस्वीकृत" त्रुटि दिखाई देगी – Zoe

1

मुझे सैमसंग गैलेक्सी एस 7 के साथ "अनुमति अस्वीकार" मुद्दा था, भले ही मैंने मैनिफेस्ट फ़ाइल में पढ़ने और लिखने की अनुमति दी थी। मैंने फोन सेटिंग्स >> एप्लिकेशन >> [मेरे ऐप] पर जाकर इसे हल किया और अनुमति के तहत "स्टोरेज" की अनुमति दी। उसके बाद ठीक काम किया।

+2

** Pitfall: ** ऐप एपीआई 23 या उच्चतर लक्ष्य को लक्षित करते समय बाहरी संग्रहण अनुमति को रनटाइम पर अनुरोध किया जाना चाहिए। WRITE_EXTERNAL_STORAGE एक खतरनाक अनुमति है, जिसका अर्थ है कि इसका अनुरोध किया जाना है। – Zoe