【问题标题】:Define The constructor File(Uri)?定义构造函数 File(Uri)?
【发布时间】:2014-01-28 00:42:49
【问题描述】:

我制作了一个壁纸集应用程序,在我的设备 (Nexus 5) 上效果很好,但在 Galaxy TAB、Fame 甚至 Optimus 上它会给出 java.lang.OutOfMemory 错误。 有人告诉我实现 public Bitmap decodeAndResizeFile(File f) 但我明白了

这是我的 MainActivity 代码 包 app.technozed.cablewallpapershd;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.net.Uri;
 import android.os.Bundle;
 import android.app.Activity;
 import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
 import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

ImageView display;
int toPhone;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    System.gc();
    toPhone = R.drawable.wal1;
    display = (ImageView) findViewById(R.id.WPdisplay);
    ImageView image1 = (ImageView) findViewById(R.id.WPimg1);
    ImageView image2 = (ImageView) findViewById(R.id.WPimg2);
    ImageView image3 = (ImageView) findViewById(R.id.WPimg3);
    ImageView image4 = (ImageView) findViewById(R.id.WPimg4);
    ImageView image5 = (ImageView) findViewById(R.id.WPimg5);
    ImageView image6 = (ImageView) findViewById(R.id.WPimg6);
    ImageView image7 = (ImageView) findViewById(R.id.WPimg7);
    ImageView image8 = (ImageView) findViewById(R.id.WPimg8);
    ImageView image9 = (ImageView) findViewById(R.id.WPimg9);
    ImageView image10 = (ImageView) findViewById(R.id.WPimg10);
    Button setWall = (Button) findViewById(R.id.BsetWall);
    image1.setOnClickListener(this);
    image2.setOnClickListener(this);
    image3.setOnClickListener(this);
    image4.setOnClickListener(this);
    image5.setOnClickListener(this);
    image6.setOnClickListener(this);
    image7.setOnClickListener(this);
    image8.setOnClickListener(this);
    image9.setOnClickListener(this);
    image10.setOnClickListener(this);       setWall.setOnClickListener(this);
    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

我在这里实现了它:

public Bitmap decodeAndResizeFile(File f) {

    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        // The new size we want to scale to
        final int REQUIRED_SIZE = 70;

        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE
                    || height_tmp / 2 < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
    }
    return null;
}

@Override
public void onClick(View v) {
    switch (v.getId()){
    case R.id.WPimg1:
         display.setImageResource(R.drawable.wal1);
         toPhone = R.drawable.wal1;

我在这里检索它:

文件文件=新文件(Uri.parse(String.valueOf(toPhone)));

在上面的行我得到 构造函数 File(Uri) 未定义错误

Bitmap bmpp = decodeAndResizeFile(file);
display.setImageBitmap(bmpp);

         break;
    case R.id.WPimg2:
         display.setImageResource(R.drawable.wal2);
         toPhone = R.drawable.wal2;
         break;
    case R.id.WPimg3:
         display.setImageResource(R.drawable.wal3);
         toPhone = R.drawable.wal3;
         break;
    case R.id.WPimg4:
         display.setImageResource(R.drawable.wal4);
         toPhone = R.drawable.wal4;
         break;
    case R.id.WPimg5:
         display.setImageResource(R.drawable.wal5);
         toPhone = R.drawable.wal5;
         break;
    case R.id.WPimg6:
         display.setImageResource(R.drawable.wal6);
         toPhone = R.drawable.wal6;
         break;
    case R.id.WPimg7:
         display.setImageResource(R.drawable.wal7);
         toPhone = R.drawable.wal7;
         break;
    case R.id.WPimg8:
         display.setImageResource(R.drawable.wal8);
         toPhone = R.drawable.wal8;
         break;
    case R.id.WPimg9:
         display.setImageResource(R.drawable.wal9);
         toPhone = R.drawable.wal9;
         break;
    case R.id.WPimg10:
         display.setImageResource(R.drawable.wal10);
         toPhone = R.drawable.wal10;
         break;
    case R.id.BsetWall:
         try{
               WallpaperManager.getInstance(getApplicationContext()).setResource(toPhone);
             Toast.makeText(getApplicationContext(), "Wallpaper was set!", Toast.LENGTH_SHORT).show();
         } catch(IOException e) {
             e.printStackTrace();
             Toast.makeText(getApplicationContext(), "No privileges!", Toast.LENGTH_SHORT).show();
         }
         break;
    }
    }
    }

我该如何定义它?我做错了什么?

【问题讨论】:

    标签: android bitmap out-of-memory


    【解决方案1】:

    文件类在其构造函数中采用 java.net.URI,您尝试为其提供 android.net.Uri。

    据我所知,这就是问题所在。

    您正在使用的 File 类: http://docs.oracle.com/javase/7/docs/api/java/io/File.html

    采用这个 URI 类:http://docs.oracle.com/javase/7/docs/api/java/net/URI.html

    你正在传递这个:http://developer.android.com/reference/java/net/URI.html

    【讨论】:

    • 我这样定义 Uri:Uri selectedImage = data.getData(); String[] filePathColumn = { MediaStore.Images.Media.DATA };光标 cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]);字符串图片路径 = cursor.getString(columnIndex); File file = new File(picturePath);// Bitmap bmpp = decodeAndResizeFile(file); 但是data.getData(); 不想工作说:数据无法解析
    【解决方案2】:

    这个错误我遇到过很多次了...终于想出了一个常用的设置图像视图的方法...下面是代码片段...

    public static void setImageView(ImageView imageView, String path) {
            Log.i("Atul Dravid", path);
            if (path.startsWith("Photo") || path.startsWith("null"))
                return;
            path = path.startsWith("/") ? path : path.substring(6);
            Bitmap picture = (Bitmap) BitmapFactory.decodeFile(path);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            picture = Bitmap.createScaledBitmap(picture, 200, 200, false);
            picture.compress(Bitmap.CompressFormat.JPEG, 20, stream);
            byte[] bitmapArray = stream.toByteArray();
            picture = (Bitmap) BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
            //imageView.setImageURI(fileUri);
            imageView.setImageBitmap(picture);
            imageView.setContentDescription(path);
    
    
        }
    

    您可能需要修改上面的代码以满足您对图片分辨率的要求。

    希望这会有所帮助。

    编辑

    技术上唯一的区别是我还在我的代码中添加了缩放...尝试在您的代码片段中引入它...我没有发现图片质量有任何明显的下降...

    【讨论】:

    • 我该如何称呼它? case R.id.WPimg4: display.setImageResource(R.drawable.wal4); toPhone = R.drawable.wal4; break;
    • 我想缩放它,但它应该适合我的手机屏幕,我该怎么做?
    【解决方案3】:

    您的编译错误是因为“文件”类没有将 Uri 对象作为参数的构造函数。要使用 Uri 构造 File 对象,通常会在 URI 上调用 getPath() 以将其路径作为字符串返回:

    File file=new File(Uri.parse(String.valueOf(toPhone)).getPath());
    

    但这仍然不能解决您的问题,因为“toPhone”是一个整数,表示项目中资源的 id(在本例中为可绘制对象)。您不能以这种方式从资源 ID 生成 URI 或文件路径。但是,在这种情况下,您可以直接使用资源 ID 而不是文件。

    更改您的解码方法以接受资源标识符(即整数):

    public Bitmap decodeAndResizeFile(int resID) {
    

    ...并使用 BitmapFactory 的 decodeResource 方法而不是 decodeStream(使用您的 Activity 的上下文):

    Bitmap bmp= BitmapFactory.decodeResource(context.getResources(), resID, o);
    

    然后你可以调用它:

    Bitmap bmpp = decodeAndResizeFile(toPhone);
    

    获取上下文的最简单方法是在 Activity 类中将其声明为变量:

    private Context context;
    

    ...并在 onCreate() 中设置:

    context = this;
    

    然后,您可以在活动中的任何需要使用它。

    【讨论】:

    • 我应该定义它吗:Context context; public myClass(Context context) { this.context = context; }
    • 请看我上面的编辑。最简单的方法是在 onCreate 中存储对活动上下文的引用。
    • } catch (FileNotFoundException e) { } return null; } 说 FileNotFoundException 的无法到达的 catch 块。 try 语句体中永远不会抛出此异常 我可以只留下} catch (Exception e) { 吗?
    • 是的。如果您不再使用文件,则不会再发生该异常。所以你可以删除 try/catch。
    • case R.id.WPimg1: display.setImageResource(R.drawable.wal1); toPhone = R.drawable.wal1; File file=new File(Uri.parse(String.valueOf(toPhone)).getPath()); Bitmap bmpp = decodeAndResizeFile(toPhone); break; mmmm .. 我无法使用 bmpp 超过 1 次,我可以像这样使用它吗这个:bmpp = decodeAndResizeFile(toPhone); ?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-13
    相关资源
    最近更新 更多