2013-10-02 48 views
0

讓我首先說我是編程新手。我試圖創建一個私人畫廊,我已經搜索了網頁,並得到了點點滴滴一起創建這個應用程序將圖片從用戶圖庫複製到SD卡上的文件夾

我有基本庫下來,它顯示我在我的私人文件夾在網格中的圖像和我可以選擇一個圖像放大縮小它以放大和縮小所有這些好東西。

我現在想要做的是...當用戶啓動他們的正常畫廊並選擇一個圖像(或視頻),然後選擇共享選項時,他們選擇我的應用程序。我希望它將文件複製到我的私人位置「/DCIM/privgal/.nomedia/」,然後從其普通圖庫中刪除該文件。

我現在正在使用HTC ONE進行所有測試,當我從「共享」菜單中選擇我的應用程序時,圖庫崩潰並想要將報告發送給HTC。我在LogCat中看不到任何錯誤,就好像它從未實際調用我的應用程序,所以我看不到有什麼問題。

我知道下面的代碼是一團糟,我知道它不是按照原樣運行的,但正如我之前說的,我是這個新手,並將這些零碎收集在一起,並希望得到它與錯誤的工作登錄貓會給我。不幸的是,它不報告任何錯誤,所以我被卡住了。

有人可以看看這個,並指出我在一個工作示例的方向或...我真的很討厭說它,修復我的代碼?

任何幫助表示讚賞!

史蒂夫

Working Code Below posted by Me. (see Below 10/27/2013) 

回答

1

活動只有私有方法,沒有onCreate覆蓋方法,所以沒有方法被調用。實際上這個活動什麼都不做,沒有看法,因此應用程序根本就沒有工作。

您需要覆蓋onCreate方法,使用getInvent作爲,明確,獲取意圖,然後getData爲內容等。

+0

我改變了上面的代碼(見上)以包含onCreate的,但它仍然無法正常工作。 – Sobo

2

這裏是工作的代碼以使用「發送到/ Share菜單」,從畫廊到的圖像複製到一個預定義的隱藏文件夾的存儲和從用戶刪除文件庫

SendToActivity.java

package com.company.privategallery; 

import android.app.Activity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.view.Gravity; 
import android.view.KeyEvent; 
import android.widget.Toast; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

public class SendToActivity extends Activity { 

     private static final int SELECT_PICTURE = 0; 
     //Generating Random Number to use as a unique file name 
     static int random = (int)Math.ceil(Math.random()*100000000); 
     private static String fname = Integer.toString(random); 

     private static String selectedImagePath; 
     //Getting the external Path to the Storage 
     private static String ExternalStorageDirectoryPath = Environment.getExternalStorageDirectory().getAbsolutePath(); 
     //Setting a directory that is already created on the Storage to copy the file to. 
     private static String targetPath = ExternalStorageDirectoryPath + "/DCIM/privgal/.nomedia/"; 

     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      //get the received intent 
      Intent receivedIntent = getIntent(); 

      //get the action 
      String receivedType = receivedIntent.getType(); 

      //make sure it's an action and type we can handle 
       if(receivedType.startsWith("image/")){ 
        //get the uri of the received image 
        Uri receivedUri = (Uri)receivedIntent.getParcelableExtra(Intent.EXTRA_STREAM); 
        selectedImagePath = getPath(receivedUri); 
        System.out.println("Image Path : " + selectedImagePath); 
        //check we have a uri 
        if (receivedUri != null) { 
         //Copy the picture 
          try { 
           copyFile(selectedImagePath, targetPath + fname + ".jpg"); 
          } catch (IOException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 
          startGallery(); 
          deleteFile(); 
          onDestroy(); 
        } 
       } 
     } 

     public void copyFile(String selectedImagePath, String string) throws IOException { 
      InputStream in = new FileInputStream(selectedImagePath); 
      OutputStream out = new FileOutputStream(string); 

      // Transfer bytes from in to out 
      byte[] buf = new byte[1024]; 
      int len; 
      while ((len = in.read(buf)) > 0) { 
       out.write(buf, 0, len); 
      } 
      in.close(); 
      out.close(); 
      Toast customToast = new Toast(getBaseContext()); 
      customToast = Toast.makeText(getBaseContext(), "Image Transferred", Toast.LENGTH_LONG); 
      customToast.setGravity(Gravity.CENTER|Gravity.CENTER, 0, 0); 
      customToast.show(); 
      } 

     private void startGallery() { 
      Intent intent = new Intent(); 
      intent.setType("image/*"); 
      intent.setAction(Intent.ACTION_GET_CONTENT); 
      intent.addCategory(Intent.CATEGORY_OPENABLE); 
      startActivityForResult(intent, SELECT_PICTURE); 
     } 

     // Delete the file that was copied over 
     private void deleteFile() { 
      File fileToDelete = new File(selectedImagePath); 
      boolean fileDeleted = fileToDelete.delete(); 

      // request scan  
      Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
      scanIntent.setData(Uri.fromFile(fileToDelete)); 
      sendBroadcast(scanIntent); 
      sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); 
      finish(); 
     } 

     public String getPath(Uri uri) { 
      String[] projection = { MediaStore.Images.Media.DATA }; 
      Cursor cursor = getContentResolver().query(uri, projection, null, null, null); 
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 
      return cursor.getString(column_index); 
     } 

     @Override 
     public void onDestroy() { 
      super.onDestroy(); 
      android.os.Process.killProcess(android.os.Process.myPid()); 
      finish(); 

     } 

    } 

新增活動和權限在馬納費斯特

權限:

<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"> </uses-permission> 
    <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"> </uses-permission> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission> 

活動:

<activity android:name="com.company.privategallery.SendToActivity" 
     android:exported="true"> 
      <intent-filter> 
       <action android:name="android.intent.action.SEND" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
       <data android:mimeType="image/*" /> 
      </intent-filter> 
    </activity> 
+0

** MISLEADING和ERRONEOUS ** - 沒有內部**存儲清單許可! –

相關問題