为什么我在 Android Studio 中收到此错误 "Expected resource of type raw"?

Why am I getting this error "Expected resource of type raw" in Android Studio?

我试图通过使用按钮设置默认墙纸,但由于某种原因,当我在 OnCreate 方法中设置 InputStream 时,出现此错误 "expected resource of type raw"。我正在引用可绘制文件夹并使用可绘制文件夹中的 icon.png 图像。我正在学习 NewBoston 系列中的教程,它似乎对 Travis 来说工作得很好,但出于某种原因,我的教程在 Android Studio 中不起作用。可能是什么错误?谢谢

Camera.java:

package com.example.user.cameraapplication;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Switch;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by user on 16-08-2015.
 */
public class Camera extends Activity implements View.OnClickListener{

    ImageView iv;
    Button b1,b2;
    ImageButton img;
    Intent i;
    final static  int cameractivity = 0;
    Bitmap b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo);
        inititalize();
        InputStream is = getResources().openRawResource(R.drawable.icon);
        b = BitmapFactory.decodeStream(is);
    }

    private void inititalize() {
        iv = (ImageView)findViewById(R.id.iView1);
        img = (ImageButton)findViewById(R.id.imgbtn);
        b1 = (Button)findViewById(R.id.btn1);
        b2 = (Button)findViewById(R.id.btn2);

        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.btn1:
                try {
                    getApplicationContext().setWallpaper(b);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.imgbtn:
                i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(i,cameractivity);

                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK)
        {
            Bundle extras = data.getExtras();
            b = (Bitmap)extras.get("data");
           iv.setImageBitmap(b);

        }
    }
}

图片:

发生错误是因为Android Studio 需要原始类型的资源文件。

解决方案 1:

在名为“raw”的“res”文件夹中创建一个新文件夹,并将图标放在那里。 raw 文件夹应该包含您应用程序的所有媒体文件。

然后替换

InputStream is = getResources().openRawResource(R.drawable.icon);

InputStream is = getResources().openRawResource(R.raw.icon);

解决方案 2:

另一种解决方案是这样做。这不需要您创建原始文件夹:

InputStream is = getResources().openRawResource(+ R.drawable.icon);

替换

InputStream is = getResources().openRawResource(R.drawable.icon);

InputStream is = getResources().openRawResource(+ R.drawable.icon);

让我们看看 openRawResource

的文档

Open a data stream for reading a raw resource. This can only be used with resources whose value is the name of an asset files -- that is, it can be used to open drawable, sound, and raw resources; it will fail on string and color resources.

所以解决方案 - 保持原样,我。 e.

InputStream is = getResources().openRawResource(R.drawable.icon);

Android Studio 中的检查是错误的。您可以添加

@SuppressWarnings("ResourceType")

隐藏错误。

备注

使用+ R.drawable.icon只是愚弄了检查,因为它无法再确定它是哪种资源。