मुझे एंड्रॉइड के साझा भंडारण में एक फ़ाइल को सहेजने की ज़रूरत है। मुझे यह लिंक मिला => https://developer.android.com /प्रशिक्षण/डेटा-भंडारण/साझा/दस्तावेज़-फ़ाइलें
मैं निर्भरता सेवा का उपयोग कर रहा हूं और मैं वांछित स्थान पर फ़ाइल को सफलतापूर्वक सहेजने में सक्षम हूं। लेकिन मैं केवल एक खाली फाइल बनाने में सक्षम हूं। मुझे उस फ़ाइल में कुछ सामग्री लिखनी है। असल में मैंने कुछ घंटे पहले एंड्रॉइड टैग के साथ एक धागा बनाया और समाधान मिला जहां मुझे ऑनएक्टिविटी रिसेट विधि को ओवरराइड करना पड़ा और इरादा डेटा प्राप्त करना पड़ा। अब मैंने इसे कर लिया है और मैं इरादा डेटा प्राप्त करने में सक्षम हूं। लेकिन अब मुझे नहीं पता कि मुझे इरादे से कौन सा पथ चुनना चाहिए और चुने हुए पथ का उपयोग करके फ़ाइल को कैसे खोलना है और उस चुनी हुई फ़ाइल में सामग्री कैसे लिखना है। कोई भी android + xamarin विशेषज्ञ मेरी मदद करने में सक्षम होना चाहिए।
लेखन सेवा को लागू करने के लिए Android प्लेटफ़ॉर्म कोड:
Activity _activity;
private static int CREATE_FILE = 6835;
public WriteFileService()
{
_activity = CrossCurrentActivity.Current.Activity;
}
void IWriteService.WriteFile(string Content)
{
Intent intent = new Intent(Intent.ActionCreateDocument);
intent.AddCategory(Intent.CategoryOpenable);
intent.SetType("application/txt");
intent.PutExtra(Intent.ExtraTitle, "Invoice.txt");
_activity.StartActivityForResult(intent, CREATE_FILE);
}
ओवरराइड OnActivityResultMethod:
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
Toast.MakeText(Application.Context, "requestCode is " + requestCode, ToastLength.Short).Show();
if (requestCode == 6835)
{
if (data != null)
{
Toast.MakeText(Application.Context,
data.GetType().ToString(),
ToastLength.Short).Show();
}
}
base.OnActivityResult(requestCode, resultCode, data);
}
यह OnActivityResult से आशय डेटा की स्क्रीन है
2 जवाब
किसी स्ट्रीम को फ़ाइल में सहेजने के लिए अपने Android प्रोजेक्ट में इसका उपयोग करें:
public async Task SaveAndView(string fileName, String contentType, MemoryStream stream)
{
try
{
string root = null;
//Get the root path in android device.
if (Android.OS.Environment.IsExternalStorageEmulated)
{
root = Android.OS.Environment.ExternalStorageDirectory.ToString();
}
else
root = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
//Create directory and file
Java.IO.File myDir = new Java.IO.File(root + "/meusarquivos");
myDir.Mkdir();
Java.IO.File file = new Java.IO.File(myDir, fileName);
//Remove if the file exists
if (file.Exists()) file.Delete();
//Write the stream into the file
FileOutputStream outs = new FileOutputStream(file);
outs.Write(stream.ToArray());
outs.Flush();
outs.Close();
}
catch (Exception ex)
{
PostLog.AppCenterLogExcecao(ex, new Dictionary<string, string> { { "origem", "OrderViewModel - 159" } });
}
}
और आपके साझा कोड में:
await DependencyService.Get<ISave>().SaveAndView(OrderId.ToString() + ".pdf", "application/pdf", stream);
कोड का उपयोग करने से पहले अनुमति मांगना सुनिश्चित करें।
फ़ाइल को सहेजने के लिए। ive ने इस कोड का इस्तेमाल किया
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
if (requestCode == 6835)
{
if (data != null)
{
Android.Net.Uri uri = Android.Net.Uri.Parse(data.DataString);
var stream = ContentResolver.OpenOutputStream(uri, "w");
byte[] byteArray = Encoding.UTF8.GetBytes("Hi Thameem. I am your file.");
MemoryStream stream1 = new MemoryStream(byteArray);
stream1.CopyTo(stream);
stream.Flush();
stream.Close();
Toast.MakeText(Application.Context,
"The file has been exported successfully",
ToastLength.Short).Show();
}
}
base.OnActivityResult(requestCode, resultCode, data);
}
संबंधित सवाल
जुड़े हुए प्रश्न
नए सवाल
xamarin.forms
Xamarin.Forms एक Microsoft उत्पाद है जो एक ही साझा C # कोडबेस से एंड्रॉइड, iOS, विंडोज और अन्य के लिए देशी, क्रॉस-प्लेटफ़ॉर्म ऐप के निर्माण की अनुमति देता है।