我想创建图片文件夹,并且我想在sdcard中添加flower.png图片。
我的布局文件中有2个按钮,一个是保存并加载,当我点击保存按钮时它必须保存在SD卡中。使用下面的代码,它显示一个toast消息“数据写成功"。但我无法在文件资源管理器中的SD卡上看到创建的文件夹和文件。只有sd卡文件夹对我来说是可见的。当我点击加载按钮时,它必须从sdcard收集保存的图像并在图像视图中设置。但这里也只是获取toast消息“数据在图像视图中成功写入。但是imageview加载图像不显示任何身体可以帮助我吗?
这是可能的布局文件: 的 main.xml中
这是我的活动档案:
public class AndroidExternalStorageActivity extends Activity implements OnClickListener {
Button btnSave,btnLoad;
Bitmap bmp;
String s;
InputStream inputstream;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSave = (Button)findViewById(R.id.save);
btnLoad = (Button)findViewById(R.id.load);
btnSave.setOnClickListener(this);
btnLoad.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if(v.getId()==R.id.save)
{
s=Environment.getExternalStorageState();
if(s.equals(Environment.MEDIA_MOUNTED))
{
File root = Environment.getExternalStorageDirectory();
File f = new File(root+ "/pictures/", "logos.png");
f.mkdir();
try {
//fis = new FileInputStream(R.drawable.logos);
InputStream inputStream = getAssets().open("logo.jpg");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
FileOutputStream fos=new FileOutputStream(f);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputstream.read(bytes)) != -1) {
fos.write(bytes, 0, read);
}
fos.flush();
fos.close();
} catch (Exception e) {
Log.e("saveToExternalStorage()", e.getMessage());
}
//DataOutputStream dos=new DataOutputStream(fos);
//dos.writeBytes("Hello friends how are you,welcome to android session");
Toast.makeText(this,"Data Written successfully.." ,Toast.LENGTH_LONG).show();
}
}
else if(v.getId()==R.id.load)
{
String s1=Environment.getExternalStorageState();
if(s1.equals(Environment.MEDIA_MOUNTED))
{
try
{
File root1 = Environment.getExternalStorageDirectory();
File f1 = new File(root1 + "/pictures/", "logos.png");
ImageView iv=(ImageView)findViewById(R.id.imageView1);
Bitmap bm=BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+ "/pictures/logos.png");
iv.setEnabled(true);
iv.setImageBitmap(bm);
Toast.makeText(this, "image is uploaded successfully in imageview" ,Toast.LENGTH_LONG).show();
//dis.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
}
I also added the following permissions in my manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
</uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE">
</uses-permission>
答案 0 :(得分:0)
您需要单独创建目录和文件。
File directory = new File(root, "pictures");
directory.mkDirs();
File f = new File(directory, "logos.png");
f.createNewFile();
你也可以得到这样的目录
File directory = Environment.getExternalStorageDirectory("pictures");
directory.mkDirs();
答案 1 :(得分:0)
确保您已在AndroidManiFest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
这将为您提供在Android上写入SD卡的完整权限。
答案 2 :(得分:0)
更新save
按钮onClick
选项,如下所示:
Folder Directory
,然后创建File
InputStream
的{{1}}对象,但您要创建另一个名为inputstream
的{{1}}对象,其中InputStream
是资本。你不应该第二次创建它。使用以下更新的代码段比较和更新您的代码,我想您将解决您的问题。
inputStream