当前位置: 首页>>代码示例>>Java>>正文


Java Context.openFileOutput方法代码示例

本文整理汇总了Java中android.content.Context.openFileOutput方法的典型用法代码示例。如果您正苦于以下问题:Java Context.openFileOutput方法的具体用法?Java Context.openFileOutput怎么用?Java Context.openFileOutput使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在android.content.Context的用法示例。


在下文中一共展示了Context.openFileOutput方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: add

import android.content.Context; //导入方法依赖的package包/类
/**
 * Adds an account to the Forge account storage
 *
 * @param context calling context
 * @param account the Forge account to add to the storage
 * @return the account added to the storage, if it's a failure returns null
 */
public static ForgeAccount add(Context context, ForgeAccount account) {
    // Initialise GSON
    Gson gson = new Gson();
    // Get the already saved Forge accounts
    HashMap<UUID, ForgeAccount> accounts = FileManager.load(context);
    // Give the account a random UID
    account.setId(java.util.UUID.randomUUID());
    // Add the new account to the hash map
    accounts.put(account.getId(), account);
    // Convert the list of Forge Accounts to a JSON string
    String jsonString = gson.toJson(new ArrayList<>(accounts.values()));
    // Internal save
    FileOutputStream outputStream;
    try {
        // Save the JSON to the file
        outputStream = context.openFileOutput(context.getString(R.string.filename_forge_accounts), Context.MODE_PRIVATE);
        outputStream.write(jsonString.getBytes());
        outputStream.close();
        return account;
    } catch (Exception e) {
        // If there is an error, log it
        Log.e(Forge.ERROR_LOG, e.getMessage());
        return null;
    }
}
 
开发者ID:jthomperoo,项目名称:Forge,代码行数:33,代码来源:FileManager.java

示例2: delete

import android.content.Context; //导入方法依赖的package包/类
/**
 * Deletes a Forge account from the storage
 *
 * @param context calling context
 * @param account the Forge account to delete
 * @return true = successfully deleted, false = failure in delete attempt
 */
public static boolean delete(Context context, ForgeAccount account) {
    // Initialise GSON
    Gson gson = new Gson();
    // Get the already saved Forge accounts
    HashMap<UUID, ForgeAccount> accounts = FileManager.load(context);
    // Remove the account with the matching UUID
    accounts.remove(account.getId());
    // Convert the list of Forge Accounts to a JSON string
    String jsonString = gson.toJson(new ArrayList<>(accounts.values()));
    // Internal save
    FileOutputStream outputStream;
    try {
        // Save the JSON to the file
        outputStream = context.openFileOutput(context.getString(R.string.filename_forge_accounts), Context.MODE_PRIVATE);
        outputStream.write(jsonString.getBytes());
        outputStream.close();
        return true;
    } catch (Exception e) {
        // If there is an error, log it
        Log.e(Forge.ERROR_LOG, e.getMessage());
        return false;
    }
}
 
开发者ID:jthomperoo,项目名称:Forge,代码行数:31,代码来源:FileManager.java

示例3: writeFileToInternalStorage

import android.content.Context; //导入方法依赖的package包/类
/**
 * Write a file to internal storage.  Used to set up our dummy "cloud server".
 *
 * @param context   the Context
 * @param resId     the resource ID of the file to write to internal storage
 * @param extension the file extension (ex. .png, .mp3)
 */
private void writeFileToInternalStorage(Context context, int resId, String extension) {
    InputStream ins = context.getResources().openRawResource(resId);
    int size;
    byte[] buffer = new byte[1024];
    try {
        String filename = context.getResources().getResourceEntryName(resId) + extension;
        FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE);
        while ((size = ins.read(buffer, 0, 1024)) >= 0) {
            fos.write(buffer, 0, size);
        }
        ins.close();
        fos.write(buffer);
        fos.close();

    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
开发者ID:googlesamples,项目名称:android-ContentProviderPaging,代码行数:26,代码来源:ImageProvider.java

示例4: newSession

import android.content.Context; //导入方法依赖的package包/类
public static void newSession(Context context) {
    sSessionCount++;
    sAutoSuggestCount = 0;
    sBackspaceCount = 0;
    sAutoSuggestUndoneCount = 0;
    sManualSuggestCount = 0;
    sWordNotInDictionaryCount = 0;
    sTypedChars = 0;
    sActualChars = 0;
    sState = State.START;
    
    if (LOGGING) {
        try {
            sKeyLocationFile = context.openFileOutput("key.txt", Context.MODE_APPEND);
            sUserActionFile = context.openFileOutput("action.txt", Context.MODE_APPEND);
        } catch (IOException ioe) {
            Log.e("TextEntryState", "Couldn't open file for output: " + ioe);
        }
    }
}
 
开发者ID:PhilippC,项目名称:keepass2android,代码行数:21,代码来源:TextEntryState.java

示例5: writeToFile

import android.content.Context; //导入方法依赖的package包/类
public static void writeToFile(Context context,ArrayList<String> list, String fileName)
{
    try
    {
        FileOutputStream stream = context.openFileOutput(fileName,Context.MODE_PRIVATE);

        for (String item : list)
        {
            stream.write((item + "\n").getBytes());
        }
        stream.close();
    }
    catch (Exception e)
    {

    }
}
 
开发者ID:lanceeeaton,项目名称:Text-Rabbit,代码行数:18,代码来源:GameUtils.java

示例6: writeToFileFloat

import android.content.Context; //导入方法依赖的package包/类
public static void writeToFileFloat(Context context,ArrayList<Float> list, String fileName)
{
    try
    {
        FileOutputStream stream = context.openFileOutput(fileName,Context.MODE_PRIVATE);

        for (Float item : list)
        {
            stream.write((Float.toString(item) + "\n").getBytes());
        }
        stream.close();
    }
    catch (Exception e)
    {

    }
}
 
开发者ID:lanceeeaton,项目名称:Text-Rabbit,代码行数:18,代码来源:GameUtils.java

示例7: createLockFile

import android.content.Context; //导入方法依赖的package包/类
/**
 * Creates lockfile
 * @return boolean
 */
public boolean createLockFile(Context context, String text) {
    try {
        Log.i( "info", "createLockFile: start");
        FileOutputStream fos = context.openFileOutput("lockfile", context.MODE_WORLD_WRITEABLE);
        String string = text + "\n";
        fos.write(string.getBytes());
        fos.close();
        Log.i( "info", "createLockFile: done");
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

    return true;
}
 
开发者ID:HTBridge,项目名称:pivaa,代码行数:20,代码来源:Authentication.java

示例8: add

import android.content.Context; //导入方法依赖的package包/类
/**
 * save/add a page/url to the history
 *
 * @param context
 * @param url
 */
static public void add(Context context, String url)
{
    try
    {
        FileOutputStream outputStream = context.openFileOutput(HISTORY_FILE,
                Context.MODE_APPEND
        );

        outputStream.write(url.getBytes());
        outputStream.write("\n".getBytes());
        outputStream.close();
    }
    catch (IOException e)
    {
        throw new RuntimeException(e);
    }
}
 
开发者ID:afonsotrepa,项目名称:PocketGopher,代码行数:24,代码来源:History.java

示例9: write

import android.content.Context; //导入方法依赖的package包/类
/**
 * 写文本文件 在Android系统中,文件保存在 /data/data/PACKAGE_NAME/files 目录下
 *
 * @param context
 */
public static void write(Context context, String fileName, String content) {
	if (content == null)
		content = "";

	try {
		FileOutputStream fos = context.openFileOutput(fileName,
				Context.MODE_PRIVATE);
		fos.write(content.getBytes());

		fos.close();
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
开发者ID:Evan-Galvin,项目名称:FreeStreams-TVLauncher,代码行数:20,代码来源:FileUtils.java

示例10: writeToFile

import android.content.Context; //导入方法依赖的package包/类
public static void writeToFile(String data, Context context) {
    try {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("imagenes.json", Context.MODE_PRIVATE));
        outputStreamWriter.write(data);
        outputStreamWriter.close();
    }
    catch (IOException e) {
        Log.e("Exception", "File write failed: " + e.toString());
    }
}
 
开发者ID:ur13l,项目名称:Guanajoven,代码行数:11,代码来源:FileUtils.java

示例11: writeToFile

import android.content.Context; //导入方法依赖的package包/类
/**
 * Write to a file
 * @param context The context
 * @param fileName File to be written
 * @param data The data to be written to the file
 * @return True if successful, false if not
 */
public static boolean writeToFile(Context context, String fileName, String data) {
    FileOutputStream outputStream;
    try {
        outputStream = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        outputStream.write(data.getBytes());
        outputStream.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
 
开发者ID:wkmeijer,项目名称:CS4160-trustchain-android,代码行数:20,代码来源:Util.java

示例12: save

import android.content.Context; //导入方法依赖的package包/类
private void save(Context context) {
    byte[] byteBuffer = new byte[Double.SIZE / Byte.SIZE];

    try {
        FileOutputStream stream = context.openFileOutput(ScoresFileName, Context.MODE_PRIVATE);

        ByteBuffer.wrap(byteBuffer).putDouble(BuildConfig.VERSION_CODE);
        stream.write(byteBuffer);

        for (Map.Entry<Integer, List<ScoreEntry>> group : this.scores.entrySet()) {
            stream.write(group.getKey());
            stream.write(group.getValue().size());

            for (ScoreEntry score : group.getValue()) {
                double[] data = score.saveData();

                for (double val : data) {
                    ByteBuffer.wrap(byteBuffer).putDouble(val);
                    stream.write(byteBuffer);
                }
            }
        }

        stream.close();
        Log.i("HighScores", "Saved");
    } catch (Exception e) {
        Log.e("HighScores", "Autosave failed", e);
    }
}
 
开发者ID:subchannel13,项目名称:EnchantedFortress,代码行数:30,代码来源:HighScores.java

示例13: write

import android.content.Context; //导入方法依赖的package包/类
public static boolean write(String filename, Context context, Object o) {
    boolean success = false;
    try {
        FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE);
        ObjectOutputStream os = new ObjectOutputStream(fos);
        os.writeObject(o);
        fos.flush();
        os.close();
        success = true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return success;
}
 
开发者ID:dmllr,项目名称:IdealMedia,代码行数:15,代码来源:FileUtils.java

示例14: writeOut

import android.content.Context; //导入方法依赖的package包/类
@TargetApi(Build.VERSION_CODES.KITKAT)
public static void writeOut(SaveData saveData, Context context) throws IOException {

    try (FileOutputStream fos = context.openFileOutput("Scans.ser", Context.MODE_PRIVATE)) {
        ObjectOutputStream os = new ObjectOutputStream(fos);
        os.writeObject(saveData);
        os.close();
        fos.close();
    }
    catch (Exception e) {

    }
}
 
开发者ID:RowanACM,项目名称:RFID-Attendance,代码行数:14,代码来源:ReadWrite.java

示例15: saveImageToStorage

import android.content.Context; //导入方法依赖的package包/类
public static void saveImageToStorage(Bitmap image, String key, Context context) {
	try {
		// Create an ByteArrayOutputStream and feed a compressed bitmap image in it
		ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
		image.compress(Bitmap.CompressFormat.PNG, 100, byteStream); // PNG as only format with transparency

		// Create a FileOutputStream with out key and set the mode to private to ensure
		// Only this app and read the file. Write out ByteArrayOutput to the file and close it
		FileOutputStream fileOut = context.openFileOutput(key, Context.MODE_PRIVATE);
		fileOut.write(byteStream.toByteArray());
		byteStream.close();
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
开发者ID:SebastianRask,项目名称:Pocket-Plays-for-Twitch,代码行数:16,代码来源:Service.java


注:本文中的android.content.Context.openFileOutput方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。