2014-06-25 58 views
0

我想共享一張照片,它位於我的xml視圖中,但任何共享intenet都需要將照片保存在存儲卡上。我怎樣才能使用這張照片?在xml視圖共享照片

這是我的xml:

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textView1" 
    android:layout_below="@+id/textView1" 
    android:layout_marginLeft="17dp" 
    android:text="Button" /> 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:src="@drawable/ellyfish" /> 

回答

0

你可以嘗試這樣的事情:

Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap(); 

使用該位圖上創建一個新的文件:

首先從imageview的得到位圖

public File createImageFromBitmap(Bitmap bitmap) { 
File f ;   
try { 
     ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
     // you can create a new file name "test.jpg" in sdcard folder. 
     File f = new File(Environment.getExternalStorageDirectory() 
       + File.separator + "rwd_temp.jpg"); 
     f.createNewFile(); 
     // write the bytes in file 
     FileOutputStream fo = new FileOutputStream(f); 
     fo.write(bytes.toByteArray()); 
     // remember close de FileOutput 
     fo.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
        return null; 
    } 
     return f; 
} 

我正在做的是從imageview獲取位圖並將該位圖保存在名爲rwd_temp.jpg的文件中。獲得文件後,您可以將其上傳到服務器或共享它。 您需要WRITE_EXTERNAL_STORAGE權限。

+0

即時新編程可以解釋更多?鋤頭我可以保存imageview在SD卡? – user3118791

+0

@ user3118791你沒有保存一個imageview你正在保存的圖像的內容(圖像)表示爲一個位圖在Android中的文件。 –

+0

在第一行,我應該把id我的imageview? – user3118791