当我尝试将位图存储到存储中时出现此错误 #
File path = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "picture");
if (! path.exists()) {
path.mkdirs();
if (!path.exists()) {
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HH_mm_ss", Locale.CHINA).format(new Date());
File imagePath = new File(path.getPath() + "_" + "IMG_" + timeStamp + ".jpg");
BufferedOutputStream fos;
try {
fos =new BufferedOutputStream(new FileOutputStream(imagePath));
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
return imagePath;
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
return null;
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
return null;
}
Run Code Online (Sandbox Code Playgroud)
fos = new BufferedOutputStream(new …
当我将数据写入Stream时,我期待以下代码抛出异常:
File file = new File("test.txt");
FileOutputStream fs = new FileOutputStream(file);
OutputStreamWriter ow = new OutputStreamWriter(fs);
BufferedWriter writer = new BufferedWriter(ow);
fs.close();
try {
ow.write(65);
writer.write("test");
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我意识到我应该关闭BufferedWriter,但在我当前的环境中,可能有可能在BufferedWriter关闭之前关闭FileOutputStream.FileOutputStream不应该抛出一个IOException,它应该向上移动直到它到达我的try/catch块并打印堆栈跟踪?
如果我尝试调用fs.write(65),那么它会抛出异常.
原始代码及其工作将数据保存到SD卡
// Writing data to internal storage
btnSaveData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isSDCardWritable()) {
String dataToSave = etData.getText().toString();
try {
// SD Card Storage
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File(sdCard.getAbsolutePath()+"/MyFiles");
directory.mkdirs();
File file = new File(directory, "text.txt");
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos);
// write the string to the file
osw.write(dataToSave);
osw.flush();
osw.close();
. . .
Run Code Online (Sandbox Code Playgroud)
然后我更改了代码以根据我的需要添加新值:
osw.append(dataToSave);
osw.flush();
osw.close();
Run Code Online (Sandbox Code Playgroud)
问题是:它覆盖文本文件而不是附加.我错过了什么?谢谢你的帮助
我正在使用Volley作为我在Android中正在进行的项目中的网络堆栈.我的部分要求是下载可能非常大的文件并将其保存在文件系统中.
我一直在研究凌空的实现,似乎凌空的唯一方法就是将整个文件下载到一个潜在的大量字节数组中,然后将这个字节数组的处理推迟到一些回调处理程序.
由于这些文件可能非常大,我担心下载过程中出现内存不足错误.
有没有办法告诉volley将http输入流中的所有字节直接处理成文件输出流?或者这需要我实现自己的网络对象?
我在网上找不到任何关于此的资料,所以任何建议都将不胜感激.
我想解码一个base64字符串并将其转换为一个文件(如pdf/jpg)并将其保存到设备,例如在(/storage/emulated/0/Download/file.pdf)中.
对于编码文件我使用此代码:
File originalFile = new File("/mnt/sdcard/download/file.pdf");
String encodedBase64 = null;
try {
FileInputStream fileInputStreamReader = new FileInputStream(originalFile);
byte[] bytes = new byte[(int) originalFile.length()];
fileInputStreamReader.read(bytes);
encodedBase64=Base64.encodeToString(bytes,Base64.NO_WRAP);
messaggio=encodedBase64.toString();
//encodedBase64 = new String(Base64.encode(bytes));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
现在,我想取消这个base64字符串并将其转换为文件并将其保存在设备上...
有人能帮我吗?
谢谢大家=)
我正在开发一个照片编辑器应用程序,在编辑我的图片后,我将它保存到我的本地存储中。它在 android 9 之前工作正常,但在 android 10 上没有。它在 Android 10 中显示“未找到此类文件或目录”的异常。经过一些研究,我发现 getExternalFilesDir() 在 android Q+ 中已弃用。但是我在 android 10 中找不到任何正确的方法来做到这一点。所以如果有人可以提供教程,那将非常有帮助。
我已经添加并授予了使用权限 android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 以防它是问题,但它没有解决任何问题。
这是我的尝试(使用 ParcelFileDescriptor):
private void fileAccessForAndroidQ(Uri fileUri){
try {
ParcelFileDescriptor parcelFileDescriptor = this.getContentResolver().openFileDescriptor(fileUri, "r", null);
InputStream inputStream = new FileInputStream(parcelFileDescriptor.getFileDescriptor());
Cursor returnCursor =
getContentResolver().query(fileUri, null, null, null, null);
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
returnCursor.moveToFirst();
fileName = returnCursor.getString(nameIndex);
file = new File(this.getFilesDir(), fileName);
OutputStream outputStream = new FileOutputStream(file);
IOUtils.copyStream(inputStream, outputStream);
}catch (Exception e){
Toast.makeText(this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
任何形式的帮助将不胜感激。
android file fileoutputstream android-external-storage android-10.0
这里完美地描述了如何做到这一点,唯一的问题是:他不知道这个功能openFileOutput();
private void saveSettingsFile() {
String FILENAME = "settings";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); //openFileOutput underlined red
try {
fos.write(string.getBytes());
fos.close();
} catch (IOException e) {
Log.e("Controller", e.getMessage() + e.getLocalizedMessage() + e.getCause());
}
}
Run Code Online (Sandbox Code Playgroud)
这些是我导入的相关包:
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
Run Code Online (Sandbox Code Playgroud) 我想要做的是:我希望我的应用程序从Internet下载图像并将其保存到手机的内部存储器中,该位置是应用程序专用的位置.如果列表项没有可用的图像(即无法在Internet上找到),我想要显示默认的占位符图像.这是我在list_item_row.xml文件中定义的默认图像.
在我的ListActivity文件中,我正在调用我编写的CustomCursorAdapter类的实例.它在CustomCursorAdapter中,我遍历所有列表项并定义需要映射到视图的内容,包括尝试从内部存储器读取它的图像文件.
我已经看到了关于这个主题的几个问题,但这些例子要么特定于外部手机内存(例如SDCard),涉及保存字符串而不是图像,要么涉及使用Bitmap.CompressFormat来降低文件的分辨率(这是不必要的)我的情况,因为这些图像将是已经很小分辨率的小缩略图).试图将每个示例中的代码拼凑起来一直很困难,因此我询问了我的具体示例.
目前,我相信我已经编写了有效的代码,但没有显示我的列表项的图像,包括默认的占位符图像.我不知道问题是由无效的下载/保存代码或无效的读取代码引起的 - 我不知道如何检查内部存储器以查看图像是否存在.
无论如何,这是我的代码.任何帮助将不胜感激.
ProductUtils.java
public static String productLookup(String productID, Context c) throws IOException {
URL url = new URL("http://www.samplewebsite.com/" + productID + ".jpg");
URLConnection connection = url.openConnection();
InputStream input = connection.getInputStream();
FileOutputStream output =
c.openFileOutput(productID + "-thumbnail.jpg", Context.MODE_PRIVATE);
byte[] data = new byte[1024];
output.write(data);
output.flush();
output.close();
input.close();
}
Run Code Online (Sandbox Code Playgroud)
CustomCursorAdapter.java
public class CustomCursorAdapter extends CursorAdapter {
public CustomCursorAdapter(Context context, Cursor c) {
super(context, c);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ImageView thumbnail …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现我可以多次启动和停止视频录制的功能,并将视频数据累积到File.
这就是我准备我的媒体记录器的方法:
private boolean prepareVideoRecorder(){
mMediaRecorder = new MediaRecorder();
//0 for landscape
//90 for portrait
//Check for available profile
CamcorderProfile profile = null;
if(CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_480P)){
Log.d(TAG, "480p");
profile = CamcorderProfile.get(CamcorderProfile.QUALITY_480P);
}else if(CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_720P)){
Log.d(TAG, "720p");
profile = CamcorderProfile.get(CamcorderProfile.QUALITY_720P);
}else{
Log.d(TAG, "LOW");
profile = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW);
}
// Step 1: Unlock and set camera to MediaRecorder
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
// Step 2: Set sources
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Step 3: Set profile
mMediaRecorder.setProfile(profile);
// Step 4: Set output file and pass media recorder the …Run Code Online (Sandbox Code Playgroud) 嗨我正在尝试从资产中读取excel并希望将其转换为JSON,但我收到错误:打开失败:ENOENT(没有这样的文件或目录),搜索了许多SO问题但找不到解决方案下面是我的代码
public void readXlsFileAndConvertToJsonObject() {
JSONArray jsonArray = new JSONArray();
JSONArray mainJsonArray = new JSONArray();
try {
File file = new File("file:///android_asset/filters.xls");
FileInputStream fis = new FileInputStream(file);
//final FileInputStream file = new FileInputStream(new File("filters.xls"));
int count = 0;
// Get the workbook instance for XLS file
HSSFWorkbook workbook = new HSSFWorkbook(fis);
// Get first sheet from the workbook
HSSFSheet sheet = workbook.getSheetAt(0);
// Iterate through each rows from first sheet
Iterator < Row > rowIterator = sheet.iterator();
while …Run Code Online (Sandbox Code Playgroud) fileoutputstream ×10
android ×9
java ×3
android-10.0 ×1
base64 ×1
file ×1
file-io ×1
inputstream ×1
java-io ×1
save ×1
text-files ×1