我正在尝试使用Mine的代码将日志写入Android文件上的自定义Log.txt文件,但此方法创建文件但不包含任何内容.基本上我想读取文件的先前内容,然后用现有内容附加我的数据.
守则如下:
public static void write(String str)
{
InputStream fileInputStream = null;
FileOutputStream fileOutpurStream = null;
try
{
fileInputStream = new FileInputStream(file);
fileOutpurStream = new FileOutputStream(file);
if(file.exists())
{
int ch = 0;
int current = 0;
StringBuffer buffer = new StringBuffer();
while((ch = fileInputStream.read()) != -1)
{
buffer.append((char) ch);
current++;
}
byte data[]=new byte[(int)file.length()];
fileInputStream.read(data);
fileOutpurStream.write(data);
fileOutpurStream.write(str.getBytes(),0,str.getBytes().length);
fileOutpurStream.flush();
}
else
{
file.createNewFile();
fileOutpurStream.write(str.getBytes(),0,str.getBytes().length);
fileOutpurStream.flush();
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
fileInputStream.close();
fileOutpurStream.flush();
fileOutpurStream.close();
fileOutpurStream = …Run Code Online (Sandbox Code Playgroud) 我将图像保存到SD卡中,直到我拉出SD卡并将其返回后才会出现在Gallery应用程序中.
你知道为什么会这样吗?
看起来像Gallery应用程序有一些缓存没有更新文件保存...
实际上,我还想在Gallery应用程序中打开刚刚保存的图像并且没有成功,
这是我对此问题的疑问.
我有一个带有图片库的应用程序,我希望用户可以将其保存到自己的图库中.我已经创建了一个带有单个声音"保存"的选项菜单,但问题是......我怎样才能将图像保存到图库中?
这是我的代码:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menuFinale:
imgView.setDrawingCacheEnabled(true);
Bitmap bitmap = imgView.getDrawingCache();
File root = Environment.getExternalStorageDirectory();
File file = new File(root.getAbsolutePath()+"/DCIM/Camera/img.jpg");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Run Code Online (Sandbox Code Playgroud)
我不确定这部分代码:
File root = Environment.getExternalStorageDirectory();
File file = new File(root.getAbsolutePath()+"/DCIM/Camera/img.jpg");
Run Code Online (Sandbox Code Playgroud)
保存到画廊是否正确?不幸的是代码不起作用:(
我正在使用第三方文件管理器从文件系统中选择一个文件(在我的情况下为PDF).
这是我启动活动的方式:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(getString(R.string.app_pdf_mime_type));
intent.addCategory(Intent.CATEGORY_OPENABLE);
String chooserName = getString(R.string.Browse);
Intent chooser = Intent.createChooser(intent, chooserName);
startActivityForResult(chooser, ActivityRequests.BROWSE);
Run Code Online (Sandbox Code Playgroud)
这就是我所拥有的onActivityResult:
Uri uri = data.getData();
if (uri != null) {
if (uri.toString().startsWith("file:")) {
fileName = uri.getPath();
} else { // uri.startsWith("content:")
Cursor c = getContentResolver().query(uri, null, null, null, null);
if (c != null && c.moveToFirst()) {
int id = c.getColumnIndex(Images.Media.DATA);
if (id != -1) {
fileName = c.getString(id);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
代码片段来自Open Intents文件管理器说明,网址为: …
因此,在我的应用程序中,我一度将一堆图像保存到临时文件夹中,我希望它们立即显示在图库中.重新启动,他们这样做,但否则他们没有.
我尝试过使用sendBroadcast方法:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
Run Code Online (Sandbox Code Playgroud)
但我得到一个权限错误:
E/AndroidRuntime( 2628): java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=2628, uid=10068
Run Code Online (Sandbox Code Playgroud)
我可以在AndroidManifest中丢失权限,还是不再支持?谢谢
我上了这堂课:
import android.content.Context;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.util.Log;
public class MediaScannerWrapper implements
MediaScannerConnection.MediaScannerConnectionClient {
private MediaScannerConnection mConnection;
private String mPath;
private String mMimeType;
// filePath - where to scan;
// mime type of media to scan i.e. "image/jpeg".
// use "*/*" for any media
public MediaScannerWrapper(Context ctx, String filePath, String mime){
mPath = "/sdcard/DCIM/Camera";
mMimeType = "jpg";
mConnection = new MediaScannerConnection(ctx, this);
}
// do the scanning
public void scan() {
mConnection.connect();
}
// start the scan when scanner …Run Code Online (Sandbox Code Playgroud) 我正在创建一个应用程序,将一些图像从Internet下载到一个特定的文件夹,定义为:
final String pathToPutFiles = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ File.separator + DOWNLOAD_FOLDER_NAME;
Run Code Online (Sandbox Code Playgroud)
我希望制作图库和任何显示媒体的应用程序,以获得这些图像的通知并能够显示它们.
可悲的是,我在Nexus 4上遇到了问题,它有Kitkat版本(确切地说是4.4.2).有时它会起作用,有时则不起作用.
有很多关于这个问题的帖子,我尝试过很多帖子,没有人使用简单的文件夹路径:
这就是我所做的(同样,它在具有Android 4.3的Galaxy S3上运行良好,但有时在具有Android 4.4.2的Nexus 4上不起作用):
protected void notifyMediaScannerOfNewImageFiles(final String folderPathToScan) {
final ArrayList<String> newFilesToScan = new ArrayList<String>();
preparePaths(folderPathToScan, newFilesToScan, true);
final String[] newFilesToScanArr = newFilesToScan.toArray(new String[newFilesToScan.size()]);
MediaScannerConnection.scanFile(getApplicationContext(), newFilesToScanArr, null, null);
}
public static void preparePathsForMediaScanning(final String folderPathToScan, final Collection<String> pathsToFill,
final boolean recursive) {
if (TextUtils.isEmpty(folderPathToScan)) …Run Code Online (Sandbox Code Playgroud) 我的应用程序显示sdcard中的歌曲列表,并且有一个选项可以从SD卡中删除歌曲.
即使删除了歌曲,该歌曲仍会出现在我的应用程序列表中.
如何更新android媒体数据库并显示更新的数据库?
我目前正在开发一个Android应用程序,其中用户拍摄存储在设备上的外部存储器中的照片,然后我也试图让图库扫描文件以将新媒体添加到图库但是这似乎不会更新直到设备重启.
我使用的代码如下:
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Log.v("INFO", mCurrentPhotoPath.toString());
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
Run Code Online (Sandbox Code Playgroud)
使用我的应用程序拍照后,我可以使用设备上的文件管理器应用程序验证它是否存在,并且在将文件传递给Media Scanner之前记录它看起来是正确的文件路径
file:/storage/emulated/0/Pictures/JPEG_20140712_163043_1418054327.jpg
Run Code Online (Sandbox Code Playgroud)
谢谢亚伦
在我的Android应用程序中,我想在库中保存位图,实际上这可以使用下面的代码.唯一的错误是,当我在画廊中打开图像时,细节中创建的时间是错误的.并且说明图像在图库中的顺序不正确.
有人有想法吗?非常感谢您的帮助
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
Bitmap combination = //get my bitmap!
//save in gallery
MediaStore.Images.Media.insertImage(exploreActivity.getContentResolver(),combination,"test_"+ timeStamp + ".jpg",timeStamp.toString());
Run Code Online (Sandbox Code Playgroud)
这里有详细的打印屏幕: