0

我正在尝试使用以下代码从外部存储中获取图像:

public class MyStorage extends Activity{

public static boolean mExternalStorageAvailable;
public static boolean mExternalStorageWriteable;

public static void checkAvailability(){
    //Checking media availability
    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        // We can read and write the media
        mExternalStorageAvailable = mExternalStorageWriteable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // We can only read the media
        mExternalStorageAvailable = true;
        mExternalStorageWriteable = false;
    } else {
        // Something else is wrong. It may be one of many other states, but all we need
        //  to know is we can neither read nor write
        mExternalStorageAvailable = mExternalStorageWriteable = false;
    }
}

public static ArrayList<String> getFiles(){
    checkAvailability();
    ArrayList<String> result = new ArrayList<String>();
    if(mExternalStorageAvailable){
        File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        String basePath = folder.toString();
        result = listDirectory(folder, basePath);
    }
    return result;
}

private static ArrayList<String> listDirectory(File folder, String basePath){
    String[] list = folder.list();
    ArrayList<String> result = new ArrayList<String>();
    for (int i = 0; i < list.length; i++) {
        File test = new File(basePath+"/"+list[i]);
        if(test.isDirectory()){
            result.addAll(listDirectory(test,basePath+"/"+list[i]));
        }else{
            result.add(basePath+"/"+list[i]);
        }
    }
    return result;
}
}

我使用这个对象:

for(int i = 0; i<result.size(); i++){
            String filename = result.get(i);
            String filenameArray[] = filename.split("\\.");
            String extension = filenameArray[filenameArray.length-1];
            if(extension.equals("jpg") || extension.equals("JPG") || extension.equals("png") || extension.equals("PNG") ){
                //get file
                File file = new File(result.get(i));

                Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                myBitmap = MyImages.resizeImage(100,myBitmap,2);

                imageViewList.add(new ImageView(this));
                imageViewList.get(imageViewList.size()-1).setImageBitmap(myBitmap);

                layout.addView(imageViewList.get(imageViewList.size()-1));

                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)imageViewList.get(imageViewList.size()-1).getLayoutParams();
                params.setMargins(0, 30, 22, 0);
                imageViewList.get(imageViewList.size()-1).setLayoutParams(params);

                //add onclick listner on the image
                imageViewList.get(imageViewList.size()-1).setOnClickListener(new OnClickListener() {
                    public void onClick(View arg0) {
                        //remove all backgrounds
                        for(int i=0; i<imageViewList.size();i++){
                            imageViewList.get(i).setBackgroundResource(android.R.color.transparent);
                        }
                        //set the border
                        arg0.setBackgroundResource(R.drawable.border_image_follow);

                        Drawable d = ((ImageView) arg0).getDrawable();
                        Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
                        ByteArrayOutputStream stream = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                        byte[] bitmapdata = stream.toByteArray();
                        image = Base64.encodeToString(bitmapdata,0);

                    }
                });
            }
        }

我有一个错误,但我无法调试它。当我处于调试模式时,我的 sd 卡已卸载,因此我无法访问存储,但是当我正常运行应用程序时,sd 卡已安装并且我可以访问存储,但我有一个错误。

有没有办法在安装我的 sd 卡的情况下进行调试?

谢谢

4

1 回答 1

1

关闭设备上的 USB 大容量存储模式

于 2013-04-02T20:00:56.633 回答