为什么我会收到此错误“原始类型的预期资源"?在 Android Studio 中? [英] Why am I getting this error "Expected resource of type raw" in Android Studio?

查看:20
本文介绍了为什么我会收到此错误“原始类型的预期资源"?在 Android Studio 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was trying to set the default wallpaper by using a button but for some reason when i set the InputStream in the OnCreate Method, i get this error "expected resource of type raw". I am referencing the drawable folder and using the icon.png image which is in the drawable folder. I was following the tutorials in the NewBoston Series and it seems to work fine for Travis but for some reason mine doesnt work in Android Studio. What could be the error? Thanks

Camera.java:

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 需要一个类型为 raw 的资源文件.

The error occurred because Android Studio expected a resource file of type raw.

在您的res"文件夹中创建一个新文件夹名为raw"的文件夹,然后将您的图标放在那里.raw 文件夹应包含您应用的所有媒体文件.

Create a new folder in your "res" folder called "raw", and put your icon there. The raw folder should contain all your media files of your app.

然后替换

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

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


解决方案 2:

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


Solution 2:

Another solution is to do it like this. This doesn't require you to create a raw folder:

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

这篇关于为什么我会收到此错误“原始类型的预期资源"?在 Android Studio 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆