我的图片没有显示在SD卡上

时间:2010-08-04 04:06:55

标签: android sd-card

我想尝试将图像存储到SD卡中并显示出来是否有效。但是,我的图像无法显示。请帮我查一下我的问题。

public class SDCard_Image {

//read drawable files 
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.mood_1);

//insert into sd card(?) 
 public static boolean StoreByteImage(Context mContext, byte[] imageData,
            int quality, String expName) { 

        File sdImageMainDirectory = new File("/sdcard/myImages");

        FileOutputStream fileOutputStream = null;       
        FileOutputStream fileOutputStream2 = null;
        FileOutputStream fileOutputStream3 = null;
        FileOutputStream fileOutputStream4 = null;
        FileOutputStream fileOutputStream5 = null;
        FileOutputStream fileOutputStream6 = null;
        FileOutputStream fileOutputStream7 = null;
        FileOutputStream fileOutputStream8 = null;
        FileOutputStream fileOutputStream9 = null;
        FileOutputStream fileOutputStream10 = null;
        FileOutputStream fileOutputStream11 = null;
        FileOutputStream fileOutputStream12 = null;
        FileOutputStream fileOutputStream13 = null;
        FileOutputStream fileOutputStream14 = null;
        FileOutputStream fileOutputStream15 = null;
        FileOutputStream fileOutputStream16 = null;

        String mood_1 = null;
        String mood_2 = null; 
        String mood_3 = null;
        String mood_4 = null; 
        String mood_5 = null;
        String mood_6 = null; 
        String mood_7 = null;
        String mood_8 = null; 
        String mood_9 = null; 
        String mood_10 = null; 
        String mood_11 = null; 
        String mood_12 = null; 
        String mood_13 = null; 
        String mood_14 = null; 
        String mood_15 = null; 
        String mood_16 = null; 

        try {

            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inSampleSize = 5;

            Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0,
                    imageData.length,options);

            fileOutputStream = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_1 + ".jpg");
            fileOutputStream2 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_2 + ".jpg");
            fileOutputStream3 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_3 + ".jpg");
            fileOutputStream4 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_4 + ".jpg");
            fileOutputStream5 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_5 + ".jpg");
            fileOutputStream6 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_6 + ".jpg");
            fileOutputStream7 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_7 + ".jpg");
            fileOutputStream8 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_8 + ".jpg");
            fileOutputStream9 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_9 + ".jpg");
            fileOutputStream10 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_10 + ".jpg");
            fileOutputStream11 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_11 + ".jpg");
            fileOutputStream12 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_12 + ".jpg");
            fileOutputStream13 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_13 + ".jpg");
            fileOutputStream14 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_14 + ".jpg");
            fileOutputStream15 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_15 + ".jpg");
            fileOutputStream16 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_16 + ".jpg");

            BufferedOutputStream bos = new BufferedOutputStream(
                    fileOutputStream);

            myImage.compress(CompressFormat.JPEG, quality, bos);

            bos.flush();
            bos.close();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return true;
    }


private Resources getResources() {
    // TODO Auto-generated method stub
    return null;
}

}

这是我想要展示我的图像的部分:

public class SDCardImagesActivity extends Activity {

private Cursor cursor;
private int columnIndex;
private DBAdapter db; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mood);
    db = new DBAdapter(this); 
    db.open();

    // Set up an array of the Thumbnail Image ID column we want
    String[] projection = {MediaStore.Images.Thumbnails._ID};
    // Create the cursor pointing to the SDCard
    cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
            projection, // Which columns to return
            null,       // Return all rows
            null,
            MediaStore.Images.Thumbnails.IMAGE_ID);

    columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

    GridView sdcardImages = (GridView) findViewById(R.id.gridview);
    sdcardImages.setAdapter(new ImageAdapter(this));

    // Set up a click listener
    sdcardImages.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {
            // Get the data location of the image
            String[] projection = {MediaStore.Images.Media.DATA};
            cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    projection, // Which columns to return
                    null,       // Return all rows
                    null,
                    null);
            columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToPosition(position);
            // Get image filename
            String imagePath = cursor.getString(columnIndex);
            // Use this path to do further processing, i.e. full screen display
        }
    });
}

private class ImageAdapter extends BaseAdapter {

    private Context context;

    public ImageAdapter(Context localContext) {
        context = localContext;
    }

    public int getCount() {
        return cursor.getCount();
    }
    public Object getItem(int position) {
        return position;
    }
    public long getItemId(int position) {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView picturesView;
        if (convertView == null) {
            picturesView = new ImageView(context);
            // Move cursor to current position
            cursor.moveToPosition(position);
            // Get the current value for the requested column
            int imageID = cursor.getInt(columnIndex);
            // Set the content of the image based on the provided URI
            picturesView.setImageURI(Uri.withAppendedPath(
                    MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
            picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            picturesView.setPadding(8, 8, 8, 8);
            picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
        }
        else {
            picturesView = (ImageView)convertView;
        }
        return picturesView;
    }
}

}

对不起,代码很长,但谢谢!

2 个答案:

答案 0 :(得分:1)

强制扫描整个SDCARD使用 sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse(“file://”)             + Environment.getExternalStorageDirectory())));

或仅扫描特定文件夹使用 sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse(“file:// FolderName”)             + Environment.getExternalStorageDirectory())));

答案 1 :(得分:0)

我有类似的问题。为了解决这个问题,我必须先通过adb shell命令提示符在模拟器中手动创建文件夹,然后才能在代码中使用文件路径。它对我有用。文件夹实际上是否存在于模拟器上的该位置?如果没有,那么你必须先手动创建它。