码迷,mamicode.com
首页 > 其他好文 > 详细

内部存储到外部存储

时间:2016-05-28 11:28:32      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

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();
        }
    }
}
View Code

 

 

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>
View Code

 

内部存储到外部存储

标签:

原文地址:http://www.cnblogs.com/1ming/p/5536908.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!