从xamarin,android中的另一个应用程序接收数据

时间:2018-03-17 21:14:51

标签: android xamarin xamarin.android

我使用xamarin.android创建了一个可以播放图像的应用。我希望其他应用程序可以将图像分享到我的应用程序,我尝试了以下操作,但抛出异常。

这是Android清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
          package="ReceiveDataFromApp.ReceiveDataFromApp" 
          android:versionCode="1" 
          android:versionName="1.0">
  <uses-sdk android:minSdkVersion="21" />
  <application android:allowBackup="true" android:label="@string/app_name">
    <activity
       android:name=".MainActivity"
       android:label="@string/app_name">
      <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="image/*" />
      </intent-filter>
    </activity>
  </application>
</manifest>

Main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imageView"
       />
</LinearLayout>

MainActivity.cs

namespace ReceiveDataFromApp
{
    [Activity(Label = "ReceiveDataFromApp", MainLauncher = true)]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);`

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            ImageView imageView = (ImageView)FindViewById(Resource.Id.imageView);

            if (Intent.Data != null)
            {
                try
                {
                    var uri = Intent.Data;
                    var iis = ContentResolver.OpenInputStream(uri);
                    imageView.SetImageBitmap(BitmapFactory.DecodeStream(iis));
                }
                catch (Exception e)
                {
                    Toast.MakeText(this, e.Message, ToastLength.Long).Show();
                }
            }
        }
    }
}

例外:

Java.Lang.RuntimeException: Unable to instantiate activity ComponentInfo{ReceiveDataFromApp.ReceiveDataFromApp/ReceiveDataFromApp.ReceiveDataFromApp.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "ReceiveDataFromApp.ReceiveDataFromApp.MainActivity" on path: DexPathList[[zip file "/data/app/ReceiveDataFromApp.ReceiveDataFromApp-1/base.apk"],nativeLibraryDirectories=[/data/app/ReceiveDataFromApp.ReceiveDataFromApp-1/lib/arm, /data/app/ReceiveDataFromApp.ReceiveDataFromApp-1/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]

纠正我做错的事。

3 个答案:

答案 0 :(得分:3)

  

ClassNotFoundException:找不到类&#34; ReceiveDataFromApp.ReceiveDataFromApp.MainActivity&#34;

默认情况下,Xamarin创建基于MD5的Java类名,以避免在生成的Java包装器中发生名称冲突。

要对Java类名进行硬编码,请使用Name

中的ActivityAttribute参数
[Activity(Name = "ReceiveDataFromApp.ReceiveDataFromApp.MainActivty", Label = "ReceiveDataFromApp", MainLauncher = true)]
public class MainActivity : Activity
~~~~

答案 1 :(得分:1)

这是问题的解决方案,感谢@sushihangover

<activity    
   android :name="ReceiveDataFromApp.ReceiveDataFromApp.MainActivity"
   android:label="@string/app_name">

在清单文件中,您必须添加:

this

答案 2 :(得分:0)

Android操作系统使用内容提供商来方便访问共享数据,如图像,媒体文件,联系人等。您可以找到如何使用它here