2013-03-18 14 views
5

आज मुझे एक मुश्किल चीज़ से निपटना है।एंड्रॉइड स्थानीय स्टोरेज में कैमरा चित्र सहेजें

मैं कैमरा शुरू करता हूं और इसे ले जाने वाली छवि को सीधे अपने आंतरिक भंडारण में सहेजने के बिना सहेजना चाहता हूं।

File targetDir = new File(getApplicationContext().getFilesDir()+File.separator+"PROJECTMAIN"+File.separator+"SUBFORDER"); 
    targetDir.mkdirs(); //create the folder if they don't exist 

    File externalFile = new File(targetDir, "picturename.jpg"); 
    Uri imageURI = Uri.fromFile(externalFile); 

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageURI); 
    startActivityForResult(takePictureIntent, actionCode); 

ऐसा लगता है कि अगर मैं उन्हें आंतरिक भंडारण में सीधे बचाने की कोशिश, कैमरा "ठीक है" बटन पर मेरे क्लिक पर ध्यान नहीं देता के बाद मैं तस्वीर लेने। मुझे लगता है कि "आंतरिक" यूआरआई में कुछ गड़बड़ है, क्योंकि अगर मैं का उपयोग getApplicationContext().getFilesDir() के बजाय अतिरिक्त_आउटपुट के लिए करता हूं, तो सब कुछ ठीक काम करता है, लेकिन फिर मुझे फ़ाइल को बाद में आंतरिक भंडारण में स्थानांतरित करना होता है (आंदोलन प्रक्रिया ठीक है "getAplicationContext() .getFilesDir() ")

जब मैं एक तस्वीर लेता हूं तो कैमरा कुछ भी नहीं करता है और आंतरिक यूआरआई के साथ जारी रखने के लिए ठीक बटन दबाता है ... मुझे विश्वास नहीं है कि भंडारण के साथ यह मुश्किल है एंड्रॉइड में

कोई विचार? शायद कैमरा सिर्फ बाहरी भंडारण में चित्रों को सहेजने की अनुमति देता है?

+0

पोस्ट http://stackoverflow.com/questions/12193607/how-to-save-capture-image-in-sdcard –

उत्तर

3

कोशिश निम्नलिखित कोड

File dir= context.getDir("dirname", Context.MODE_PRIVATE); //Creates Dir inside internal memory 
File file= new File(dir, "filename"); //It has directory details and file name 
FileOutputStream fos = new FileOutputStream(file); 
+0

देखें यदि यह उत्तर आपकी मदद करता है, तो कृपया इस उत्तर को स्वीकार करें .. – Aju

0

एंड्रॉयड 7.0 उपयोग इस कोड के उच्च संस्करण के लिए,

<application 
    ...> 
    <activity> 
    .... 
    </activity> 
    <provider 
     android:name="android.support.v4.content.FileProvider" 
     android:authorities="com.your.package.fileProvider" 
     android:grantUriPermissions="true" 
     android:exported="false"> 
     <meta-data 
      android:name="android.support.FILE_PROVIDER_PATHS" 
      android:resource="@xml/file_paths" /> 
    </provider> 
    </application> 

अब xml संसाधन फ़ोल्डर में एक फ़ाइल बनाने,

<?xml version="1.0" encoding="utf-8"?> 
<paths> 
    <external-path path="Android/data/com.your.package/" name="files_root" /> 
    <external-path path="." name="external_storage_root" /> 
</paths> 

तो जब भी उपयोग कैमरे के इरादे के लिए इसका उपयोग करें,

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
      intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
      Uri contentUri = FileProvider.getUriForFile(getContext(), "com.your.package.fileProvider", newFile); 
      intent.setDataAndType(contentUri, type); 
     } else { 
      intent.setDataAndType(Uri.fromFile(newFile), type); 
     } 
संबंधित मुद्दे