内部存储到外部存储
Posted 尚文韬
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了内部存储到外部存储相关的知识,希望对你有一定的参考价值。
JAVA
package com.example.lenovo.lianxiyong;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Enumeration;
public class ShoujicunchuActivity extends AppCompatActivity {
ImageView iv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shoujicunchu);
iv1=(ImageView)findViewById(R.id.iv1);
}
public void bt_1(View v)
{
try {
AssetManager assetManager = getAssets();
InputStream inputStream = assetManager.open("nnn.jpg");
FileOutputStream fileOutputStream = openFileOutput("ttt.jpg", MODE_PRIVATE);
byte[]b=new byte[1024];
int i=0;
while ((i=inputStream.read(b))>0)
{
fileOutputStream.write(b,0,i);
}
fileOutputStream.close();
inputStream.close();
Toast.makeText(ShoujicunchuActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(ShoujicunchuActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
}
}
// public void bt_2(View v)
// {
// String path=getFilesDir().getAbsolutePath()+"ttt.jpg";
// Bitmap bm= BitmapFactory.decodeFile(path);
// }
public void bt_3(View v)
{
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
String path=getFilesDir().getAbsolutePath()+"ttt.jpg";
Bitmap bm= BitmapFactory.decodeFile(path);
try {
String str = bm.toString();
String str1 = getExternalFilesDir("test").getCanonicalPath();
FileOutputStream fos=new FileOutputStream("str1");
PrintStream ps=new PrintStream(fos);
ps.print(str);
ps.close();
fos.close();
}
catch (Exception e)
{
Toast.makeText(ShoujicunchuActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(ShoujicunchuActivity.this, "SD卡未挂载", Toast.LENGTH_SHORT).show();
}
}
}
xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.lenovo.lianxiyong.ShoujicunchuActivity" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/iv1" android:src="@drawable/nnn"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="保存资产文件到内部存储" android:onClick="bt_1"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="转到外部内存" android:onClick="bt_3"/> </LinearLayout>
以上是关于内部存储到外部存储的主要内容,如果未能解决你的问题,请参考以下文章