1

在底部遇到问题,我正在尝试创建一个案例以将图像保存到 SD 卡但它不起作用,我不知道为什么。我认为这与 getRawResource 有关,但仍不确定。

谢谢你的帮助!!!

    package com.nk_apps.random;

import java.io.InputStream;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class Main extends Activity {

    private Gallery gallery;
    private ImageView imgView;

    private Integer[] imgId = {
            R.drawable.image1,
            R.drawable.image2,
            R.drawable.image3,
            R.drawable.image4,
            R.drawable.image5,
            R.drawable.image6,
    };

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

        imgView = (ImageView)findViewById(R.id.imageView01);    
        imgView.setImageResource(imgId[0]);

         gallery = (Gallery) findViewById(R.id.examplegallery);
         gallery.setAdapter(new AddImgAdp(this));

         gallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
                imgView.setImageResource(imgId[position]); 
            }
        });

    }

    public class AddImgAdp extends BaseAdapter {
        int GalItemBg;
        private Context cont;

        public AddImgAdp(Context c) {
            cont = c;
            TypedArray typArray = obtainStyledAttributes(R.styleable.Gallery1);
            GalItemBg = typArray.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
            typArray.recycle();
        }

        public int getCount() {
            return imgId.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imgView = new ImageView(cont);

            imgView.setImageResource(imgId[position]);
            imgView.setLayoutParams(new Gallery.LayoutParams(170, 270));
            imgView.setScaleType(ImageView.ScaleType.FIT_XY);
            imgView.setBackgroundResource(GalItemBg);

            return imgView;}

            public void onClick(View v) {
                // TODO Auto-generated method stub

            switch (v.getId()){
            case R.id.bsaveImage:
                InputStream inps = getResources().openRawResource(GalItemBg);
                Bitmap bmp = BitmapFactory.decodeStream(inps);
                try{
                    MediaStore.Images.Media.insertImage(getContentResolver(), bmp, "Title" , "Hip-Hop Wallpapers");
                    Toast saveToast = Toast.makeText(Main.this, "The image has been saved.", Toast.LENGTH_SHORT);
                    saveToast.show();
                }catch(Exception e){
                e.printStackTrace();
                }
                break;
        }
    }

} }
4

1 回答 1

2

您是否确认您对 sdcard 有写权限?(这是 WRITE_EXTERNAL_STORAGE)

此外,您应该确保查看正确的挂载点以找到映像(例如,对于某些设备,可以是“内部 sdcard”)。

于 2012-09-05T20:42:38.647 回答