我有相机应用程序。它应该拍照,然后将其存储到外部存储,但我得到了以下日志错误。请帮我。为什么我收到此错误?
这是logcat:
以下是存储中的商店代码:
Bitmap bitmap = BitmapFactory.decodeByteArray(Constant.imageData,
0, Constant.imageData.length);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, stream);
File file = new File(Environment.getExternalStorageDirectory().toString()
+ File.separator + "Android" + File.separator + "data"
+ File.separator + context.getPackageName()
+ File.separator + "files" + File.separator + "image.jpg");
Log.e("pa", file.getPath() + " : " + file.getAbsolutePath());
Log.e("len", stream.size() + "");
file.createNewFile();
/*
* Log.e("Camrera", "yesssssssssssssssssss"); Log.e("Camrera",
* pictureFile.getAbsolutePath());
*/
FileOutputStream fos = new FileOutputStream(file);
fos.write(stream.toByteArray());
fos.close();
答案 0 :(得分:1)
我已回答此问题链接如下:
Android: Get a file from http and store in SDCard
根据您的要求修改代码
以下是代码:
private void savePrivateExternalFile(String fileURL, String fName) {
HttpURLConnection connection = null;
URL url = null;
try {
url = new URL(fileURL);
connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty(BConstant.WEB_SERVICES_COOKIES,
cookie);
connection.setDoOutput(true);
connection.connect();
} catch (IOException e1) {
e1.printStackTrace();
}
File folderDir = null;
folderDir = new File(getExternalFilesDir("Directory Name") + "/Files");
File file = new File(folderDir, fName);
if (file.exists()) {
file.delete();
}
if ((folderDir.mkdirs() || folderDir.isDirectory())) {
try {
InputStream inputStream = connection.getInputStream();
BufferedInputStream bufferedInputStream = null;
bufferedInputStream = new BufferedInputStream(inputStream,
1024 * 5);
FileOutputStream fileOutputStream = new FileOutputStream(
folderDir + "/" + fName);
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len1);
}
bufferedInputStream.close();
fileOutputStream.close();
inputStream.close();
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
如果要打开已下载的文件,请使用此选项:
File file = new File(getExternalFilesDir("Directory Name")+ "/Files/" + fileName);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),"application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
答案 1 :(得分:1)
此代码解释了很多关于如何将图像存储到SD卡并将图像添加到MediaStore的内容。
File imagesFolder;
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
if(BitmapImage != null)
{
BitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
}
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO)
{
imagesFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"/MyAppName");
}
else
{
imagesFolder= new File (Environment.getExternalStorageDirectory() + "/dcim/"+"MyAppName");
}
if (!imagesFolder.exists())
{
imagesFolder.mkdirs();
}
try
{
myfile = File.createTempFile("Image_Name", ".jpeg", imagesFolder);
FileOutputStream out = new FileOutputStream(myfile);
out.write(bytes.toByteArray());
out.flush();
out.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
Uri contentUri = Uri.fromFile(myfile);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
答案 2 :(得分:0)
您是否使用了许可?
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>