首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >写入和读取文件

写入和读取文件
EN

Stack Overflow用户
提问于 2015-08-05 00:46:32
回答 1查看 143关注 0票数 0

我在写入和读取文件时遇到问题。

代码如下:

代码语言:javascript
复制
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);


    if (resultCode == Activity.RESULT_OK) {

        switch(requestCode) {
            case INTENT_COUNTRY:
                if (data.getExtras().containsKey("country")) {
                    final String c = data.getStringExtra("country");
                    Log.d("Profile", "Country: " + c);
                    txtCountry.setText(c);
                }
                break;

            case PICKER_CAMERA:
                Log.d("Profile", "PICKER_CAMERA");
                Bitmap bitmapCamera = (Bitmap) data.getExtras().get("data");
                Bitmap thumbnailCamera = ThumbnailUtils.extractThumbnail(bitmapCamera, 320, 320);

                ByteArrayOutputStream streamCamera = new ByteArrayOutputStream();
                thumbnailCamera.compress(Bitmap.CompressFormat.PNG, 100, streamCamera);
                byte[] byteArrayCamera = streamCamera.toByteArray();
                InputStream isCamera = new ByteArrayInputStream(byteArrayCamera);

                uploadPicture(isCamera);

                break;

            case PICKER_GALLERY:
                Log.d("Profile", "PICKER_GALLERY");
                Uri imageUri = data.getData();

                try {
                    Bitmap bitmapGallery = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), imageUri);
                    Bitmap thumbnailGallery = ThumbnailUtils.extractThumbnail(bitmapGallery, 320, 320);

                    ByteArrayOutputStream streamGallery = new ByteArrayOutputStream();
                    thumbnailGallery.compress(Bitmap.CompressFormat.PNG, 100, streamGallery);
                    byte[] byteArrayGallery = streamGallery.toByteArray();
                    InputStream isGallery = new ByteArrayInputStream(byteArrayGallery);

                    uploadPicture(isGallery);

                } catch(IOException e) {
                    e.printStackTrace();
                }

                break;

            default:
                Log.d("Profile", "Unmanaged request code: " + requestCode);
                break;
        }
    }
}

public void uploadPicture(InputStream is) {
    final ProgressDialog progress = new ProgressDialog(getActivity());
    progress.setIndeterminate(true);
    progress.setCancelable(false);
    progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progress.setTitle(getString(R.string.loading));
    progress.setMessage(getString(R.string.loading_msg));
    progress.show();

    /* Upload*/
    AppDelegate appDelegate = (AppDelegate) getActivity().getApplication();
    appDelegate.setPicture(is, new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            Log.d("Profile", "Upload ok");
            progress.dismiss();
            return null;
        }
    }, new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            Log.d("Profile", "Upload failed");
            progress.dismiss();
            return null;
        }
    });
}

public void setPictureFile(final InputStream is) {
    String filename = "pict.png";
    FileOutputStream outputStream;
    try {
        outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
        outputStream.write(IOUtils.toByteArray(is));
        outputStream.close();
        Log.d("Profile", "File stored locally.");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public Bitmap getPictureFile() {
    String filename = "pict.png";
    FileInputStream inputStream;
    //String filePath = getFilesDir().getAbsolutePath() + "/" + filename;
    Bitmap bitmap = null;
    try {
        inputStream = openFileInput(filename);
        BufferedInputStream buf = new BufferedInputStream(inputStream);
        bitmap = BitmapFactory.decodeStream(buf);
        //Bitmap bitmap = BitmapFactory.decodeFile(filename);
        if (bitmap == null) {
            Log.d("Profile", "WARNING: bitmap == null");
        }

        if (inputStream != null) {
            inputStream.close();
        }
        if (buf != null) {
            buf.close();
        }
    } catch(FileNotFoundException e) {
        Log.d("Profile", "Picture FILE not found.");
        e.printStackTrace();
        return null;
    } catch (OutOfMemoryError e) {
        Log.d("Profile", "Out Of Memory");
    } catch(Exception e) {
        e.printStackTrace();
    }
    return bitmap;
}

在控制台中,我总是有:

代码语言:javascript
复制
WARNING: bitmap == null

setPictureFile方法中的InputStream不为空(上传到web服务的工作正常),并且我在setPictureFile中没有得到任何异常。

另一方面,当我试图读取文件时,位图似乎是空的,没有异常正在上升!

我试图读取的文件大约是200-300 KB,所以它并不大,我没有耗尽内存。

有人知道这是怎么回事吗?

EN

回答 1

Stack Overflow用户

发布于 2015-08-05 18:27:50

解决了。到处使用byte[]而不是InputStream解决了我的问题。

代码语言:javascript
复制
public void setPictureFile(final byte[] buffer) {
    String filename = "pict.png";
    FileOutputStream outputStream;

    try {
        outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
        outputStream.write(buffer);
        outputStream.close();
        Log.d("Profile", "File stored locally.");
    } catch (Exception e) {
        Log.d("Profile", e.toString());
        e.printStackTrace();
    }
}

public Bitmap getPictureFile() {
    String filename = "pict.png";
    FileInputStream inputStream;

    // http://stackoverflow.com/questions/11182714/bitmapfactory-example
    // http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

    //String filePath = getFilesDir().getAbsolutePath() + "/" + filename;

    Bitmap bitmap = null;

    try {
        inputStream = openFileInput(filename);
        byte[] reader = new byte[inputStream.available()];
        if (inputStream.read(reader)!=-1) {
            Log.d("Profile", "Reading from stream...");
        }
        Log.d("Profile", "Stream length: " + reader.length);
        bitmap = BitmapFactory.decodeByteArray(reader, 0, reader.length);
        //BufferedInputStream buf = new BufferedInputStream(inputStream);
        //bitmap = BitmapFactory.decodeStream(inputStream);

        //Bitmap bitmap = BitmapFactory.decodeFile(filename);

        if (bitmap == null) {
            Log.d("Profile", "WARNING: bitmap == null");
        }

        inputStream.close();

        //if (buf != null) {
        //    buf.close();
        //}
    } catch(FileNotFoundException e) {
        Log.d("Profile Exception", e.toString());
        e.printStackTrace();
    } catch (OutOfMemoryError e) {
        Log.d("Profile Exception", e.toString());
    } catch(Exception e) {
        Log.d("Profile Exception", e.toString());
        e.printStackTrace();
    }

    return bitmap;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31814906

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档