如何从内部存储中检索图像?

时间:2019-11-26 13:47:23

标签: android android-image android-internal-storage

我的项目是从存储中检索图像..当sdcard存在时它工作正常,但是当sd卡移除时它崩溃了。我是否需要在代码中包含任何内容?。有人可以建议我该怎么做吗?是可能获得Android设备的“内部下载文件夹”路径?以下是我的活动代码,请看看

活动:

public class NextActivity extends AppCompatActivity {
ImageView myImage;
private int count;
private Bitmap[] thumbnails;
private boolean[] thumbnailsselection;
private String[] arrPath;
private ImageAdapter imageAdapter;
ArrayList<String> f = new ArrayList<String>();// list of file paths
File[] listFile;
GridView imagegrid;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_next);
    myImage = (ImageView) findViewById(R.id.gridImage);
    getFromSdcard();
    imagegrid = (GridView) findViewById(R.id.griddown);
    imageAdapter = new ImageAdapter();
    imagegrid.setAdapter(imageAdapter);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    assert navigationView != null;
    //navigationView.setNavigationItemSelectedListener((NavigationView.OnNavigationItemSelectedListener) this);
    if (getSupportActionBar() != null){
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }
    // getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    //getSupportActionBar().setHomeButtonEnabled(true);
}
public void getFromSdcard() {
    if (Environment.getExternalStorageState() == null) {
        //create new file directory object
       File directory = new File(Environment.getDataDirectory()
                + "/hairImages/");
       File photoDirectory = new File(Environment.getDataDirectory()
                + "/Hair/");
        /*
         * this checks to see if there are any previous test photo files
         * if there are any photos, they are deleted for the sake of
         * memory
         */
        if (photoDirectory.exists()) {
            File[] dirFiles = photoDirectory.listFiles();
            if (dirFiles.length != 0) {
                for (int ii = 0; ii <= dirFiles.length; ii++) {
                    dirFiles[ii].delete();
                }
            }
        }
        // if no directory exists, create new directory
        if (!directory.exists()) {
            directory.mkdir();
        }

        // if phone DOES have sd card
    } else if (Environment.getExternalStorageState() != null) {
        // search for directory on SD card
        File file= new File(Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DOWNLOADS);
if (file.isDirectory()) { listFile = file.listFiles();
Log.d("Images","ImageSize - " + listFile.length);
for (int i = 0; i < listFile.length; i++) {
    f.add(listFile[i].getAbsolutePath());
}
}
    }
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        finish(); // close this activity and return to preview activity (if there is any)
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {

    super.onBackPressed();
}
public class ImageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public ImageAdapter() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return f.size();
    }

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.grid_list_down, null);
            holder.imageview = (ImageView) convertView.findViewById(R.id.gridImage);

            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }


        Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position));
        holder.imageview.setImageBitmap(myBitmap);
        return convertView;
    }
}
class ViewHolder {
    ImageView imageview;


}}

任何帮助将不胜感激。

这是没有sdcard而崩溃的代码:

   public void getFromSdcard() {
  File file= new File(Environment.getExternalStorageDirectory() + "/"Environment.DIRECTORY_DOWNLOADS);
if (file.isDirectory()) { listFile = file.listFiles();
Log.d("Images","ImageSize - " + listFile.length);
for (int i = 0; i < listFile.length; i++) {
    f.add(listFile[i].getAbsolutePath());
}
}

}

我尝试了这种方法,但是失败了:

 public void getFromSdcard() {
    if (Environment.getExternalStorageState() == null) {

        File file= new File(Environment.DIRECTORY_DOWNLOADS+"/my_file.JPEG");
        if (file.isDirectory()) { listFile = file.listFiles();
            Log.d("Images","ImageSize - " + listFile.length);
            for (int i = 0; i < listFile.length; i++) {
                f.add(listFile[i].getAbsolutePath());
            }
        }
    }


   else if (Environment.getExternalStorageState() != null){


        File file= new File(Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DOWNLOADS+"/");
if (file.isDirectory()) { listFile = file.listFiles();
Log.d("Images","ImageSize - " + listFile.length);
for (int i = 0; i < listFile.length; i++) {
    f.add(listFile[i].getAbsolutePath());
}
}}

1 个答案:

答案 0 :(得分:0)

为避免崩溃,请先检查sd卡是否是其存储卡,然后再使用所选文件(如果您检查文件是否存在)将解决您的问题,并且如果您正在使用kotlin,请使用 let 可以帮助您处理空指针