在我的应用程序中,我需要在设备存储中存储大量图像.这些文件往往满足设备存储,我想让用户能够选择外部SD卡作为目标文件夹.
我到处读到Android不允许用户写入外部SD卡,SD卡是指外部和可安装的SD卡而不是外部存储器,但文件管理器应用程序设法在所有Android版本上写入外部SD.
在不同的API级别(Pre-KitKat,KitKat,Lollipop +)上授予对外部SD卡的读/写访问权限的更好方法是什么?
更新1
我从Doomknight的答案中尝试了方法1,但没有结果:正如您所看到的那样,我在尝试在SD上写入之前在运行时检查权限:
HashSet<String> extDirs = getStorageDirectories();
for(String dir: extDirs) {
Log.e("SD",dir);
File f = new File(new File(dir),"TEST.TXT");
try {
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)==PackageManager.PERMISSION_GRANTED) {
f.createNewFile();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
但是我在两个不同的设备上尝试了访问错误:HTC10和Shield K1.
10-22 14:52:57.329 30280-30280/? E/SD: /mnt/media_rw/F38E-14F8
10-22 14:52:57.329 30280-30280/? W/System.err: java.io.IOException: open failed: EACCES (Permission denied)
10-22 14:52:57.329 30280-30280/? W/System.err: at java.io.File.createNewFile(File.java:939)
10-22 14:52:57.329 30280-30280/? W/System.err: at com.myapp.activities.TestActivity.onResume(TestActivity.java:167)
10-22 14:52:57.329 30280-30280/? W/System.err: at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1326)
10-22 14:52:57.330 30280-30280/? W/System.err: at android.app.Activity.performResume(Activity.java:6338)
10-22 14:52:57.330 …Run Code Online (Sandbox Code Playgroud) 我需要截取屏幕截图Activity(没有标题栏,用户不应该看到实际拍摄的截图),然后通过动作菜单按钮"分享"进行分享.我已经尝试了一些解决方案,但它们对我不起作用.有任何想法吗?
我创建了一个位图,现在我想将该位图保存到某个目录.任何人都可以告诉我这是如何完成的.谢谢
FileInputStream in;
BufferedInputStream buf;
try {
in = new FileInputStream("/mnt/sdcard/dcim/Camera/2010-11-16_18-57-18_989.jpg");
buf = new BufferedInputStream(in);
Bitmap _bitmapPreScale = BitmapFactory.decodeStream(buf);
int oldWidth = _bitmapPreScale.getWidth();
int oldHeight = _bitmapPreScale.getHeight();
int newWidth = 2592;
int newHeight = 1936;
float scaleWidth = ((float) newWidth) / oldWidth;
float scaleHeight = ((float) newHeight) / oldHeight;
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
Bitmap _bitmapScaled = Bitmap.createBitmap(_bitmapPreScale, 0, 0, oldWidth, oldHeight, matrix, true);
Run Code Online (Sandbox Code Playgroud)
(我想将_bitmapScaled保存到SD卡上的文件夹中)
我需要将一些Drawable资源导出到一个文件.
例如,我有一个函数返回给我一个Drawable对象.我想把它写到一个文件中/sdcard/drawable/newfile.png.我该怎么做?
我想知道如何通过点击按钮将图像保存到用户的SD卡.有人可以告诉我该怎么做.Image为.png格式,存储在drawable目录中.我想编写一个按钮将该图像保存到用户的SD卡.
正如标题所暗示的那样,我试图让我的Android应用程序的用户从他的设备中选择一个图像(完成),然后我想缩小图像(完成),压缩/转换图像到png并发送它API作为base64字符串.
所以我现在调整图像大小如下:
options.inSampleSize = calculateInSampleSize(options, MAX_IMAGE_DIMENSION, MAX_IMAGE_DIMENSION);
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
Run Code Online (Sandbox Code Playgroud)
然后我有一个位图,我想将其转换为PNG,然后从那里转换为base64.我发现一些示例代码转换为PNG并将其存储在设备上这里.
try {
FileOutputStream out = new FileOutputStream(filename);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
问题是我不想保存图像.我只想将它作为PNG保存在内存中,然后将其进一步转换为base64字符串.
有没有人知道如何将图像转换为png并将其存储在变量中,或者甚至更好,立即将其转换为base64?欢迎所有提示!
actionBar图标应该像
图像
当设备分辨率为1920x1080和Android 4.2以上时,图标大小看起来非常小:
图像
(android 4.1是正确的)
我的问题似乎是我想改变动作栏图标大小.
图标图像大小为113x40,然后我使用getActionBar().getHeight()来获取ActionBar高度为56.
menu.xml文件:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_buyer"
android:icon="@drawable/buyer_bts"
android:showAsAction="ifRoom"/>
<item
android:id="@+id/menu_exhibition_bt"
android:icon="@drawable/exhibition_bt"
android:showAsAction="ifRoom"/>
<item
android:id="@+id/menu_oj"
android:icon="@drawable/download_bt"
android:showAsAction="ifRoom"/>
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法,但不起作用:
创建更大的图标图像(ex.170x60)然后放入drawable-hdpi(xhdpi,xxhdpi ......等)文件夹
使用menu.findItem(R.id.menu_buyer).setIcon(resizeImage(int resId,int w,int h)); 调整图标大小
在style.xml中添加了一个样式<item name ="android:actionBarSize"> 200dp </ item>,然后在Manifest文件中设置主题
有没有办法可以正确显示图标?
我想将位图作为附件发送到邮件中.图像不存储在SDCARD中或设备中的任何位置.位图对象在运行时创建,应作为附件发送.
我想将Drawable转换为int然后反之亦然.基本上我想将Arraylist对象保存到sharedPrefrence中.为此我有实施Gson到Srtring转换方法.如果我在这里使用Drawable而不是int,那么Gson String转换需要很多时间.所以我想使用int而不是Drawable.
private List<AppInfo> apps = null;
public void setIcon(int icon) {
this.icon = icon;
}
apps.get(position).setIcon(mContext.getResources().getDrawable(apps.get(position).getIcon()));
Run Code Online (Sandbox Code Playgroud)
AppInfo在哪里
public AppInfo(String appname, String pname, String versionName, int versionCode, int icon, int color) {
this.appname = appname;
this.pname = pname;
this.versionName = versionName;
this.versionCode = versionCode;
this.icon = icon;
this.color = color;
}
Run Code Online (Sandbox Code Playgroud)
以下是将Custom对象的ArrayList转换为String的源代码,以便我可以将其保存到SharedPrefrence.
Gson gson = new Gson();
apps.get(number).setColor(picker.getColor());
String JsonAppsdata = gson.toJson(apps);
System.out.println("Storing="+JsonAppsdata);
utility.StoreData(getApplicationContext(), JsonAppsdata);
Run Code Online (Sandbox Code Playgroud)