| 안드로이드에서 공유버튼으로 넘어온 그림을 저장하는 방법 |
Tech - https://ohyung.net/643 (YMD: 12/06/26 00:40)
안드로이드에서 가끔 저방버튼은 없으면서 공유하기는 있는 앱들이 있다.
특히나 안드로이드 페이스북앱.
그래서 저장하기 버튼 대용으로 공유 -> 저장 하기를 만들어봄.

안드로이드 앱 -> 인텐트 -> 파일 저장 Flow를 따라서 저장하면 됨.

Code Type : JAVA
Intent intent = getIntent();
로 File Intent 를 받아내고

Code Type : JAVA
bitmap = Images.Media.getBitmap(getContentResolver(), uri);
로 Bitmap에 넣어준다음

Code Type : JAVA
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
파일에 써버리면 된다.

아래는 전체적인 소스.

Code Type : JAVA
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent intent = getIntent();

        if (intent != null) {
            Bundle extras = intent.getExtras();

            if (extras != null) {
                Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
                TextView tv = (TextView) findViewById(R.id.textView1);

                if (uri != null) {
                    Bitmap bitmap = null;
                    try {
                        bitmap = Images.Media.getBitmap(getContentResolver(), uri);
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    ImageView iv = (ImageView) findViewById(R.id.imageView1);
                    iv.setImageURI(uri);

                    if (bitmap != null) {
                        long time = System.currentTimeMillis();
                        Date date = new Date(time);
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
                        String filename = sdf.format(date);

                        String path = Environment.getExternalStorageDirectory().toString();
                        String saveDirectory = "/DCIM/Ohyung/";
                        File file = new File(path, saveDirectory + filename + ".jpg");
                        File folder = new File(path, saveDirectory);
                       if(!folder.isDirectory()) {
                           folder.mkdirs();
                        }
                       
                        try {
                            file.createNewFile();
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                        
                        tv.setText(path + saveDirectory + filename + ".jpg");

                        try {
                            //Log.d("ohyung", "fuck1!");
                            FileOutputStream out = new FileOutputStream(file);
                            //Log.d("ohyung", "fuck2!");
                            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
                            out.flush();
                            out.close();
                            //Log.d("ohyung", "fuck3!");
                        } catch (Exception e) {
                            //Log.d("ohyung", "fuck!XXXX");

                        }

                        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
                                + Environment.getExternalStorageDirectory())));

                    }
                }
            }

        }

그냥 오랜만에 블로그에 글 남기기 ㅎㅎ
| 이 포스트에 대한 이용규약 |
Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시 2.0 라이센스 에 따라 이용하실 수 있습니다.
This work is licensed under a Creative Commons Attribution 2.0 Korea LicenseLink in a new window.

| 이 글과 태그로 연관된 글 |

| 트랙백 |
트랙백 주소 :: https://ohyung.net/rserver.php?mode=tb&sl=643