我将图像添加到SDCARD上的文件夹中.由于图像和我的文件夹在图库中没有立即可见,我试图让MediaScannerConnection更新并在图库中显示文件夹/图像.这对我来说不是很好,因为Gallery中没有任何内容.我只在Eclipse AVD中测试.
我没有看到很多关于这个的讨论,因为scanFile是自api8以来的新功能.有人能说明这是怎么做的吗?
我在服务和Activity中尝试它但在onScanCompleted时继续获得uri = null.
这是我的代码:
private boolean writeToSD(Bitmap bm, String url) {
if (canIWriteOnSD()) {
File sd = Environment.getExternalStorageDirectory();
File dest = new File(sd, "MoveInBlue/");
try {
url = urlCleaner(url);
if (!dest.exists()) {
dest.mkdir();
}
File file = new File(dest, url + ".png");
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
return true;
} catch (Exception e) {
e.printStackTrace();
// Do nothing
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
问题解决了:
urlCleaner现在返回url.substring(url.lastIndexOf('?')+ 1),一切都按预期工作.
实际上是例外file.createNewFile();,我不知道为什么.
非常感谢你.
(urlCleaner只是从URL中删除http:// ...并保留php标记)
这是LogCat: …
这是我的代码,我希望保存图像按钮只是将图像保存到图库.
package com.nk_apps.hip.hop.lyric.wallpapers;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class Wallpapers extends Activity implements OnClickListener {
}
ImageView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.wallpapers);
display = (ImageView) findViewById(R.id.IVDisplay);
ImageView image1 = (ImageView) findViewById(R.id.IVimage1);
ImageView image2 = (ImageView) findViewById(R.id.IVimage2);
ImageView image3 = (ImageView) findViewById(R.id.IVimage3);
ImageView image4 = (ImageView) findViewById(R.id.IVimage4);
ImageView image5 = (ImageView) findViewById(R.id.IVimage5);
ImageView image6 = (ImageView) findViewById(R.id.IVimage6);
ImageView image7 = (ImageView) findViewById(R.id.IVimage7);
ImageView image8 …Run Code Online (Sandbox Code Playgroud) 不幸的是,我发现的解决方案不适用于Android 5.1.1.我有一个名为source的位图.我需要将它直接保存到我手机的图库中.我的清单包含 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
你能给我一个工作方法吗?
我想下载一个图像并将其存储在本地存储中的特定文件夹中.
我用这个来下载图片:
var imageData = await AzureStorage.GetFileAsync(ContainerType.Image, uploadedFilename);
var img = ImageSource.FromStream(() => new MemoryStream(imageData));
Run Code Online (Sandbox Code Playgroud) 我想通过拍摄布局的屏幕截图将我的框架布局保存到图库中.这段代码适用于一个项目(名称:试用版),但当我在其他项目(名称:Fg)上尝试相同的代码时,它无效.照片未保存在图库中.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_background);
}
public Bitmap click(View view) {
FrameLayout memecontentView =(FrameLayout) findViewById(R.id.my);
View view1 = memecontentView;
Bitmap b = Bitmap.createBitmap(view1.getWidth(), view1.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(b);
view1.draw(canvas);
String extr = Environment.getExternalStorageDirectory().toString();
File myPath = new File(extr, getString(R.string.free_tiket)+".jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage(getContentResolver(), b,
"Screen", "screen");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) { …Run Code Online (Sandbox Code Playgroud) 我正在开发一个基本的相机应用程序。我可以拍照,并且可以在我的应用程序中查看它。但我想在图库中异步查看我的照片。重新启动手机后,我可以在图库中看到我的照片。
示例代码。
public class PhotosActivity extends Fragment implements View.OnClickListener {
private final int REQUEST_CODE = 100;
private Button fotobutton;
private ImageView foto_image;
private static final String IMAGE_DIRECTORY_NAME = "OlkunMustafa";
public static final int MEDIA_TYPE_IMAGE = 1;
private Uri fileUri;
// Directory name to store captured images and videos
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.photos_activity,container,false);
fotobutton = (Button) rootView.findViewById(R.id.fotobutton);
foto_image = (ImageView) rootView.findViewById(R.id.foto_image);
fotobutton.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View v) { …Run Code Online (Sandbox Code Playgroud)