本文介绍了在类型共享preferences.Editor的方法putString(字符串,字符串)不适用的参数(字符串,InputStream的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图想出一个办法,从我的InputStream保存数据,并在其他活动中使用它。到目前为止,我已经尝试用下面的方式共享preferences - 但我发现了一个错误,说明:

 在类型共享preferences.Editor的方法putString(字符串,字符串)不适用的参数(字符串,InputStream的)

就行了:

  editor.putString(文件名,附件);

怎么可能这是可以避免的?

来源:

 保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    //获取附件的文件名
    意向theIntent = getIntent();
    字符串attachmentFileName =没有文件名找到;
    如果(theIntent = NULL&放大器;!&安培;!theIntent.getData()= NULL){
        光标C = getContentResolver()查询(theIntent.getData(),空,
                NULL,NULL,NULL);
        c.moveToFirst();
        最终诠释fileNameColumnId = C
                .getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
        如果(fileNameColumnId> = 0)
            attachmentFileName = c.getString(fileNameColumnId);        尝试{            InputStream的附件= getContentResolver()。openInputStream(
                    。getIntent()的getData());
            共享preferences preferences = preferenceManager
                    .getDefaultShared preferences(本);
            共享preferences.Editor编辑器= preferences.edit();
            editor.putString(文件名,附件);
            editor.commit();
        }赶上(FileNotFoundException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
        }    }


解决方案

您不能在的InputStream 保存在共享preferences 因为只有原始数据( INT 布尔字符串等),可以保存这种方式。

你应该做的是读取数据,将其保存到字符串,然后你可以存储字符串的:

 的BufferedReader读者=新的BufferedReader(新的InputStreamReader(附件));
StringBuilder的出=新的StringBuilder();
串线;
而((行= reader.readLine())!= NULL){
    out.append(线);
}

那么你可以做

  editor.putString(文件名,out.toString());

如果您的真正的要实际的InputStream 发送到另一个活动(这是奇怪的,但我是谁的判断),但是,你可以用它做 intent.putSerializableExtra(附件)

I'm attempting to figure out a way to save the data from my InputStream and use it in another activity. So far I've tried to use sharedPreferences in the manner below - but I'm getting an error stating:

"The method putString(String, String) in the type SharedPreferences.Editor is not applicable for the arguments (String, InputStream)"

on the line:

editor.putString("fileName", attachment);

How might this be avoided?

Source:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // get the attachment's filename
    Intent theIntent = getIntent();
    String attachmentFileName = "No file name found";
    if (theIntent != null && theIntent.getData() != null) {
        Cursor c = getContentResolver().query(theIntent.getData(), null,
                null, null, null);
        c.moveToFirst();
        final int fileNameColumnId = c
                .getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
        if (fileNameColumnId >= 0)
            attachmentFileName = c.getString(fileNameColumnId);

        try {

            InputStream attachment = getContentResolver().openInputStream(
                    getIntent().getData());
            SharedPreferences preferences = PreferenceManager
                    .getDefaultSharedPreferences(this);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString("fileName", attachment);
            editor.commit();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
解决方案

You can't store the InputStream in SharedPreferences because only primitive data (int,boolean,String, etc.) can be saved that way.

What you should do is read the data, save it to a String and then you can store the string (see here):

BufferedReader reader = new BufferedReader(new InputStreamReader(attachment));
StringBuilder out = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
    out.append(line);
}

then you can do

editor.putString("fileName", out.toString());

If you really want to send the actual InputStream to another activity (which is odd, but who am I to judge), however, you can do it using intent.putSerializableExtra(attachment).

这篇关于在类型共享preferences.Editor的方法putString(字符串,字符串)不适用的参数(字符串,InputStream的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 17:57