简体   繁体   English

在Xamarin Android上拍照后如何将照片发送到drawable文件夹?

[英]How to send a photo to the drawable folder after take the photo on Xamarin Android?

I'd create a xamarin android project on visual studio who take a photo from the camera, but I have a problem, I can take the photo but I don't know how I can make to the project after take the photo send the photo to the drawable folder of the project, how can I make this? 我会在Visual Studio上创建一个从相机拍摄照片的xamarin android项目,但是我有一个问题,我可以拍摄照片,但是在拍摄照片后发送照片我不知道如何制作该项目到项目的可绘制文件夹中,我该如何做?

The XAML Code XAML代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:background="#c1cdcd"
    android:layout_weight="9"
    android:id="@+id/imgvw1" />
<Button
    android:text="Take a Photo"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btnCamera" />
</LinearLayout>

The MainAcivity MainAcivity

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Content;
using Android.Provider;
using Android.Runtime;
using Android.Graphics;
using System;

namespace Droid_Camera
{
[Activity(Label = "Droid_Camera", MainLauncher = true, Icon = 
"@drawable/icon")]
public class MainActivity : Activity
{
    ImageView imgView1;
    Button btnCamera;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);
        btnCamera = FindViewById<Button>(Resource.Id.btnCamera);
        imgView1 = FindViewById<ImageView>(Resource.Id.imgvw1);

        btnCamera.Click += BtnCamera_Click;

    }

    protected override void OnActivityResult(int requestCode, 
    [GeneratedEnum] Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        Bitmap bitmap = (Bitmap)data.Extras.Get("data");
        imgView1.SetImageBitmap(bitmap);
    }

    private void BtnCamera_Click(object sender, System.EventArgs e)
    {
        Intent intent = new Intent(MediaStore.ActionImageCapture);
        StartActivityForResult(intent, 0);
    }
}

You can't do this unfortunately, the Drawables folder is a compile time resource that you cannot add to in run time. 不幸的是,您不能做到这一点, Drawables文件夹是一个编译时资源,您无法在运行时添加到该资源。 Otherwise, the R.java file would not be valid. 否则, R.java文件将无效。

As the comments and answers mentioned you cannot add a file at runtime to your Drawable . 正如评论和答案中提到的那样,您不能在运行时将文件添加到Drawable As a matter of fact you cannot add any file to the bundle itself in runtime. 实际上,您不能在运行时将任何文件添加到捆绑包本身。

What you can do is add your file to the filesystem. 您可以做的就是将文件添加到文件系统中。 If you want the file to be 'visible' only to your app use getFilesDir() . 如果您希望文件仅对您的应用程序“可见”,请使用getFilesDir() This returns a folder path present in your application sandbox. 这将返回应用程序沙箱中存在的文件夹路径。 If it's meant to be a sort of temporary file and is less than 1MB use getCacheDir() 如果它是一种临时文件并且小于1MB,请使用getCacheDir()

If the file is meant to be publicly visible use getExternalStoragePublicDirectory() but for this you would need WRITE_EXTERNAL_STORAGE permission. 如果该文件是公开可见的,请使用getExternalStoragePublicDirectory()但为此,您需要WRITE_EXTERNAL_STORAGE权限。

Check out the documentation for details 查看文档以了解详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM