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

标签 java android eclipse android-studio inputstream

我试图通过使用按钮设置默认墙纸,但由于某种原因,当我在 OnCreate 方法中设置 InputStream 时,我收到此错误“原始类型的预期资源”。我正在引用 drawable 文件夹并使用 drawable 文件夹中的 icon.png 图像。我正在学习 NewBoston 系列中的教程,它似乎适用于 Travis,但出于某种原因,我的教程在 Android Studio 中不起作用。可能是什么错误?谢谢

相机.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);

        }
    }
}

图片:

Image

最佳答案

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

解决方案一:

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

然后替换

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

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

解决方案 2:

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

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

关于java - 为什么我在 Android Studio 中收到此错误 "Expected resource of type raw"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32038476/

相关文章:

Java HashMap 初始化?

java - 正则表达式用于屏蔽 Java 中除前两个和最后两个字符之外的字符

android - 将日期/时间(以毫秒为单位)格式化为 Android 中的本地字符串

android - 可以将 sharedPrefrence 与 Coroutine kotlin 一起使用吗

java - 如何使用正则表达式来匹配 b1、b2、... 到 b15 等表达式,但仅此而已?

java - 为什么 Java 枚举不能是最终的?

android - 什么是 Android Studio 中的向后兼容性?

eclipse - Gradle 多项目最佳实践

eclipse - 使用eclipse在tomcat上调试GWT

java - 夏令时和java