【问题标题】:How to set image from text file in android如何在android中从文本文件中设置图像
【发布时间】:2013-07-29 09:13:52
【问题描述】:

我想通过以下方式将图像设置为 ImageView

1) 点击按钮图片库打开时

2) 选择任何图像并设置为图像视图并将该图像路径存储到文本文件

我做了所有这些工作,但是当应用程序关闭时,重新启动,ImageView 不会通过从文本文件中检索图像路径来显示上一个图像

代码如下 公共无效 onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

    img = (ImageView) findViewById(R.id.imageView1);
    ((Button) findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT); 
            startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
        }

    });
}

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
        if (requestCode == SELECT_PICTURE) { 
            Uri selectedImageUri = data.getData(); 
            selectedImagePath = getPath(selectedImageUri); 
            System.out.println("Image Path : " + selectedImagePath); 
            img.setImageURI(selectedImageUri); 
            File myFile = new File("/sdcard/ImageLocation.txt"); 
            try { 
                myFile.createNewFile(); 
                FileOutputStream fOut = new FileOutputStream(myFile); 
                OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut); 
                myOutWriter.append(selectedImagePath); 
                myOutWriter.close(); 
                fOut.close(); 
                Toast.makeText(getBaseContext(),"Done writing SD ",Toast.LENGTH_SHORT).show();
            } catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace();
            } 
        }
    }
}

public String getPath(Uri uri) { 
    String[] projection = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = managedQuery(uri, projection, null, null, null); 
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst(); 

    return cursor.getString(column_index); 
}

【问题讨论】:

    标签: android file imageview


    【解决方案1】:

    重启时你在哪里读取 ImagePath?请显示相同的代码。此外,在将 ImagePath 写入文本文件时,您应该执行以下操作

    File myFile = new File(Environment.getExternalStorageDirectory(), "ImageLocation.txt"); 
        try { 
            if(!myFile.exists()) {
                myFile.createNewFile();
            }
            FileOutputStream fOut = new FileOutputStream(myFile); 
            OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut); 
            myOutWriter.append(selectedImagePath); 
            myOutWriter.close(); 
            fOut.close(); 
        } catch (IOException e) { 
            e.printStackTrace();
        }
    

    在阅读文件时,您还应该使用Environment.getExternalStorageDirectory()阅读

    编辑:

    如果您只想查看图片库,则不应使用intent.setAction(Intent.ACTION_GET_CONTENT);,将其更改为intent.setAction(Intent.ACTION_PICK);

    现在,下面将是您的文件路径读取功能(您的文件写入代码将始终覆盖现有的存储路径..因此您将始终只存储一个路径....如果您希望存储多个图像path 那么你应该修改你的文件写入和下面的文件读取部分)。

    public String getStoredPath() {
        String path = null;
        File myFile = new File(Environment.getExternalStorageDirectory(), "ImageLocation.txt"); 
        if(myFile.exists()) {
            //Read text from file
            try { 
                BufferedReader br = new BufferedReader(new FileReader(myFile)); 
                String line; 
                // Below while loop will only run once as your file writing 
                // always overwrites the existing line
                while ((line = br.readLine()) != null) { 
                    path = line;
                } 
            } catch (IOException e) { 
    
            } 
        }
    
        return path;
    } 
    

    然后在你要设置图片uri的地方。

    String imagePath = getStoredPath();
    if(imagePath != null) {
        img.setImageURI(Uri.parse(imagePath));
    }
    

    确保对文件写入进行建议的更改。此外,如果您只想存储和获取一个图像路径,请考虑使用共享首选项。

    【讨论】:

    • 代码在哪里?你在说public String getPath(Uri uri)吗?
    • 文件 file = new File("/sdcard/ImageLocation.txt"); if(file.exists()) { //从文件中读取文本 StringBuilder text = new StringBuilder();尝试 { BufferedReader br = new BufferedReader(new FileReader(file));字符串线; while ((line = br.readLine()) != null) { text.append(line); } } catch (IOException e) { } /// img.setImageURI(selectedImageUri); // 我想在这里分配图像 } else { Toast.makeText(getApplicationContext(), "抱歉文件不存在!!", Toast.LENGTH_LONG).show(); } }
    • 正如我在回答中所传达的,您需要在读取或写入文件时使用File file = new File(Environment.getExternalStorageDirectory(), "ImageLocation.txt");。你还在使用/sdcard。请更改相同的
    • 另外,如果文本文件中只有一个条目,请考虑使用 SharedPreferences。如果文本文件中有很多条目,那么您需要注意阅读不同的条目
    • 如果我想读取已经存储在 ImageLocation.txt 文件中的图像路径并检索该路径的路径并将图像设置为图像视图,我如何编写 img.setImageURI(selectedImageUri);但没用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多