简体   繁体   English

定义构造函数File(Uri)?

[英]Define The constructor File(Uri)?

i made a wallpaper set application, on my device (Nexus 5) it works great but on Galaxy TAB, Fame or even on Optimus it gives java.lang.OutOfMemory error. 我制作了墙纸应用程序,在我的设备(Nexus 5)上运行良好,但在Galaxy TAB,Fame甚至在Optimus上,它给出了java.lang.OutOfMemory错误。 someone told me to implement public Bitmap decodeAndResizeFile(File f) but i get 有人告诉我实现公共位图的decodeAndResizeFile(File f),但我得到了

here is my MainActivity code package app.technozed.cablewallpapershd; 这是我的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;
}

here i implemented it: 在这里我实现了它:

public Bitmap decodeAndResizeFile(File f) { 公开位图的decodeAndResizeFile(文件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;

here i retrive it: 在这里我检索它:

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

on the line above i get The constructor File(Uri) is undefined error 在上面的行上,我得到了构造函数File(Uri)是undefined错误

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;
    }
    }
    }

how can i define it? 我该如何定义呢? what i'm doing wrong? 我做错了什么?

The file class takes a java.net.URI in its constructor, you're trying to give it an android.net.Uri. 文件类在其构造函数中使用java.net.URI,您正在尝试为其提供android.net.Uri。

That's what's wrong as far as I can see. 就我所知这就是错误的。

The File class you're using: http://docs.oracle.com/javase/7/docs/api/java/io/File.html 您正在使用的File类: http : //docs.oracle.com/javase/7/docs/api/java/io/File.html

Takes this URI class: http://docs.oracle.com/javase/7/docs/api/java/net/URI.html 采用此URI类: http : //docs.oracle.com/javase/7/docs/api/java/net/URI.html

You're passing this: http://developer.android.com/reference/java/net/URI.html 您正在通过此: http : //developer.android.com/reference/java/net/URI.html

I had encountered this error so many times... Finally I came out with one common method to set the image view... Following is the code fragment... 我已经多次遇到此错误...最后我想到了一种设置图像视图的常用方法...以下是代码片段...

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);


    }

You may want to modify above code for your requirement of resolution of the picture. 您可能需要修改以上代码,以符合图像分辨率的要求。

Hope this helps. 希望这可以帮助。

EDIT 编辑

Technically only difference is that I also added Scaling in my code... Try introducing that in your code fragment... I have not found any noticeable degradation in quality of the picture... 从技术上来说唯一的区别是,我还在代码中添加了Scaling ...尝试在您的代码片段中引入...在图像质量方面我没有发现任何明显的下降...

Your compile error is because there is no constructor for the class 'File' that takes a Uri object as a parameter. 您的编译错误是因为没有为类“ File”使用Uri对象作为参数的构造函数。 To construct a File object using an Uri, you would do usually call getPath() on the URI to return its path as a String: 要使用Uri构造File对象,通常会在URI上调用getPath()以将其路径作为String返回:

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

However that will still not solve your problem because 'toPhone' is an integer representing the id of a resource in your project (in this case a drawable). 但是,这仍然不能解决您的问题,因为“ toPhone”是一个整数,表示项目中资源的ID(在本例中为drawable)。 You cannot generate a URI or file path from a resource id in that manner. 您不能以这种方式从资源ID生成URI或文件路径。 However in this case, you can work directly with the resource id instead of a file anyway. 但是,在这种情况下,无论如何您都可以直接使用资源ID而不是文件。

Change your decode method to accept a resource identifier (ie an integer): 更改您的解码方法以接受资源标识符(即整数):

public Bitmap decodeAndResizeFile(int resID) {

...and use the decodeResource method of BitmapFactory instead of decodeStream (using your Activity's context): ...并使用BitmapFactory的encodeResource方法而不是解码流(使用您的Activity的上下文):

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

Then you can call it with: 然后,您可以使用以下命令调用它:

Bitmap bmpp = decodeAndResizeFile(toPhone);

Easiest way to get the context is to declare it as a variable in your Activity class: 获取上下文的最简单方法是在Activity类中将其声明为变量:

private Context context;

...and set it in onCreate(): ...并在onCreate()中进行设置:

context = this;

You can then use it wherever you need to in your activity. 然后,您可以在活动中的任何地方使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM