【问题标题】:Get id From imageView and Set image as Wallpaper Onclick从 imageView 获取 id 并将图像设置为墙纸 Onclick
【发布时间】:2014-03-23 07:28:11
【问题描述】:

我的项目中有一个 Imageview 和一个 Button,我希望该按钮将图像设置为主屏幕背景,但出现错误“imageView”无法在“Bitmap bitmap=((BitmapDrawable)imageView.getDrawable ()).getBitmap();"

package com.example.androidhive;

import java.io.IOException;

import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

 public class FullImageActivity extends Activity {

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.full_image);

    // get intent data
    Intent i = getIntent();

    // Selected image id
    int position = i.getExtras().getInt("id");
    ImageAdapter imageAdapter = new ImageAdapter(this);

    ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
    imageView.setImageResource(imageAdapter.mThumbIds[position]);
    }


 public void setaswall(View view) { // SET AS WALLPAPER BUTTON
     // TODO Auto-generated method stub
     WallpaperManager myWallpaperManager
      = WallpaperManager.getInstance(getApplicationContext());
     try {
      Bitmap bitmap=((BitmapDrawable)imageView.getDrawable()).getBitmap();
      if(bitmap!=null)
      myWallpaperManager.setBitmap(bitmap);
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
  }
 };
 public void setaswall(View view) { // SET AS WALLPAPER BUTTON
     WallpaperManager myWallpaperManager 
     = WallpaperManager.getInstance(getApplicationContext());
     };
  }

【问题讨论】:

    标签: android button imageview


    【解决方案1】:

    您必须将您的 imageView 声明为全局的,因此当您在 setaswall(View view) 上使用它时,它就在范围内,而不是仅在 onCreate() 中创建它

    像这样:

    public class FullImageActivity extends Activity {
    
    private ImageView _imageView;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    

    然后,在 setaswall(查看视图)上:

        Bitmap bitmap=((BitmapDrawable)_imageView.getDrawable()).getBitmap();
            if(bitmap!=null)
                myWallpaperManager.setBitmap(bitmap);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-27
      • 1970-01-01
      相关资源
      最近更新 更多